python - How to generate sequence using LSTM? -
i want generate sequence when particular input activated. want generate odd or sequence according corresponding input neuron activation. trying create model using lstm because can remember short term order.
i tried way
import numpy np keras.models import sequential keras.layers import dense,lstm x=np.array([[1,0], [0,1]]) y=np.array([[1,3,5,7,9], [2,4,6,8,10]]) model = sequential() model.add(dense(10, input_shape=(2)) model.add(lstm(5, return_sequences=true)) model.add(lstm(5, return_sequences=false)) model.add(dense(5)) model.compile(loss='mse', optimizer='adam') model.fit(x,y)
but when tried fit model giving me error
nameerror: name 'model' not defined
to use rnns in keras need introduce additional dimension data: timesteps. in case want have 5 timesteps. because want have one-to-many relationship between input , output data need replicate input data 5 times. last lstm
layer must set return sequences, since want result every timestep , not last one. make dense
layers aware of time dimension need wrap them timedistributed
layer. , last dense layer has 1 output, since output 1 result each timestep.
import numpy np keras.models import sequential keras.layers import dense,lstm keras.layers.wrappers import timedistributed x=np.array([[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0]], [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]]) y=np.array([[[ 1], [ 3], [ 5], [ 7], [ 9]], [[ 2], [ 4], [ 6], [ 8], [10]]]) model = sequential() model.add(timedistributed(dense(10), input_shape=(5, 2))) model.add(lstm(5, return_sequences=true)) model.add(lstm(5, return_sequences=true)) model.add(timedistributed(dense(1))) model.compile(loss='mse', optimizer='adam') model.fit(x,y, nb_epoch=4000) model.predict(x)
with after 4000 epochs following result:
epoch 4000/4000 2/2 [==============================] - 0s - loss: 0.0032 out[20]: array([[[ 1.02318883], [ 2.96530271], [ 5.03490496], [ 6.99484348], [ 9.00506973]], [[ 2.05096436], [ 3.96090508], [ 5.98824072], [ 8.0701828 ], [ 9.85805798]]], dtype=float32)
Comments
Post a Comment