【问题标题】:ValueError at model.fit in Keras conv1D (expected min_ndim=3, found ndim=2)Keras conv1D 中 model.fit 的 ValueError(预期 min_ndim=3,发现 ndim=2)
【发布时间】: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


【解决方案1】:

工作示例代码

import tensorflow as tf
N_FEATURES=5
N_TIMESTEPS=10
Batch_size =10
input_shape = (Batch_size, N_TIMESTEPS , N_FEATURES)
x = tf.random.normal(input_shape)
model = tf.keras.layers.Conv1D(
32, 3, activation='relu',input_shape=input_shape[1:])(x)
print(model.shape)

输出

(10, 8, 32)

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2022-07-13
    • 2021-04-05
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多