为什么我收到此错误“图层“dense_30”的输入 0 与图层不兼容:预期 min_ndim=2,发现 ndim=1。收到完整形状:(无,)”

原文标题why I’m getting this error “Input 0 of layer “dense_30″ is incompatible with the layer:expected min_ndim=2,found ndim=1. Full shape received: (None,)”

设置随机种子

print(np.__version__) tf.random.set_seed(42)
  1. 使用 Sequential Api model = tf.keras.Sequential([tf.keras.layers.Dense(1)]) 创建模型
  2. 编译模型 model.compile(loss=tf.keras.losses.mae,# mae 是 mean absolute error optimizer = tf.keras.optimizers.SGD(), # sgd 是 stochastic gradient descent metrics=[“mae 的缩写”]
  3. 拟合模型 model.fit(X, y, epochs=5) // 这里是错误

原文链接:https://stackoverflow.com//questions/71671437/why-im-getting-this-error-input-0-of-layer-dense-30-is-incompatible-with-the

回复

我来回复
  • Kudo的头像
    Kudo 评论

    您需要添加一个输入层,例如:

    model = tf.keras.Sequential([  tf.keras.layers.InputLayer(input_shape=(1,)),  tf.keras.layers.Dense(1) ])
    
    2年前 0条评论