【问题标题】:ValueError: Input 0 of layer "value" is incompatible with the layer: expected min_ndim=2, found ndim=1ValueError: Input 0 of layer \"value\" is in compatible with the layer: expected min_ndim=2, found ndim=1
【发布时间】:2022-11-06 20:16:11
【问题描述】:

我是 tensorflow 的新手。当我尝试修改模型结构时,我使用 tf.reshape(r,[4,]) 将形状为 (None,1) 的张量“r”转换为一维张量,然后将其输入到密集层。一维张量的shape为(4,),dense layer的input_shape设置为(4,),出现错误。 ValueError:图层“值”的输入 0 与图层不兼容:预期 min_ndim=2,发现 ndim=1。收到的完整形状:(4,) 如果您能帮我解决这个问题,我将不胜感激 我的张量流版本是 2.10.0

【问题讨论】:

    标签: tensorflow reshape tensor dimension


    【解决方案1】:

    您应该将数据作为(samples or batch, data shape) 传递。因此,输入数据至少有 2 个维度。

    model = tf.keras.models.Sequential()
    model.add(tf.keras.Input(shape=(4,)))
    model.add(tf.keras.layers.Dense(32, activation='relu'))
    model.add(tf.keras.layers.Dense(32))
    x=tf.random.normal((4,))
    y=tf.random.uniform((4,),0,1)
    model.compile('rmsprop','binary_crossentropy',)
    model.fit(x,y)
    

    上面的代码引发以下值错误

    Input 0 of layer "dense_2" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
    

    将输入形状更改为(批量,数据形状)后,模型工作正常。

    model = tf.keras.models.Sequential()
    model.add(tf.keras.Input(shape=(4,)))
    model.add(tf.keras.layers.Dense(32, activation='relu'))
    model.add(tf.keras.layers.Dense(32))
    # The input data should be of shape (batch_size, data shape)
    x=tf.random.normal((120,4,))
    y=tf.random.uniform((120,),0,1)
    model.compile('rmsprop','binary_crossentropy',)
    model.fit(x,y)
    

    输出:

    4/4 [==============================] - 2s 3ms/step - loss: 4.2611
    <keras.callbacks.History at 0x7fa2e901db90>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2019-07-19
      • 2017-07-28
      • 1970-01-01
      • 2017-10-26
      • 2022-09-24
      • 2018-05-19
      相关资源
      最近更新 更多