**AI for Mechanical Engineering**

**Recurrent Neural Networks (RNN)**

# Problem 1: LSTM with TensorFlowÂ¶

In this problem, you will make a LSTM model to predict the half of an MNIST image using the other half.

You will split an MNIST image into 28 pieces.

MNIST is 28 x 28 image. The model predicts a piece of 1 x 28 image.

First, 14 x 28 image will be feeded into a model as time series, then the model predict the last 14 x 28 image, recursively.

```
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
```

- Load MNIST Data

- Download MNIST data from the tensorflow tutorial example

```
(train_imgs, train_labels), (test_imgs, test_labels) = tf.keras.datasets.mnist.load_data()
train_imgs = train_imgs/255.0
test_imgs = test_imgs/255.0
print('train_x: ', train_imgs.shape)
print('test_x: ', test_imgs.shape)
```

- Plot a ramdomly selected data with its label

```
def batchmaker(x, y, n_batch):
idx = np.random.randint(len(x), size = n_batch)
return x[idx], y[idx]
```

```
train_x, train_y = batchmaker(train_imgs, train_labels, 1)
img = train_x[0].reshape(28, 28)
plt.figure(figsize = (5, 3))
plt.imshow(img,'gray')
plt.title("Label : {}".format(train_y[0]))
plt.xticks([])
plt.yticks([])
plt.show()
```

- Define LSTM Structure

```
## write your code here
#
```

- Define Cost, Initializer and Optimizer Loss

- Regression: Squared loss

$$ \frac{1}{N} \sum_{i=1}^{N} (\hat{y}^{(i)} - y^{(i)})^2$$

Initializer

- Initialize all the empty variables

Optimizer

- AdamOptimizer: the most popular optimizer

```
## write your code here
#
```

- Define optimization configuration and then optimize

- Highly recommand to use GPU or Colab.

```
## write your code here
#
```

- Test or Evaluate

- Predict the MNIST image
- MNIST is 28 x 28 image. The model predicts a piece of 1 x 28 image.
- First, 14 x 28 image will be feeded into a model, then the model predict the last 14 x 28 image, recursively.

```
## write your code here
#
```

# Problem 2Â¶

In this problem, we have bearing data with 3 classes (healthy, inner fault, outer fault).

The objective is to classify the given data using state-of-the-art deep learning models.

You will build and test a deep learning model.

### Dataset DescriptionÂ¶

The bearing data is collected by a sensory system which has 2 channels: __vibration__ and __rotational speed__. You can refer to the paper to see the specification in detail. The experimental setup is shown in the below figure. The dataset contains 36 files with 3 classes, 2 sensor positions, and 4 speed varying conditions. Every data is sampled at 200,000 Hz of sampling frequency and 10 seconds of duration. We will use only the **increasing speed** condition and the **channel 1** (vibration data) for the sake of simplicity.