9951 explained code solutions for 126 technologies


python-kerasHow do I use Python Keras to create a Recurrent Neural Network (RNN) example?


A Recurrent Neural Network (RNN) example in Python Keras can be created by following these steps:

  1. Import the necessary libraries, such as keras and numpy:
import keras
import numpy as np
  1. Define the model:
model = keras.Sequential()
model.add(keras.layers.Embedding(input_dim=1000, output_dim=64))
model.add(keras.layers.LSTM(64))
model.add(keras.layers.Dense(1, activation='sigmoid'))
  1. Compile the model:
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])
  1. Generate dummy training data:
x_train = np.random.random((1000, 10))
y_train = np.random.randint(2, size=(1000, 1))
  1. Train the model:
model.fit(x_train, y_train, epochs=10, batch_size=32)
  1. Generate dummy test data:
x_test = np.random.random((100, 10))
y_test = np.random.randint(2, size=(100, 1))
  1. Evaluate the model:
model.evaluate(x_test, y_test)

The output of the last command should be a list of two numbers, the first being the loss and the second being the accuracy.

Helpful links

Edit this code on GitHub