I have two simple NumPy arrays features and labels:
features = np.array([
[6.4, 2.8, 5.6, 2.2],
[5.0, 2.3, 3.3, 1.0],
[4.9, 2.5, 4.5, 1.7],
[4.9, 3.1, 1.5, 0.1],
[5.7, 3.8, 1.7, 0.3],
])
labels = np.array([2, 1, 2, 0, 0])
I convert these two NumPy arrays to TensorFlow Dataset like this:
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
I define and compile a model:
model = keras.Sequential([
keras.layers.Dense(5, activation=tf.nn.relu, input_shape=(4,)),
keras.layers.Dense(3, activation=tf.nn.softmax)
])
model.compile(
optimizer=keras.optimizers.Adam(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Now I tried train a model using fit() method:
model.fit(dataset, epochs=100)
and I get error:
ValueError: Error when checking input: expected dense_input to have shape (4,) but got array with shape (1,)
If I provide NumPy features and labels arrays directly to fit() method then everything is good.
model.fit(features, labels, epochs=100)
Results:
Train on 5 samples
Epoch 1/100
5/5 [==============================] - 0s 84ms/sample - loss: 1.8017 - accuracy: 0.4000
Epoch 2/100
5/5 [==============================] - 0s 0s/sample - loss: 1.7910 - accuracy: 0.4000
...............................
Epoch 100/100
5/5 [==============================] - 0s 0s/sample - loss: 1.2484 - accuracy: 0.2000
If I understand correct I need to create TensorFlow Dataset which will return tuple (features, labels). So How to convert NumPy features and labels arrays to TensorFlow Dataset which can be used for model.fit()?
Just set a batch size when you create Dataset:
batch_size = 2
dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(batch_size)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With