I am trying to rewrite the code from C3_W3_Assignment to tensroflow and I would like to know how you achieved, that generated data with different padding length from batch to batch was fitted to model?
I have something like this
model = keras.Sequential([
tf.keras.layers.Embedding(32767, 50),
tf.keras.layers.LSTM(50),
tf.keras.layers.Dense(10),
tf.keras.layers.Softmax()
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(tf.data.Dataset.zip(test_sentences_vec, test_tags_vec).padded_batch(10), epochs=3)
Outputs
Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
logits and labels must have the same first dimension, got logits shape [10,10] and labels shape [20]
[[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_896421]
And this is what in data
>>> list(tf.data.Dataset.zip(test_sentences_vec, test_tags_vec).padded_batch(10).as_numpy_iterator())
[(array([[ 1, 0],
[2151, 0],
[ 1, 0],
[ 1, 0],
[ 1, 0],
[ 1, 0],
[2652, 1],
[ 1, 0],
[ 1, 0],
[ 1, 0]]),
array([[ 1128, 0],
[ 1, 0],
[ 47, 0],
[ 36, 0],
[ 52, 0],
[ 52, 0],
[ 32, 32],
[ 45, 0],
[ 45, 0],
[10206, 0]])),
(array([[ 1, 0, 0, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0],
[ 1, 16688, 3431, 1032, 2201, 1, 1115, 1],
[11009, 1, 0, 0, 0, 0, 0, 0],
[ 1, 1059, 0, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0],
[18680, 0, 0, 0, 0, 0, 0, 0],
[ 2084, 0, 0, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0]]),
array([[ 52, 0, 0, 0, 0, 0, 0, 0],
[ 52, 0, 0, 0, 0, 0, 0, 0],
[ 32, 32, 32, 32, 32, 32, 32, 32],
[ 45, 45, 0, 0, 0, 0, 0, 0],
[ 45, 45, 0, 0, 0, 0, 0, 0],
[10206, 0, 0, 0, 0, 0, 0, 0],
[ 1128, 0, 0, 0, 0, 0, 0, 0],
[ 6235, 0, 0, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0, 0, 0],
[ 47, 0, 0, 0, 0, 0, 0, 0]])),
(array([[ 1, 0, 0, 0, 0, 0],
[20047, 0, 0, 0, 0, 0],
[ 2820, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 2331, 4945, 1882, 1, 1536, 1],
[13770, 13773, 24500, 0, 0, 0],
[ 1, 17789, 1012, 1, 0, 0]]),
array([[1128, 0, 0, 0, 0, 0],
[6235, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 47, 0, 0, 0, 0, 0],
[ 36, 0, 0, 0, 0, 0],
[ 52, 0, 0, 0, 0, 0],
[ 52, 0, 0, 0, 0, 0],
[ 32, 32, 32, 32, 32, 32],
[ 45, 45, 45, 0, 0, 0],
[ 45, 45, 45, 45, 0, 0]])),
The main problem is that tf.keras.layers.Dense(10) likes to take just fixed length of element (10 — that is number of all existing tags) in batch. If I do .padded_batch(10, padded_shapes=([10], [len(10])) everything works.
But I checked in your lab and next(train_generator)[0].shape really outputs different shapes from batch to batch. So, how was achieved, that your model is working fine if there are different shapes from batch to batch?