In Keras, how to use Model.predict function when looping over a tensorflow Dataset?

Viewed 35

I want to use the Model.predict() function in a loop over the training batches. To divide the train_data into batches I use the Dataset class and the batch function:

@tf.function
def training(modell, train_data, batch_size):
    train_dataset = tf.data.Dataset.from_tensor_slices(train_data)
    train_dataset = train_dataset.shuffle(buffer_size=1024).batch(parameters['batch_size'])
    iterator = tf.compat.v1.data.make_initializable_iterator(train_dataset)
    for step in range(4):
        with tf.GradientTape() as tape:
            modell.predict(iterator.get_next(), steps=1)


training(model, train, 400)

but when running the code, I get this error:

file.py:36 training * modell.predict(iterator.get_next(), steps=1) ValueError: When feeding symbolic tensors to a model, we expect the tensors to have a static batch size. Got tensor with shape: (None, 2, 48, 48, 1)

I have looked into other posts but I could not find any solution.

Thanks

1 Answers

The training loop should obviously not be a TF function, just think about the sort of graph it should have to build...

Ignoring the fact that the graph would be useless since you will call this function only one time... instead, the training loop should be "normal python code", and the train phase (everything inside the train loop) can be a TF function

Also consider that modell.predict will most likely not produce any gradient, instead you should use __call__

Related