简体   繁体   English

简单的回归神经网络输入形状

[英]Simple Recurrent Neural Network input shape

I am trying to code a very simple RNN example with keras but the results are not as expected. 我试图用keras编写一个非常简单的RNN示例,但结果并不像预期的那样。

My X_train is a repeated list with length 6000 like: 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, ... 我的X_train是一个长度为6000的重复列表,如:1,0,0,0,0,0,1,0,0,0 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, ...

I formatted this to shape: (6000, 1, 1) 我将其格式化为: (6000, 1, 1)

My y_train is a repeated list with length 6000 like: 1, 0.8, 0.6, 0, 0, 0, 1, 0.8, 0.6, 0, ... 我的y_train是一个长度为6000的重复列表,如:1,0.8,0.6,0,0,0,1,0.8,0.6,0 1, 0.8, 0.6, 0, 0, 0, 1, 0.8, 0.6, 0, ...

I formatted this to shape: (6000, 1) 我将其格式化为: (6000, 1)

In my understanding, the recurrent neural network should learn to predict the 0.8 and 0.6 correctly because it can remember the 1 in X_train two timesteps ago. 根据我的理解,递归神经网络应该学会正确地预测0.8和0.6,因为它可以记住两个时间段前X_train中的1。

My model: 我的模特:

model=Sequential()
model.add(SimpleRNN(input_dim=1, output_dim=50))
model.add(Dense(output_dim=1, activation = "sigmoid"))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(X_train, y_train, nb_epoch=10, batch_size=32)

The model can be trained successfully with minimal loss ~0.1015 but the results are not as expected. 该模型可以成功训练,损失最小〜0.1015但结果不如预期。

test case ---------------------------------------------  model result -------------expected result 

model.predict(np.array([[[1]]])) --------------------0.9825--------------------1

model.predict(np.array([[[1],[0]]])) ----------------0.2081--------------------0.8

model.predict(np.array([[[1],[0],[0]]])) ------------0.2778 -------------------0.6

model.predict(np.array([[[1],[0],[0],[0]]]))---------0.3186--------------------0

Any hints what I am misunderstanding here? 我在这里有什么误解吗?

The input format should be three-dimensional: the three components represent sample size, number of time steps and output dimension 输入格式应该是三维的:三个组件代表样本大小,时间步数和输出维度

Once appropriately reformatted the RNN does indeed manage to predict the target sequence well. 一旦适当地重新格式化,RNN确实能够很好地预测目标序列。

np.random.seed(1337)

sample_size = 256
x_seed = [1, 0, 0, 0, 0, 0]
y_seed = [1, 0.8, 0.6, 0, 0, 0]

x_train = np.array([[x_seed] * sample_size]).reshape(sample_size,len(x_seed),1)
y_train = np.array([[y_seed]*sample_size]).reshape(sample_size,len(y_seed),1)

model=Sequential()
model.add(SimpleRNN(input_dim  =  1, output_dim = 50, return_sequences = True))
model.add(TimeDistributed(Dense(output_dim = 1, activation  =  "sigmoid")))
model.compile(loss = "mse", optimizer = "rmsprop")
model.fit(x_train, y_train, nb_epoch = 10, batch_size = 32)

print(model.predict(np.array([[[1],[0],[0],[0],[0],[0]]])))
#[[[ 0.87810659]
#[ 0.80646527]
#[ 0.61600274]
#[ 0.01652312]
#[ 0.00930419]
#[ 0.01328572]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM