ValueError: Exception encountered when calling layer "sequential" (type Sequential) #256
-
When fitting the mode for first time in the 01_neural_network_regression_in_tensorflow.ipynb notebook, I get the following error shown below. I have tried to reshape the training data but to no avail. "ValueError: Exception encountered when calling layer "sequential_6" (type Sequential).
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
Hey @Citizen-Dan, This looks like it's an update from TensorFlow 2.7.0 that will break some code. See the TensorFlow 2.7.0 update here: https://github.com/tensorflow/tensorflow/releases/tag/v2.7.0 But the main thing is incorrect input shapes for models, in essence So we have to do it manually by adding an extra dimension. ErrorThe error you might see if passing a scalar to any TensorFlow training function:
TensorFlow versionYou will see this breakage if you're coming from TensorFlow <2.7.0 (all versions prior to 2.7.0). You can check your TensorFlow version here: import tensorflow as tf
print(tf.__version__)
>>> 2.7.0 Google Colab uses TensorFlow 2.7.0+ as of November 2021. Code Beforeimport tensorflow as tf
# Create features (using tensors)
X = tf.constant([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])
# Create labels (using tensors)
y = tf.constant([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])
# Set random seed
tf.random.set_seed(42)
# Create a model using the Sequential API
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
# Compile the model
model.compile(loss=tf.keras.losses.mae, # mae is short for mean absolute error
optimizer=tf.keras.optimizers.SGD(), # SGD is short for stochastic gradient descent
metrics=["mae"])
# Fit the model
model.fit(X, y, epochs=5) # this will break with TensorFlow 2.7.0+ Code After (how to fix it)Notice the change to the input of import tensorflow as tf
# Create features (using tensors)
X = tf.constant([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])
# Create labels (using tensors)
y = tf.constant([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])
# Set random seed
tf.random.set_seed(42)
# Create a model using the Sequential API
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
# Compile the model
model.compile(loss=tf.keras.losses.mae, # mae is short for mean absolute error
optimizer=tf.keras.optimizers.SGD(), # SGD is short for stochastic gradient descent
metrics=["mae"])
# Fit the model
model.fit(tf.expand_dims(X, axis=-1), y, epochs=5) # <- updated line That's the only major change that you should have to do when fitting a model on data that's a scalar (e.g. no second dimension, like |
Beta Was this translation helpful? Give feedback.
-
import tensorflow as tf Set random seedtf.random.set_seed(42) Create` features (using tensors)X = tf.constant([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0]) Create labels (using tensors)y = tf.constant([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0]) Create a model using the Sequential APIm = tf.keras.Sequential([ Compile the modelm.compile(loss=tf.keras.losses.mae, Fit the modelm.fit(tf.expand_dims(X, axis = -1), y, epochs = 5) # updated line |
Beta Was this translation helpful? Give feedback.
-
why to use that tf.expand |
Beta Was this translation helpful? Give feedback.
Hey @Citizen-Dan,
This looks like it's an update from TensorFlow 2.7.0 that will break some code.
See the TensorFlow 2.7.0 update here: https://github.com/tensorflow/tensorflow/releases/tag/v2.7.0
But the main thing is incorrect input shapes for models, in essence
fit()
no longer turns data from(batch_size, )
to(batch_size, 1)
.So we have to do it manually by adding an extra dimension.
Error
The error you might see if passing a scalar to any TensorFlow training function: