Reputation: 318
I have written a simple tf.keras.models.Sequential model. When I try to fit it with data and labels as tf.Tensor, it gives me some error. However I can fit it with numpy array with exactly the same underlying data. Why is it?
I am using tensorflow 1.13 with only CPU. I checked the fit function of tf.keras.models.Sequential but it says both tf.Tensor and numpy array can be used as data and label as long as their types match.
import tensorflow as tf
tf.enable_eager_execution()
# very simple keras Sequential model
model = tf.keras.Sequential([
tf.keras.layers.Dense(3, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')])
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
# use tf.Tensor as data and label
data = tf.constant([[0,0,1],[0,1,0],[1,0,0]])
label = tf.constant([[0,0,1],[0,1,0],[1,0,0]])
# This throws the following error
# InvalidArgumentError: Index out of range using input dim 2; input has only 2 dims [Op:StridedSlice] name: strided_slice/
model.fit(data, label, epochs=10)
# use numpy array with the same underlying data and label
data = data.numpy()
label = label.numpy()
# This works
model.fit(data, label, epochs=10)
The first fit does not work and throws the following error. But the second works. This is interesting because they have exacly the same underlying data
Upvotes: 3
Views: 1172
Reputation: 14525
Ok, it looks like perhaps you are using tensorflow 2.0 because of the call to .numpy()
which I believe doesn't exist on 1.13 (perhaps you realise already but you can check the version with tf.__version__
)
If you intend to use 1.13 then you need to make 2 changes to allow the call to fit
to execute without error.
steps_per_epoch
argumentThis code, for example, does not throw any errors:
model = tf.keras.Sequential([
tf.keras.layers.Dense(3, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')])
model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
data = tf.constant([[0,0,1],[0,1,0],[1,0,0]], dtype=tf.float32)
label = tf.constant([[0,0,1],[0,1,0],[1,0,0]], dtype=tf.float32)
model.fit(data, label, epochs=10, steps_per_epoch=2)
Upvotes: 1