Reputation: 840
I'm learning Tensorflow and i tried out this program This program is from this Youtube Video at 4:40:00
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x = np.array([float(i) for i in range(-7, 15, 3)])
y = x + 10
plt.scatter(x, y)
plt.show()
x = tf.constant(x)
y = tf.constant(y)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
model.compile(loss = tf.keras.losses.mae,
optimizer = tf.keras.optimizers.SGD(),
metrics = ["mae"])
model.fit(x, y, epochs = 5)
But I get this error:
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-146-19ee806b894a> in <module>()
6 optimizer = tf.keras.optimizers.SGD(),
7 metrics = ["mae"])
----> 8 model.fit(x, y, epochs = 5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_44" (type Sequential).
Input 0 of layer "dense_39" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
Call arguments received:
• inputs=tf.Tensor(shape=(None,), dtype=float64)
• training=True
• mask=None
What mistake have i done and what should i do to correct it? If I have to make some changes then how did it work in the video?
Upvotes: 1
Views: 1692
Reputation: 11
Thanks for posting this answer.
After applying your solution - I also learned that order is critical:
this order ERRORS out
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.Input(shape=(1,)))
Input shape has to come before Dense
model = tf.keras.Sequential()
model.add(tf.keras.Input(shape=(1,)))
model.add(tf.keras.layers.Dense(1))
-- The answer model.fit(x[..., None], y, epochs = 100)
fixes the problem too
Upvotes: 1
Reputation: 1162
You need to add an input layer into your sequential model as follows:
model = tf.keras.Sequential([tf.keras.Input(shape=(1,)), tf.keras.layers.Dense(1) ])
.
The input layer specifies that indeed you are expecting scalar inputs.
Upvotes: 2
Reputation: 17219
You may need to add a new axis to x
to be compatible with the dense layer. Try the following code.
model.compile(loss = tf.keras.losses.mae,
optimizer = tf.keras.optimizers.SGD(),
metrics = ["mae"])
model.fit(x[..., None], y, epochs = 100)
# OK
Note, in the tutorials, the instructor also mentioned it in his Github repo.
Upvotes: 1