0

我是初学者,RNNsGRU为股票预测建立一个运行模型门控循环单元。

我有一个用于这种形状的训练数据的 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)

My is (1122,) 并用和train_y.shape表示二进制变量。是买是卖。1010

考虑到这一点,我开始尝试我的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)

显然我输入模型的尺寸不正确,但我不明白它们应该如何适应,所以模型可以顺利运行。

4

1 回答 1

1

因此,如果您有 1122 个数据样本,每个样本有 20 个时间步长,每个时间步长有 320 个特征,并且您想教您的模型在买卖之间做出二元决策,请尝试以下操作:

import tensorflow as tf
tf.random.set_seed(1)

model = tf.keras.Sequential()
model.add(tf.keras.layers.GRU(50, return_sequences=True, input_shape=(20, 320), activation='tanh'))
model.add(tf.keras.layers.GRU(50,activation='tanh'))
model.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01,decay=1e-7,momentum=0.9,nesterov=False),loss='binary_crossentropy')
print(model.summary())

train_x = tf.random.normal((1122, 20, 320))
train_y = tf.random.uniform((1122,), maxval=2, dtype=tf.int32)
model.fit(train_x, train_y, epochs=5, batch_size=16)
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 gru (GRU)                   (None, 20, 50)            55800     
                                                                 
 gru_1 (GRU)                 (None, 50)                15300     
                                                                 
 dense (Dense)               (None, 1)                 51        
                                                                 
=================================================================
Total params: 71,151
Trainable params: 71,151
Non-trainable params: 0
_________________________________________________________________
None
Epoch 1/5
71/71 [==============================] - 5s 21ms/step - loss: 0.7050
Epoch 2/5
71/71 [==============================] - 2s 22ms/step - loss: 0.6473
Epoch 3/5
71/71 [==============================] - 1s 21ms/step - loss: 0.5513
Epoch 4/5
71/71 [==============================] - 1s 21ms/step - loss: 0.3640
Epoch 5/5
71/71 [==============================] - 1s 20ms/step - loss: 0.1258
<keras.callbacks.History at 0x7f4eac87e610>

请注意,您有一个输出节点,因为您的模型应该做出二元决策。这也是你必须使用损失函数的原因binary_crossentropy

关于 GRU 层,它需要一个形状为 的输入(batch_size, timesteps, features),但 batch_size 是在训练期间推断出来的,因此在input_shape. 由于下一个 GRU 也需要此形状,因此您使用第一个 GRU 中的参数,该参数为每个输入时间步return_sequences=True返回一个形状(batch_size, 20, 50)=> 一个隐藏状态输出的序列。此外,您的情况也不需要图层。它通常用于将表示文本的整数序列映射为一维向量表示。50nEmbeddingn

于 2021-11-17T09:20:13.253 回答