Keras LSTM模型输出尺寸错误

pkwftd7m  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(131)

我想创建一个LSTM模型,它以股票市场信息为输入,以预测价格为输出。
x_train的形状为(35676,10,10),x_test的形状为(8920,10,10),y_train的形状为(35676,1),y_test的形状为(8920,1)。
当我用x_train和y_train拟合模型时,输出预测的形状为(8920,11),而不是(8920,1)。

\#spliting data
split_lmt = int(len(x)\*0.8)
x_train, x_test = x\[:split_lmt\], x\[split_lmt:\]
y_train, y_test = y\[:split_lmt\], y\[split_lmt:\]

\#lstm model
lstm_input = Input(shape=(10,10), name='LSTM_input')
inputs = LSTM(80, name='firtst_layer')(lstm_input)
inputs = Dense(1, name='dence_layer')(inputs)
output = Activation('linear', name='output')(inputs)
model = Model(inputs=lstm_input, outputs=output)
adam = optimizers.Adam()
model.compile(optimizer=adam, loss='mse')

model.fit(x_train, y_train, batch_size=15, epochs=30, shuffle=False, validation_split=0.1)

y_pred = model.predict(x_test)
pred_copy = np.repeat(y_pred, tf2.shape\[1\], axis=-1)

y_pred = sc.inverse_transform(pred_copy)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
print(y_pred.shape)

\#output
(35676, 10, 10)
(8920, 10, 10)
(35676, 1)
(8920, 1)
(8920, 11)

我不知道是什么引起的,你知道吗?

6g8kf2rb

6g8kf2rb1#

问题是在这里pred_copy = np.repeat(y_pred, tf2.shape\[1\], axis=-1).idk什么是tf2。你应该张贴完成的代码。
在我跑完之后

from keras.layers import Dense, LSTM, Activation, Input
from keras.models import Model
from keras import optimizers
import numpy as np
x = np.random.random((35676+8920,10,10))
y = np.random.random((35676+8920,1))
print(x.shape,y.shape)
#spliting data
split_lmt = int(len(x)*0.8)
x_train, x_test = x[:split_lmt], x[split_lmt:]
y_train, y_test = y[:split_lmt], y[split_lmt:]
print(x_train.shape,y_train.shape,x_test.shape,y_test.shape)
#lstm model
lstm_input = Input(shape=(10,10), name='LSTM_input')
inputs = LSTM(80, name='firtst_layer')(lstm_input)
inputs = Dense(1, name='dence_layer')(inputs)
output = Activation('linear', name='output')(inputs)
model = Model(inputs=lstm_input, outputs=output)
adam = optimizers.Adam()
model.compile(optimizer=adam, loss='mse')
print(model.summary())
model.fit(x_train, y_train, batch_size=150, epochs=1, shuffle=False, validation_split=0.1)
y_pred = model.predict(x_test)
print(y_pred.shape)

y_pred大小是(8920, 1)
下面是上面代码的输出:

(44596, 10, 10) (44596, 1)
(35676, 10, 10) (35676, 1) (8920, 10, 10) (8920, 1)
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 LSTM_input (InputLayer)     [(None, 10, 10)]          0         
                                                                 
 firtst_layer (LSTM)         (None, 80)                29120     
                                                                 
 dence_layer (Dense)         (None, 1)                 81        
                                                                 
 output (Activation)         (None, 1)                 0         
                                                                 
=================================================================
Total params: 29,201
Trainable params: 29,201
Non-trainable params: 0
_________________________________________________________________
None

215/215 [==============================] - 6s 28ms/step - loss: 0.0831 - val_loss: 0.0837
279/279 [==============================] - 1s 4ms/step
(8920, 1)

相关问题