簡體   English   中英

ValueError:檢查輸入時出錯:預期lstm_1_input具有形狀(973,215),但數組的形狀為(61,215)

[英]ValueError: Error when checking input: expected lstm_1_input to have shape (973, 215) but got array with shape (61, 215)

嘗試通過更改許多參數來解決以下問題時,我收到多個不同的ValueErrors

這是一個time series problem ,我有60個商店,215個項目,1034天的數據。 我將973天的火車時間和61天的測試時間分開:

train_x = train_x.reshape((60, 973, 215))
test_x = test_x.reshape((60, 61, 215))
train_y = train_y.reshape((60, 973, 215))
test_y = test_y.reshape((60, 61, 215))

我的模特

model = Sequential()
model.add(LSTM(100, input_shape=(train_x.shape[1], train_x.shape[2]), 
return_sequences='true'))
model.add(Dense(215))
model.compile(loss='mean_squared_error', optimizer='adam', metrics= 
['accuracy'])
history = model.fit(train_x, train_y, epochs=10,
                validation_data=(test_x, test_y), verbose=2, shuffle=False)

ValueError:檢查輸入時出錯:預期lstm_1_input具有形狀(973,215),但數組的形狀為(61,215)

您已按照時間步長(而不是樣本)拆分了數據。 您首先需要確定樣本是什么。 為了回答這個問題,我將假設它們沿着第一個軸(假設數據已被構造為有監督的時間序列問題)。

所述input_size在LSTM期望的形狀(timesteps, data_dim)作為解釋這里 ,這些尺寸必須保持相同每批。 在您的示例中,來自訓練和測試的樣本具有不同的維度。 批處理大小可以不同(除非使用batch_size參數指定)。

您的數據應沿第一個軸在訓練和測試之間划分。 這是Keras教程中的一個類似示例:

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

data_dim = 16
timesteps = 8
num_classes = 10

# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32))  # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# Generate dummy training data
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))

# Generate dummy validation data
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))

model.fit(x_train, y_train,
          batch_size=64, epochs=5,
          validation_data=(x_val, y_val))

您會注意到,對於訓練和測試數據和x_train.shape[1] == x_val.shape[1]timesteps是相同的。 它是沿第一軸不同的樣本數x_train.shape[0]1000x_val.shape[0]100

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM