I'm very new to machine learning and Keras. I'm attempting predictive values on a time series with 9 input features and 1 output feature.
I have input data of shape:(5787, 9) --> 5787 data points, 9 features per sample
I have target data of shape:(5787,) --> 5787 target data points
My model looks like this and compiles just fine:
# Build Model
model = Sequential()
model.add(layers.Dense(units=(features * neuron_per_feature),
# input_shape=input_data.shape,
activation = 'relu',
name='dense1',
))
# First GRU Layer
# input shape should be form (batch, window_size, features)
model.add(layers.GRU(features * neuron_per_feature,
# input_shape=(5787, features * neuron_per_feature),
dropout=dropout,
return_sequences=True,
name='gru1',
))
model.add(layers.Dense(features * (neuron_per_feature / 2),
activation = 'relu',
name='dense2',
))
model.add(layers.GRU(features * 4,
# input_shape=(window_size * features, ),
dropout=dropout,
return_sequences=False,
name='gru2',
))
model.add(layers.Dense(1,
name='dense3',
))
# Configure Adam optimizer
opt = keras.optimizers.Adam(
learning_rate=learning_rate,
beta_1=0.9,
beta_2=0.98,
epsilon=1e-9)
# Compile Model
model.compile(optimizer=opt, loss='mse') # mse = mean squared error
# model.summary()
I try to train the model like this:
`# Fit network
history = model.fit(
x=input_data,
y=target_data,
validation_split=0.0,
batch_size=batch_size,
epochs=epochs,
verbose="auto",
# validation_data=(test_X, test_Y),
shuffle=False,
workers=2,
use_multiprocessing=True,
)`
However, I continually get errors when adding in the GRU layers:
`<ipython-input-62-76971a07670d> in <module>
1 # Fit network
----> 2 history = model.fit(
3 x=input_data,
4 y=target_data,
5 validation_split=0.0,
1 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 214, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "sequential_19" (type Sequential).
Input 0 of layer "gru1" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 108)
Call arguments received by layer "sequential_19" (type Sequential):
• inputs=tf.Tensor(shape=(None, 9), dtype=float32)
• training=True
• mask=None`
Any help would be much appreciated!
I've tried disabling the GRU layers, and the model will run the fit training. But when I re-add the GRUs, it fails.
Related
I'm trying to train a CNN on my own images. I've set class_mode to sparse, so I figured I would use SparseCategoricalCrossentropy. However, when I do this (or even CategoricalCrossentropy), I get this error about the elements not being of a supported type.
I've tried a few different loss functions with the same error persisting. I think there must be something wrong with the formatting of my data. I've attached my data_gen and NN code along with the full traceback.
train_datagen = ImageDataGenerator(
rescale=1./255,
width_shift_range=.05,
height_shift_range=.05,
zoom_range=0.05,
validation_split=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode="constant",
cval=0)
test_datagen = ImageDataGenerator(
rescale=1./255)
#data augmentation
train_ds = train_datagen.flow_from_directory(
train_data_dir,
shuffle=True,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='sparse',
subset='training');
cnn = Sequential()
cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(img_width,img_height,3)))
cnn.add(BatchNormalization())
cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())
cnn.add(MaxPooling2D(pool_size=(2, 2)))
cnn.add(Dropout(0.2))
cnn.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())
cnn.add(Flatten())
cnn.add(Dense(64, activation='relu'))
cnn.add(Dense(22, activation='softmax'))
cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
optimizer="Adam",
metrics=['accuracy'])
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [24], line 111
106 cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
107 optimizer="Adam",
108 metrics=['accuracy'])
110 epochs=20
--> 111 history = cnn.fit(
112 train_ds,
113 validation_data=valid_ds,
114 steps_per_epoch=math.ceil(len(train_ds) / batch_size),
115 epochs=epochs)
117 plt.plot(history.history['loss'], label='train')
118 plt.plot(history.history['val_loss'], label='validation')
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~\AppData\Local\Temp\__autograph_generated_fileswqeb6ya.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
TypeError: in user code:
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1160, in train_function *
return step_function(self, iterator)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 158, in __call__
return losses_utils.compute_weighted_loss(
File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\losses_utils.py", line 328, in compute_weighted_loss
losses = tf.convert_to_tensor(losses)
TypeError: Failed to convert elements of <keras.losses.SparseCategoricalCrossentropy object at 0x0000017523557AC0> to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.
I'm trying to compile and fit a model but, this error is occurring:
ValueError: Shapes (None, 10, 10, 10) and (None, 10) are incompatible
code:
from tensorflow.keras.layers import Conv2D
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=3, activation="relu", input_shape=(28, 28, 1)))
model.add(Conv2D(filters=32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dense(10, activation="softmax"))
# Compile the model
model.compile(loss="categorical_crossentropy", metrics=["accuracy"], optimizer="adam")
# Fit the model
model.fit( x=X_train, y=y_train, batch_size=32, epochs=10, validation_split = 0.3)
Output:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-a118e2826a10> in <module>()
3
4 # Fit the model
----> 5 model.fit( x=X_train, y=y_train, batch_size=32, epochs=10, validation_split = 0.3)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1790, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5083, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 10, 10, 10) and (None, 10) are incompatible
I'm using tensorflow 2.8.2
How can i put it to work ?
What am i doing wrong ?
edit :
the problem was that I ran this part of the program twice (dumb mistake):
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
print("Shape of y_train:", y_train.shape)
print("One value of y_train:", y_train[0])
I restarted the environment, and the problem was fixed.
I get the next error when im trying to fine tuning a bert model to predict sentiment analysis.
Im using as input:
X-A list of strings that contains tweets
y-a numeric list (0 - negative, 1 - positive)
I am trying to fine tuning a bert model to predict sentiment analysis but i always get the same error in logits and labels when im trying to fit the model. I load a pretrained model and then build the dataset but when i am trying to fit it, it is impossible.
The text used as input is a list of strings made of tweets and the labels used as input are a list of categories (negative and positive) but transformed to 0 and 1.
from sklearn.preprocessing import MultiLabelBinarizer
#LOAD MODEL
hugging_face_model = 'distilbert-base-uncased-finetuned-sst-2-english'
batches = 32
epochs = 1
tokenizer = BertTokenizer.from_pretrained(hugging_face_model)
model = TFBertModel.from_pretrained(hugging_face_model, num_labels=2)
#PREPARE THE DATASET
#create a list of strings (tweets)
lst = list(X_train_lower['lower_text'].values)
encoded_input = tokenizer(lst, truncation=True, padding=True, return_tensors='tf')
y_train['sentimentNumber'] = y_train['sentiment'].replace({'negative': 0, 'positive': 1})
label_list = list(y_train['sentimentNumber'].values)
#CREATE DATASET
train_dataset = tf.data.Dataset.from_tensor_slices((dict(encoded_input), label_list))
#COMPILE AND FIT THE MODEL
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5), loss=BinaryCrossentropy(from_logits=True),metrics=["accuracy"])
model.fit(train_dataset.shuffle(len(df)).batch(batches),epochs=epochs,batch_size=batches) ```
ValueError Traceback (most recent call last)
<ipython-input-158-e5b63f982311> in <module>()
----> 1 model.fit(train_dataset.shuffle(len(df)).batch(batches),epochs=epochs,batch_size=batches)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1000, in train_step
loss = self.compiled_loss(y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 512, 768) vs (None, 1)).
As described in this kaggle notebook, you must build a custom Keras Model around the pre-trained BERT model to perform classification,
The bare Bert Model transformer outputing raw hidden-states without
any specific head on top
Here is a copy of a piece of code:
def create_model(bert_model):
input_ids = tf.keras.Input(shape=(60,),dtype='int32')
attention_masks = tf.keras.Input(shape=(60,),dtype='int32')
output = bert_model([input_ids,attention_masks])
output = output[1]
output = tf.keras.layers.Dense(32,activation='relu')(output)
output = tf.keras.layers.Dropout(0.2)(output)
output = tf.keras.layers.Dense(1,activation='sigmoid')(output)
model = tf.keras.models.Model(inputs = [input_ids,attention_masks],outputs = output)
model.compile(Adam(lr=6e-6), loss='binary_crossentropy', metrics=['accuracy'])
return model
Note: you might have to adapt this code and in particular modify the Input shape (60 to 512 seemingly from the error message, your tokenizer maximum length)
Load BERT model and build the classifier :
from transformers import TFBertModel
bert_model = TFBertModel.from_pretrained(hugging_face_model)
model = create_model(bert_model)
model.summary()
Summary:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 60)] 0 []
input_2 (InputLayer) [(None, 60)] 0 []
tf_bert_model_1 (TFBertModel) TFBaseModelOutputWi 109482240 ['input_1[0][0]',
thPoolingAndCrossAt 'input_2[0][0]']
tentions(last_hidde
n_state=(None, 60,
768),
pooler_output=(Non
e, 768),
past_key_values=No
ne, hidden_states=N
one, attentions=Non
e, cross_attentions
=None)
dense (Dense) (None, 32) 24608 ['tf_bert_model_1[0][1]']
dropout_74 (Dropout) (None, 32) 0 ['dense[0][0]']
dense_1 (Dense) (None, 1) 33 ['dropout_74[0][0]']
==================================================================================================
Total params: 109,506,881
Trainable params: 109,506,881
Non-trainable params: 0
I don't know why this error keep coming when I run the code below
CNN.fit(X_train_vector, y_train, epochs=10)
My CNN code is this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (21367, 9000)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
My "X_train_vector" has a shape:
(21367, 9000)
My "y_train" has a shape:
(21367, 1)
The Error I am getting:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-108-895976bf38cd> in <module>()
----> 1 CNN.fit(X_train_vector, y_train, epochs=10)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shape=(None, 21367, 9000), found shape=(None, 9000)
I have tried several solutions, including changing my first line of CNN to this:
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
But running it says:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-109-dd8d734d0a9f> in <module>()
1 CNN = tf.keras.models.Sequential()
----> 2 CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
3 CNN.add(tf.keras.layers.MaxPooling1D(2))
4 CNN.add(tf.keras.layers.Dropout(0.2))
5 CNN.add(tf.keras.layers.Flatten())
3 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
441 else:
442 batch_size = None
--> 443 batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
444 self._batch_input_shape = batch_input_shape
445
TypeError: 'int' object is not iterable
Can anyone help me. I have been looking for the solution for two days, it should work the way I am trying. Is there a mistake I am making? Please let me know.
Thanks In Advance.
The input array should have the shape (None, shape_0, shape_1), where None represent the batch size, and (shape_0, shape_1) represents the shape of the feature. So, you should reshape your input array:
X_train_vector = X_train_vector.reshape(-1, 9000, 1)
And you don't really need to specify the batch size when building the model, so remove that and just use (9000, 1) as the input_shape. Try this:
CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000, 1)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())
CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))
And this should solve the problem the same error would not appear again.
I am creating a model to detect skin cancer using the ISIC skin cancer dataset. I have created a CNN model but after compiling it's throwing a shape error.
My-Code-
scale = 1./255
num_classes = 9
model = Sequential()
model.add(layers.experimental.preprocessing.Rescaling(scale, offset =0.0))
model.add(layers.Conv2D(32, (3, 3), padding = 'same', input_shape=(180, 180, 3)))
model.add(layers.Activation('relu'))
model.add(layers.Conv2D(64, (3, 3), padding='same'))
model.add(layers.Activation('relu'))
model.add(layers.Conv2D(128, (3, 3)))
model.add(layers.Activation('relu'))
model.add(layers.Flatten())
model.add(layers.Dense(20))
model.add(layers.Activation('relu'))
model.add(layers.Dense(num_classes))
model.add(layers.Activation('softmax'))
model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
epochs = 2
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
My training data-
<PrefetchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>
My Val data -
<PrefetchDataset shapes: ((None, 180, 180, 3), (None,)), types: (tf.float32, tf.int32)>
Now after fitting the model I am getting the error - ValueError: Shapes (None, 1) and (None, 9) are incompatible
Epoch 1/2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-c695c39c566b> in <module>()
3 train_ds,
4 validation_data=val_ds,
----> 5 epochs=epochs
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:797 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:155 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:259 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1644 categorical_crossentropy
y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4862 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 1) and (None, 9) are incompatible
My Notebook
Data
You set label_mode='int', that's why you should use sparse_categorical_crossentropy as a loss function but you use loss function categorical_crossentropy which use generally when your target is one-hot encoded.
From tf.keras.preprocessing.image_dataset_from_directory, the label_mode should be as follows
- 'int': means that the labels are encoded as integers (e.g. for
sparse_categorical_crossentropy loss).
- 'categorical' means that the labels are encoded as a categorical
vector (e.g. for categorical_crossentropy loss).
- 'binary' means that the labels (there can be only 2) are encoded as
float32 scalars with values 0 or 1 (e.g. for binary_crossentropy).
- None (no labels).
That's why in your case, changing the loss function from categorical_crossentropy to sparse_categorical_crossentropy should solve the issue.