machine-learning - Keras model.predict() 抛出 ValueError
问题描述
X 和 Y 的形状分别为 (89362, 5) 和 (89362,)。
x_train, x_test, y_train, y_test = train_test_split(X, Y,
test_size = 0.3,
random_state = 1)
x_train.shape, y_train.shape = ((62553, 5), (62553,))
x_test.shape, y_test.shape = ((26809, 5), (26809,))
将向量重新整形为:
torch.Size([1, 62553, 5]), torch.Size([1, 62553])
torch.Size([1, 26809, 5]), torch.Size([1, 26809])
模型定义为
n_steps = 62553
n_features = 5
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(62553))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=10, verbose=0)
使用 x_test 进行预测时,会引发值错误
yhat = model.predict(x_test, verbose=0)
print(yhat)
ValueError: Error when checking input: expected conv1d_4_input to have shape (62553, 5) but got array with shape torch.Size([26809, 5])
解决方案
发生这种情况是因为您在此处指定了固定大小:
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps, n_features)))
一旦您将其他内容传递给模型,模型仍然期望具有尺寸的固定大小:
n_steps = 62553
n_features = 5
删除 input_shape 参数应该可以解决这个问题:
model.add(Conv1D(filters=64, kernel_size=2, activation='relu'))
我希望这对你有帮助。
推荐阅读
- haskell - 如何约束类实例
- java - 代码未按预期工作。在我的 Selenium Webdriver Java 代码中使用 ArrayList 和 for 循环
- airflow - 气流:在环境变量中存储连接,用于数据块连接
- c# - 如何使用 lambda 表达式加入使用 groupby 的列表中的列表
- cookies - 如何在任何 word press 网站上自动阻止自定义 cookie。我已使用代码阻止单个 cookie 但未成功
- primefaces - 从输入文本字段和命令按钮获取空值未在primefaces中调用操作方法
- javascript - JavaScript - 从 HTML 文本区域中提供的用户数据创建链接
- r - 如何在 Plotly 散点图中为两个不同的变量着色?
- python - AttributeError:模块“龙卷风”没有属性“curl_httpclient”
- spring-batch - 反序列化包含 QName 的 unclosedHeaderCallbackElements