hi我试图在tensorflow中建立尽可能简单的回归模型,但出现了这个错误。tensorflow版本:2.7.0
import tensorflow as tf
X_train = tf.cast(tf.constant([1,2,3]), dtype=tf.float32)
y_train = tf.cast(tf.constant([2,3,4]), dtype=tf.float32)
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile()
model.fit(X_train, y_train, epochs=10)
ValueError:调用层"时遇到异常;sequencial_7";(类型Sequential(。"层"的输入0;dense_5";与层不兼容:应为min_ndim=2,实际为ndim=1。收到完整形状:(无,(
您没有指定输入形状、损失函数和优化器。
import tensorflow as tf
X_train = tf.cast(tf.constant([1, 2, 3]), dtype=tf.float32)
y_train = tf.cast(tf.constant([2, 3, 4]), dtype=tf.float32)
model = tf.keras.Sequential([
tf.keras.Input(shape=(1, )),
tf.keras.layers.Dense(1)
])
model.compile(optimizer="Adam", loss="binary_crossentropy")
model.fit(X_train, y_train, epochs=10)