Reputation: 63
I'm having difficulty training my TensorFlow model using a tf.Dataset
rather than, say, a pd.DataFrame
(which works fine).
I have created a dummy example below that I would expect to work given what I have read online/on the TensorFlow website.
!pip install tensorflow==2.0.0 > /dev/null
import numpy as np
import tensorflow as tf
features, target = np.random.rand(100, 30), np.random.randint(0, 2, 100)
dataset = tf.data.Dataset.from_tensor_slices((features, target))
model = tf.keras.Sequential([
tf.keras.layers.Dense(30, activation='relu', input_shape=(30,)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(30, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.fit(
dataset,
epochs=10,
)
which returns the following error message
...
ValueError: Error when checking input: expected dense_input to have shape (30,) but got array with shape (1,)
Is there anything obviously wrong in the above? Why is TensorFlow grabbing an input with shape (1,)
?
Upvotes: 2
Views: 91
Reputation: 1545
Try to use tf.data.Dataset.from_tensors
instead tf.data.Dataset.from_tensor_slices
The difference explained here: https://stackoverflow.com/a/55370549/10418812
Upvotes: 1