【发布时间】:2021-12-28 06:01:48
【问题描述】:
我是RNNs 的初学者,想为股票预测构建一个运行模型门控循环单元GRU。
我有一个用于这种形状的训练数据的 numpy 数组:
train_x.shape
(1122,20,320)
`1122` represents the total amount timestamps I have `20` is the amount of timestamps I want to predict the future from `320` is the number of features (different stocks)
我的train_y.shape 是 (1122,) 并表示带有 1 和 0 的二进制变量。 1 是买入 0 是卖出。
考虑到这一点,我开始尝试我的GRU 模型:
def GRU_model(train_x,train_y,test_x,test_y):
model = Sequential()
model.add(layers.Embedding(train_x.shape[0],50,input_length=320))
model.add(layers.GRU(50, return_sequences=True,input_shape=(train_x.shape[1],1),activation='tanh'))
model.add(layers.GRU(50, return_sequences=True,input_shape=(train_x.shape[1],1),activation='tanh'))
model.add(layers.GRU(50, return_sequences=True,input_shape=(train_x.shape[1],1),activation='tanh'))
model.add(layers.GRU(50,activation='tanh'))
model.add(Dense(units=2))
model.compile(optimizer=SGD(lr=0.01,decay=1e-7,momentum=0.9,nesterov=False),loss='mean_squared_error')
model.fit(train_x,train_y,epochs=EPOCHS,batch_size=BATCH_SIZE)
GRU_predict = model.predict(validation_x)
return model,GRU_predict
my_gru_model,my_gru_predict = GRU_model(train_x,train_y,validation_x,validation_y)
ValueError: Input 0 of layer gru_42 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 20, 320, 50)
显然我输入模型的尺寸不正确,但我不明白它们应该如何适应,这样模型才能顺利运行。
【问题讨论】:
标签: python tensorflow keras recurrent-neural-network gated-recurrent-unit