I am having a value error in my keras model, calling 'model.fit' gives a value error. i keep getting value error error when i try to fit the model

Question:

X=np.array([-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0])
y=np.array([3.0,6.0,9.0,12.0,15.0,18.0,21.0,24.0])
X=tf.cast(tf.constant(X), dtype=tf.float32)
y=tf.cast(tf.constant(y), dtype=tf.float32)


tf.random.set_seed(42)

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)

`—————————————————————————
ValueError Traceback (most recent call last)
in
10 metrics=["mae"])
11 # 3. Fit the model
—> 12 model.fit(X, y, epochs=5)

1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True

i keep getting this error when i try to fit the model. i am following a youtube video, and i did the exact thing the tutor did , whilst he didnt get an error, i am getting one. how do i resolve this

Asked By: Victor Ebere

||

Answers:

Tensorflow always expects a batch size from you. change your input shapes like this, hopefully this will work

import numpy as np
import tensorflow as tf

X=np.array([-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0]).reshape(-1,1)
y=np.array([3.0,6.0,9.0,12.0,15.0,18.0,21.0,24.0]).reshape(-1,1)

X=tf.cast(tf.constant(X), dtype=tf.float32)
y=tf.cast(tf.constant(y), dtype=tf.float32)


tf.random.set_seed(42)

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, batch_size=1, epochs=5)

For model.predict() use model.predict([[17.0]])

Answered By: Mohammad Ahmed