Home > Net >  tensorflow keras.sequential function error
tensorflow keras.sequential function error

Time:04-09

hi i tried to bulid the simplest possible regression model in tensorflow but this error appearanced. tensorflow ver: 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: Exception encountered when calling layer "sequential_7" (type Sequential). Input 0 of layer "dense_5" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)

CodePudding user response:

You didn't specify an input shape, a loss function, and an optimizer.

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)
  • Related