Thursday, October 24, 2019

Python code snippets for RNNs


 How to build a vanilla RNN in Keras.

# import libraries
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import SimpleRNN

# define parameters
n_output = # number of classes in case of classification, 1 in case of regression
output_activation = # “softmax” or “sigmoid” in case of classification, “linear” in case of regression

# ---- build RNN architecture ----

# instantiate sequential model
model = Sequential()

# add the first hidden layer
n_cells = #number of neurons to add in the hidden layer
time_steps = # length of sequences
features = # number of features of each entity in the sequence

model.add(SimpleRNN(n_cells, input_shape=(time_steps, features)))

# add output layer
model.add(Dense(n_output, activation=output_activation)

No comments:

Post a Comment