Home > Mobile >  How to deal with Tensorflow model.predict() value error?
How to deal with Tensorflow model.predict() value error?

Time:11-02

I am getting the following error in my code

WARNING:tensorflow:Model was constructed with shape (None, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 3), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None,).

and here is my code

import numpy as np
import tensorflow as tf
inum = np.array([[1,1,2],[2,25,6],[32,4,7],[8,9,0]], dtype="float")
onum = np.array([3,56,135,72],dtype="float")
l0 = tf.keras.layers.Dense(units=4, input_shape=(3,))
l1 = tf.keras.layers.Dense(units=4)
l2 = tf.keras.layers.Dense(units=4)
l3 = tf.keras.layers.Dense(units=1)
model = tf.keras.Sequential([l0,l1,l2,l3])
model.compile(loss="mean_squared_error",optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(inum,onum,epochs=1200,verbose=False)
model.predict([2,2,4])

I am very new to Machine Learning and have now idea what to do with this.

Any help is greatly appreciated.

CodePudding user response:

Use

Model.predict([[2,2,4]])

Because keras model treat inputs as a batch of data. So even if your just want to input one data has shape [3], you should wrap it as a [1,3] data like I do.

  • Related