francesco
francesco

Reputation: 41

ValueError: When using data tensors as input to a model

I'm trying to train a model with GradientTape in Keras. Here is the code:

@tf.function
def train_step(x,y):
    
    with tf.GradientTape() as tape:
                
        predictions = model.predict(x)
        
        loss = compute_loss(y, predections)
    
    grads = tape.gradient(loss, model.trainable_variables)
        
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    
    return loss

history = []

for iter in tqdm(range(num_iters)):
    
    x_batch, y_batch = get_batch(x_train, y_train, batch_dim)
    loss = train_step(x_batch, y_batch)
    history.append(loss.numpy().mean())
    

This code leads to the following error:

ValueError: When using data tensors as input to a model, you should specify the `steps` argument.

However if I try to call the prediction outside the function as follows:

history = []

for iter in tqdm(range(num_iters)):
    
    x_batch, y_batch = get_batch(x_train, y_train, batch_dim)       
    x_hat = model.predict(x_batch)

I get no error...

Can someone explain me why do I get this behavior from Keras?

Upvotes: 1

Views: 1939

Answers (1)

user11530462
user11530462

Reputation:

Answering here even though the user has answered in the comment for the benefit of the community.

By changing the data type of x_batch and y_batch to float32 and then called model(x_batch) for predicting the output.
In this way, the issue will be resolved and by maintaining the train_step function as @tf.function.

Upvotes: 1

Related Questions