【发布时间】:2021-08-24 12:47:53
【问题描述】:
我正在做一个简单的 conv1D 模型。在执行 model.fit 时,我收到此错误:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (10, 5)
我使用的基本示例来自这个 SO 答案 - How does it works the input_shape variable in Conv1d in Keras?
from keras.models import Sequential
from keras.layers import Dense, Conv1D
import numpy as np
N_FEATURES=5
N_TIMESTEPS=10
X = np.random.rand(100, N_FEATURES)
Y = np.random.randint(0,2, size=100)
# Create a Sequential model
model = Sequential()
# Change the input shape to input_shape=(N_TIMESTEPS, N_FEATURES)
model.add(Conv1D(filters=32, kernel_size=N_TIMESTEPS, activation='relu', input_shape=(N_TIMESTEPS, N_FEATURES)))
# If it is a binary classification then you want 1 neuron - Dense(1, activation='sigmoid')
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
我使用的 model.fit 是:
model.fit(X, Y, epochs=150, batch_size=10)
我确实认为数据已为模型正确准备,但为什么在这个简单的示例中会出现此错误?我在这里摸不着头脑,因为我已经看到很多类似的 ValueError 并且我无法解决它(根据我收集的信息,这似乎是由于不正确的数据输入)。
对此的任何建议将不胜感激。提前致谢。
【问题讨论】:
-
X 必须是 3D(n_samples、n_timesteps、n_features)
-
对不起我比较新,这是否意味着在这种情况下我需要重塑
X?你有一个例子吗?非常感谢! -
不,数据没有准备好,你的X数据中的N_TIMESTEPS在哪里?
-
知道了,我在这里看到了我的问题!谢谢。 :)
标签: python tensorflow keras conv-neural-network