keras 数据基数不明确请确保所有数组包含相同数量的样本

xurqigkl  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(197)

我试过测试一个模型,但是遇到了一个错误。下面是我的代码

Train1 = train1.astype('float32')         
test1 = test1.astype('float32')
train1 /= 255.0              
test1 /= 255.0

Xtrain = np.concatenate([train1, test1])
X_train, X_test= train_test_split(Xtrain,test_size=0.3)

print(test1)
                     
model = Sequential()

model.add(LSTM(units =100, activation='relu', return_sequences=True, input_shape = (1,X_train.shape[1])))
model.add(Dropout(0.5))
model.add(LSTM(units =50, activation='relu', return_sequences=True))
model.add(Dropout(0.5))
model.add(Dense(units=1, activation='sigmoid')) # Prediction of the next value
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])

history = model.fit(X_train, X_test, epochs=10, batch_size=200, verbose=1)`

我错了:

Data cardinality is ambiguous:
  x sizes: 23608
  y sizes: 10119
Make sure all arrays contain the same number of samples.

X_train = (26,23608)
X_test = (26,10119)

我需要以适当的方式准备我的训练和测试数据集。有什么建议吗?

szqfcxe2

szqfcxe21#

model.fit()将x,y作为前两个参数,同时传递x_train和x_test(大小不同)。
x和y的大小应该相同,因为第i个x引用第i个y。通常train_test_split用作
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
然后可以训练模型:
history = model.fit(X_train, y_train, epochs=10, batch_size=200, verbose=1)
最后,您可以使用evaluate来评估模型性能:
model.evaluate(X_test, y_test)
我建议您阅读有关这些主题的KerasSklearn文档。

相关问题