0

I am trying to use an array as an input for a tensorflow model The training input is xs The training output is ys The input to test is zs When i train the model everything works fine but if i use predict this error appears:

ValueError                                Traceback (most recent call last)
<ipython-input-1-08c8b5d5ab9e> in <module>()
     26 model.fit(xs,ys, epochs=500)
     27 
---> 28 print(model.predict(zs))
     29 #print(str(model.get_weights()))
     30 #np.array([np.array(x) for x in xs])

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1462 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1452 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1445 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1418 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
        ' but received input with shape ' + str(shape))

    ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape [None, 1]

This is my code:

import numpy as np
from tensorflow import keras

model = tf.keras.Sequential([keras.layers.Dense(units=3, input_shape=[3])])
model.add(keras.layers.Dense(units=20))
model.add(keras.layers.Dense(units=40))
model.add(keras.layers.Dense(units=100))
model.add(keras.layers.Dense(units=20))
model.add(keras.layers.Dense(units=1))

#model.set_weights()
model.compile(optimizer='sgd', loss='mean_squared_error')

xs = tf.constant([[15,0,0],
               [17,0,0],
               [19,1,0],
               [21,1,20],
               [23,1,20],
               [1,1,0],
               [3,1,0],
               [5,1,0]])
ys = tf.constant([0,0,0,0,0,1,1,1])
zs = tf.constant([15,0,0])
print(model.output_shape)
model.fit(xs,ys, epochs=500)

print(model.predict(zs))

How can I use my model correcly? I heard about evealuate but this just returns the loss.

0

2 Answers 2

1

A Keras input expects a batch dimension. Your zs variable does not have it.

You can use zs = tf.expand_dims(zs,axis=0) to add the batch dimension, or use a nested list when creating zs.

zs = tf.constant([[15,0,0]])

You can check the shape expected by the model by looking at its inputs:

>>> model.input.shape
TensorShape([None, 3])

Here None, means that the first dimension can be any number > 0.

If you check your variable zs, you will see that the shape does not match.

>>> zs = tf.constant([15,0,0])
>>> zs.shape
TensorShape([3])

But if we use tf.expand_dims, or use a nested list when constructing zs, the shape will be compatible.

>>> zs_exp = tf.expand_dims(zs,axis=0)
>>> zs_exp.shape
TensorShape([1, 3])
>>> zs_nested_list = tf.constant([[15,0,0]])
>>> zs_nested_list.shape
TensorShape([1, 3])
Sign up to request clarification or add additional context in comments.

Comments

1

Change your input to something like this

zs = tf.constant([[15,0,0]])

The shape of prediction input should be similar to training input

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.