ValueError: 检查目标时出错:预期activation_10有两个维度,但得到的是形状为(118, 50, 1)的数组。

问题描述 投票:1回答:1

我试图使用LSTM来预测股票价格,但我遇到了以下错误。

这是我的代码。

from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import lstm, time

X_train, X_test, Y_train, Y_test = lstm.load_data('tata.csv', 50, True)

#build model
model = Sequential()

mode.add(LSTM(
    input_dim=1,
    output_dim=50,
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    100,
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(
    output_dim=1,))
model.add(Activation('linear'))

start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print("compilation time: ", time.time - start)

#train the model
model.fit(
    X_train,
    Y_train,
    batch_size=512,
    nb_epoch=1,
    validation_split=0.05)

#predicting the prices
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
lstm.plot_results_multiple(predictions, Y_test, 50)

这是我遇到的错误。

ValueError: Error when checking target: expected activation_10 to have 2 dimensions, but got array with shape (118, 50, 1)

这是完整的图像

我无法理解问题出在哪里。请看我链接的图片,以便更好地理解。

python tensorflow machine-learning keras lstm
1个回答
0
投票

当模型输入形状和数据传递形状不同时,就会出现这个错误。

下面是一个例子,我使用了你的代码,但使用了不同的输入文件(因为我无法使用)。import lstm),并对输入进行了一些预处理,用 MinMaxScaler,将数据集分割成 XtrainYtrain 并最终转化为 listnp.array.

我增加了 X_train = X_train[:,:,np.newaxis] 的代码中,以适应 X_train 形状与模型。如果没有这一行,模型将抛出 ValueError: Error when checking input: expected lstm_13_input to have 3 dimensions, but got array with shape (1975, 50).

完整的代码 -

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import time

url = 'https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv'
dataset_train = pd.read_csv(url)
training_set = dataset_train.iloc[:, 1:2].values

dataset_train.head()

from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)

X_train = []
y_train = []
for i in range(60, 2035):
  X_train.append(training_set_scaled[i-50:i, 0])
  y_train.append(training_set_scaled[i, 0])

X_train, Y_train = np.array(X_train), np.array(y_train)
X_train = X_train[:,:,np.newaxis]

#build model
model = Sequential()

model.add(LSTM(
    input_dim=1,
    output_dim=50,
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    100,
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(
    output_dim=1,))
model.add(Activation('linear'))

#model.summary()

start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print("compilation time: ", time.time() - start)

#train the model
model.fit(
    X_train,
    Y_train,
    batch_size=512,
    nb_epoch=1,
    validation_split=0.05)

产出 -

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:34: UserWarning: The `input_dim` and `input_length` arguments in recurrent layers are deprecated. Use `input_shape` instead.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:34: UserWarning: Update your `LSTM` call to the Keras 2 API: `LSTM(return_sequences=True, input_shape=(None, 1), units=50)`
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:43: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1)`
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:58: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
compilation time:  0.021114110946655273
Train on 1876 samples, validate on 99 samples
Epoch 1/1
1876/1876 [==============================] - 3s 1ms/step - loss: 0.0468 - val_loss: 0.0067
<keras.callbacks.callbacks.History at 0x7fa58e0efd30>

希望这能回答你的问题。谢谢你的回答。

© www.soinside.com 2019 - 2024. All rights reserved.