如何使用SimpleRNN建模模函数并预测多对多序列

时间:2018-06-21 11:35:17

标签: keras recurrent-neural-network

我设计了这个玩具问题,以了解SimpleRNN在Keras中的工作。

我的输入顺序是:

 [x1,x2,x3,x4,x5]

,相应的输出是:

[(0+x1)%2,(x1+x2)%2,(x2+x3)%2,(x3+x4)%2,(x4+x5)%2)]

我的代码是:

import numpy as np
import random
from scipy.ndimage.interpolation import shift
def generate_sequence():
    max_len = 5
    x = np.random.randint(1,100,max_len)
    shifted_x = shift(x, 1, cval=0)
    y = (x + shifted_x) % 2
    return x.reshape(max_len,1),y.reshape(max_len,1),shifted_x.reshape(max_len,1)

X_train = np.zeros((100,5,1))
y_train = np.zeros((100,5,1))
for i in range(100):
    x,y,z = generate_sequence()
    X_train[i] = x
    y_train[i] = y

X_test = np.zeros((100,5,1))
y_test = np.zeros((100,5,1))
for i in range(100):
    x,y,z = generate_sequence()
    X_test[i] = x
    y_test[i] = y

from keras.layers import SimpleRNN
model = Sequential()
model.add(SimpleRNN(3,input_shape=(5,1),return_sequences=True,name='rnn'))
model.add(Dense(1,activation='sigmoid'))

# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

print('Train...')
model.fit(X_train, y_train,
          batch_size=70,
          epochs=200,verbose=0,validation_split=0.3)

score, acc = model.evaluate(X_test, y_test,
                            batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

训练SimpleRNN时,我的准确率只有50%,序列中的每个项目仅取决于上一个项目。 RNN为什么要努力学习这一点?

100/100 [==============================] - 0s 37us/step
Test score: 0.6975522041320801
Test accuracy: 0.5120000243186951

更新: 事实证明,mod函数很难建模,我切换到简单的数据生成策略,例如y [t] = x [t]

def generate_rnn_sequence():
    max_len = 5
    x = np.random.randint(1,100,max_len)
    shifted_x = shift(x, 1, cval=0)
    y = (x < shifted_x).astype(float)
    return x.reshape(5,1),y.reshape(5,1)

那我该如何使用RNN为mod函数建模呢?

0 个答案:

没有答案