您应该将数据作为(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>