9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to create a recurrent neural network?


To use Python and Keras to create a recurrent neural network, you will need to import the necessary libraries and create the model.

import keras
from keras.layers import SimpleRNN

# Create a simple RNN model
model = keras.models.Sequential()
model.add(SimpleRNN(3, input_shape=(2, 10)))
model.add(keras.layers.Dense(1))
model.compile(optimizer='adam', loss='mse')

This example code creates a simple RNN model with 3 neurons in the hidden layer, 2 timesteps, and 10 features in the input. The output is a single number.

Code explanation

  1. import keras - imports the Keras library for use in the code
  2. from keras.layers import SimpleRNN - imports the SimpleRNN layer from the Keras library
  3. model = keras.models.Sequential() - creates a sequential model
  4. model.add(SimpleRNN(3, input_shape=(2, 10))) - adds a SimpleRNN layer with 3 neurons, 2 timesteps, and 10 features to the model
  5. model.add(keras.layers.Dense(1)) - adds a dense layer with a single output to the model
  6. model.compile(optimizer='adam', loss='mse') - compiles the model with the Adam optimizer and mean squared error loss

Helpful links

Edit this code on GitHub