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.

InÂ [Â ]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

InÂ [Â ]:
(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)

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step
train_x:  (60000, 28, 28)
test_x:  (10000, 28, 28)

1. Plot a ramdomly selected data with its label
InÂ [Â ]:
def batchmaker(x, y, n_batch):
idx = np.random.randint(len(x), size = n_batch)
return x[idx], y[idx]

InÂ [Â ]:
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()

1. Define LSTM Structure

InÂ [Â ]:
## write your code here
#

1. 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
InÂ [Â ]:
## write your code here
#

1. Define optimization configuration and then optimize
• Highly recommand to use GPU or Colab.
InÂ [Â ]:
## write your code here
#

1. 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.
InÂ [Â ]:
## 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.