Input shape of tf.data.Dataset not accepted by model.fit()

Viewed 1921

I would like to feed with data my model by applying a tf.data.Dataset.

Having checked the documentation of TF 2.0 I found that the .fit() function (https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit) accepts:

x - A tf.data dataset. Should return a tuple of either (inputs, targets) or (inputs, targets, sample_weights).

So, I wrote the following minial proof of concept code:

from sklearn.datasets import make_blobs
import tensorflow as tf
from tensorflow.keras import Model, Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.metrics import Accuracy, AUC

X, Y = make_blobs(n_samples=500, n_features=2, cluster_std=3.0, random_state=1)

def define_model():
    model = Sequential()
    model.add(Dense(units=1, activation="sigmoid", input_shape=(2,)))
    model.compile(optimizer="adam", loss="binary_crossentropy", metrics=[AUC(), Accuracy()])
    return model

model = define_model()

X_ds = tf.data.Dataset.from_tensor_slices(X)
Y_ds = tf.data.Dataset.from_tensor_slices(Y)
dataset = tf.data.Dataset.zip((X_ds, Y_ds))

for elem in dataset.take(1):
    print(type(elem))
    print(elem)

model.fit(x=dataset) #<-- does not work
#model.fit(x=X, y=Y) <-- does work without any problems....

As mentioned in the second comment, the code that does not apply a tf.data.Dataset works fine.

However, when applying the Dataset object I get the following error message:

<class 'tuple'>
(<tf.Tensor: shape=(2,), dtype=float64, numpy=array([-10.42729974,  -0.85439721])>, <tf.Tensor: shape=(), dtype=int64, numpy=1>)
... other output here...
ValueError: Error when checking input: expected dense_19_input to have
shape (2,) but got array with shape (1,)

From my understanding of the documentation, the dataset I have constructed should be exactly the tuple object the fit method expects.

I do not understand this error message.

What am I doing wrong here?

1 Answers

When you pass a dataset to fit, it is expected that it will directly generate batches, not individual examples. You just need to batch your dataset before training.

dataset = dataset.batch(batch_size)
model.fit(x=dataset)
Related