How to solve fit error of ANN using Python Keras - python

So, I was doing my code like the one below. I want to Fit the ANN into the Training set, but the error occurred like this. I was confused about how to solve it, I already try several suggestions on Google, but it still came out an error. I also tried several codes to display the result, but most of the errors occurred is because of the fitting like this one. So, I was thinking that the main problem my code can't run is because of the fitting model.
#Importing necessary Libraries
import numpy as np
import pandas as pd
import tensorflow as tf
import keras
#dataset
data = pd.read_excel("E:\\MATKUL LUV\\THESIS\\DATASETS\\DPM1 1052.xlsx")
#print (data.head)
# Separate Target Variable and Predictor Variables
TargetVariable=['DPM1Fault']
Predictors=['DPM1Cx', 'DPM1Cy']
X=data[Predictors].values
y=data[TargetVariable].values
# Split the data into training and testing set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Quick sanity check with the shapes of Training and testing datasets
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
# importing the libraries
from keras.models import Sequential
from keras.layers import Dense
# create ANN model
model = Sequential()
# Defining the Input layer and FIRST hidden layer, both are same!
model.add(Dense(units=5, input_dim=2, kernel_initializer='normal', activation='relu'))
# Defining the Second layer of the model
# after the first layer we don't have to specify input_dim as keras configure it automatically
model.add(Dense(units=4, kernel_initializer='normal', activation='relu'))
# The output neuron is a single fully connected node
# Since we will be predicting a single number
model.add(Dense(1, kernel_initializer='normal'))
# Compiling the model
model.compile(loss='mean_squared_error', optimizer='adam')
# Fitting the ANN to the Training set
model.fit(X_train, y_train ,batch_size = 20, epochs = 50, verbose=1)
and the result like this
Epoch 1/50
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
Input In [52], in <cell line: 23>()
20 model.compile(loss='mean_squared_error', optimizer='adam')
22 # Fitting the ANN to the Training set
---> 23 model.fit(X_train, y_train ,batch_size = 20, epochs = 50, verbose=1)
File ~\anaconda3\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 ~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py:52, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
50 try:
51 ctx.ensure_initialized()
---> 52 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
53 inputs, attrs, num_outputs)
54 except core._NotOkStatusException as e:
55 if name is not None:
UnimplementedError: Graph execution error:
Detected at node 'mean_squared_error/Cast' defined at (most recent call last):
File "C:\Users\16agn\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\16agn\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Users\16agn\anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\Users\16agn\anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Users\16agn\anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
self._run_once()
File "C:\Users\16agn\anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
handle._run()
File "C:\Users\16agn\anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
await self.process_one()
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
await dispatch(*args)
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
await result
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
reply_content = await reply_content
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 360, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\16agn\anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
result = self._run_cell(
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
return runner(coro)
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3106, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3309, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "C:\Users\16agn\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\16agn\AppData\Local\Temp\ipykernel_33292\314088425.py", line 23, in <cell line: 23>
model.fit(X_train, y_train ,batch_size = 20, epochs = 50, verbose=1)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1650, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1249, in train_function
return step_function(self, iterator)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1233, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1222, in run_step
outputs = model.train_step(data)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1024, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\engine\training.py", line 1082, in compute_loss
return self.compiled_loss(
File "C:\Users\16agn\anaconda3\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\16agn\anaconda3\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\losses.py", line 284, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\16agn\anaconda3\lib\site-packages\keras\losses.py", line 1499, in mean_squared_error
y_true = tf.cast(y_true, y_pred.dtype)
Node: 'mean_squared_error/Cast'
Cast string to float is not supported
[[{{node mean_squared_error/Cast}}]] [Op:__inference_train_function_11279]
​
Please help me to solve my problem, I don't know how to make the program works

The error is due to a mismatch between the target data and the output layer in the model. The target data is of shape (N, 1), but the output layer of the model has shape (N,), which means the model is expecting a 1D array instead of a 2D array. You can resolve the issue by reshaping the target data to a 1D array using the reshape method from numpy:
y_train = y_train.reshape(-1,)
y_test = y_test.reshape(-1,)
Before fitting the model:
model.fit(X_train, y_train ,batch_size = 20, epochs = 50, verbose=1)

Related

InvalidArgumentError: Graph execution error in datascience model

I am building a datascience model using tensorflow and i got this error and i can't figure out how to resolve it.
I am using ipynb.
import numpy as np
import tensorflow as tf
npz = np.load('Audiobooks_data_train.npz')
train_inputs = npz['inputs'].astype(np.single)
train_targets = npz['targets'].astype(np.intc)
npz = np.load('Audiobooks_data_validation.npz')
validation_inputs, validation_targets = npz['inputs'].astype(np.single), npz['targets'].astype(np.intc)
npz = np.load('Audiobooks_data_test.npz')
test_inputs, test_targets = npz['inputs'].astype(np.single), npz['targets'].astype(np.intc)
input_size = 10
output_size = 2
hidden_layer_size = 50
model = tf.keras.Sequential([
tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
tf.keras.layers.Dense(output_size, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
batch_size = 100
max_epochs = 100
early_stopping = tf.keras.callbacks.EarlyStopping(patience=2)
model.fit(train_inputs,
train_targets,
batch_size=batch_size,
epochs=max_epochs,
callbacks=[early_stopping],
validation_data=(validation_inputs, validation_targets),
verbose = 2
)
Epoch 1/100
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call
last) d:\data
science\TensorFlow_Audiobooks_Machine_Learning_with_comments.ipynb
Cell 9 in <cell line: 40>()
36 early_stopping = tf.keras.callbacks.EarlyStopping(patience=2)
38 # fit the model
39 # note that this time the train, validation and test data are not iterable
---> 40 model.fit(train_inputs, # train inputs
41 train_targets, # train targets
42 batch_size=batch_size, # batch size
43 epochs=max_epochs, # epochs that we will train for (assuming early stopping doesn't kick in)
44 # callbacks are functions called by a task when a task is completed
45 # task here is to check if val_loss is increasing
46 callbacks=[early_stopping], # early stopping
47 validation_data=(validation_inputs, validation_targets), # validation data
48 verbose = 2 # making sure we get enough information about the training process
49 )
File
c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py:70,
in filter_traceback..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
c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\eager\execute.py:52,
in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
50 try:
51 ctx.ensure_initialized()
---> 52 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
53 inputs, attrs, num_outputs)
54 except core._NotOkStatusException as e:
55 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node
'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
defined at (most recent call last):
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\runpy.py",
line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\runpy.py",
line 86, in _run_code
exec(code, run_globals)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel_launcher.py",
line 17, in
app.launch_new_instance()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\traitlets\config\application.py",
line 846, in launch_instance
app.start()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelapp.py",
line 712, in start
self.io_loop.start()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\tornado\platform\asyncio.py",
line 199, in start
self.asyncio_loop.run_forever()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 600, in run_forever
self._run_once()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 1896, in _run_once
handle._run()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py",
line 80, in _run
self._context.run(self._callback, *self._args)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py",
line 504, in dispatch_queue
await self.process_one()
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py",
line 493, in process_one
await dispatch(*args)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py",
line 400, in dispatch_shell
await result
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\kernelbase.py",
line 724, in execute_request
reply_content = await reply_content
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\ipkernel.py",
line 383, in do_execute
res = shell.run_cell(
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\ipykernel\zmqshell.py",
line 528, in run_cell
return super().run_cell(*args, **kwargs)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py",
line 2880, in run_cell
result = self._run_cell(
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py",
line 2935, in _run_cell
return runner(coro)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\async_helpers.py",
line 129, in pseudo_sync_runner
coro.send(None)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py",
line 3134, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py",
line 3337, in run_ast_nodes
if await self.run_code(code, result, async=asy):
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py",
line 3397, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\nakul\AppData\Local\Temp\ipykernel_10980\600390782.py", line
40, in <cell line: 40>
model.fit(train_inputs, # train inputs
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py",
line 65, in error_handler
return fn(*args, **kwargs)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1650, in fit
tmp_logs = self.train_function(iterator)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1249, in train_function
return step_function(self, iterator)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1233, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1222, in run_step
outputs = model.train_step(data)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1024, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py",
line 1082, in compute_loss
return self.compiled_loss(
File "c:\Users\nakul\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\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py",
line 152, in call
losses = call_fn(y_true, y_pred)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py",
line 284, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py",
line 2098, in sparse_categorical_crossentropy
return backend.sparse_categorical_crossentropy(
File "c:\Users\nakul\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\backend.py",
line 5633, in sparse_categorical_crossentropy
res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
logits and labels must have the same first dimension, got logits shape
[100,2] and labels shape [1000] [[{{node
sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
[Op:__inference_train_function_974]
i couldn't figure out the cause of error

Maximal spectral entropy as custom loss in Tensorflow giving incompatible shapes

I would like to maximize the spectral entropy in Tensorflow. I have create the following custom loss function. Note that minus sign is absent from the entropy score because minimizing the negative of the spectral entropy should maximize the spectral entropy.
class SpectralEntropy(tf.keras.losses.Loss):
def call(self, y_true, y_pred):
y_pred = tf.reshape(y_pred, [len(y_pred)])
result = y - y_pred
result = tf.signal.rfft(result)
result = tf.abs(result)
result = tf.math.pow(result, 2)
result = result / result.shape[0]
result = result / tf.reduce_sum(result)
result = result * tf.math.log(result)
result = tf.reduce_sum(result)
return result
Here is the rest of the code (for reproducibility):
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
x = tf.random.normal((1000,))
y = 5 * x + 10 + tf.random.normal((1000,))
model = tf.keras.Sequential([
layers.Dense(input_shape=[1,], units=1)
])
model.compile(loss=SpectralEntropy())
model.summary()
history = model.fit(x=x, y=y, epochs=1)
And here is the error I am getting:
Traceback (most recent call last):
File "/home/galen/Dropbox/bin/correlation_as_loss.py", line 54, in <module>
history = model.fit(x=x, y=y, epochs=1)
File "/usr/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 54, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'gradient_tape/SpectralEntropy/sub/BroadcastGradientArgs' defined at (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.10/idlelib/run.py", line 164, in main
ret = method(*args, **kwargs)
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/home/galen/Dropbox/bin/correlation_as_loss.py", line 54, in <module>
history = model.fit(x=x, y=y, epochs=1)
File "/usr/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/lib/python3.10/site-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/lib/python3.10/site-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/lib/python3.10/site-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/lib/python3.10/site-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/lib/python3.10/site-packages/keras/engine/training.py", line 893, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "/usr/lib/python3.10/site-packages/keras/optimizers/optimizer_v2/optimizer_v2.py", line 537, in minimize
grads_and_vars = self._compute_gradients(
File "/usr/lib/python3.10/site-packages/keras/optimizers/optimizer_v2/optimizer_v2.py", line 590, in _compute_gradients
grads_and_vars = self._get_gradients(tape, loss, var_list, grad_loss)
File "/usr/lib/python3.10/site-packages/keras/optimizers/optimizer_v2/optimizer_v2.py", line 471, in _get_gradients
grads = tape.gradient(loss, var_list, grad_loss)
Node: 'gradient_tape/SpectralEntropy/sub/BroadcastGradientArgs'
Incompatible shapes: [1000] vs. [32]
[[{{node gradient_tape/SpectralEntropy/sub/BroadcastGradientArgs}}]] [Op:__inference_train_function_696]
One guess is that Tensorflow is truncating to the Nyquist frequency which results in a shorter array that the automated differentiation doesn't know how to backpropogate from... But I don't know.
Note that the above code works fine if I replace the SpectralEntropy loss with mean squared error. Or maybe y_pred = tf.reshape(y_pred, [len(y_pred)]) is causing a problem? I am reshaping because otherwise the difference seems to be (1000, 1000) rather than (1000,).

How to use K-fold cross validation on transfer learning?

I have created a transfer learning model using Resnet50. I want to perform K-fold cross-validation on my model after which I want to find the average AUC value and standard deviation. However, I am getting an error message while performing the task. I have created a separate Files.csv file which contains the image names and their corresponding labels. I am not sure if this is the correct method or not. Please let me know if there is any other process. Please find my code below:
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras import Model, layers
from tensorflow.keras.models import load_model, model_from_json
from tensorflow.keras.layers import GlobalAveragePooling2D, Dropout, Dense, Input
import numpy as np
import pandas as pd
import os
from sklearn.model_selection import KFold, StratifiedKFold
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_data = pd.read_csv('Files.csv')
Y = train_data[['label']]
kf = KFold(n_splits = 5)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
idg = ImageDataGenerator(rescale = 1./255,
horizontal_flip=True,
rotation_range=40,
zoom_range= 0.2,
shear_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,)
validation_datagen = ImageDataGenerator(rescale = 1./255)
def get_model_name(k):
return 'model_'+str(k)+'.h5'
from keras import models
from keras.layers import Dense, Flatten
from tensorflow.keras import optimizers
from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications import ResNet50
image_dir=r'D:/regionGrowing_MLT/NewSavedRGBImages/Training'
VALIDATION_ACCURACY = []
VALIDAITON_LOSS = []
save_dir = 'C:/Users/warid'
fold_var = 1
for train_index, val_index in kf.split(np.zeros(n),Y):
training_data = train_data.iloc[train_index]
validation_data = train_data.iloc[val_index]
train_data_generator = idg.flow_from_dataframe(training_data, directory = image_dir,
x_col = "filename", y_col = "label",
class_mode = "categorical", shuffle = True)
valid_data_generator = idg.flow_from_dataframe(validation_data, directory = image_dir,
x_col = "filename", y_col = "label",
class_mode = "categorical", shuffle = True)
# CREATE NEW MODEL
model = models.Sequential()
model.add(ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3)))
model.add(Flatten())
model.add(ChannelAttention(32, 8))
model.add(SpatialAttention(7))
model.add(Dense(256, activation='relu', name='fc1'))
model.add(Dense(128, activation='relu', name='fc2'))
model.add(layers.Dropout(0.5)) #### used for regularization (to aviod overfitting)
model.add(Dense(2, activation='sigmoid'))
# model.summary()
model.compile(optimizer=optimizers.Adam(learning_rate=2e-5),
loss='binary_crossentropy',
metrics=['accuracy'])
# COMPILE NEW MODEL
# CREATE CALLBACKS
checkpoint = tf.keras.callbacks.ModelCheckpoint(save_dir+get_model_name(fold_var),
monitor='val_accuracy', verbose=1,
save_best_only=True, mode='max')
callbacks_list = [checkpoint]
# There can be other callbacks, but just showing one because it involves the model name
# This saves the best model
# FIT THE MODEL
history = model.fit(train_data_generator,
epochs=num_epochs,
callbacks=callbacks_list,
validation_data=valid_data_generator)
#PLOT HISTORY
# :
# :
# LOAD BEST MODEL to evaluate the performance of the model
model.load_weights("/saved_models/model_"+str(fold_var)+".h5")
results = model.evaluate(valid_data_generator)
results = dict(zip(model.metrics_names,results))
VALIDATION_ACCURACY.append(results['accuracy'])
VALIDATION_LOSS.append(results['loss'])
tf.keras.backend.clear_session()
fold_var += 1
After running this code, I am getting the following error message:
Found 3076 validated image filenames belonging to 2 classes.
Found 769 validated image filenames belonging to 1 classes.
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
Input In [75], in <cell line: 7>()
39 callbacks_list = [checkpoint]
40 # There can be other callbacks, but just showing one because it involves the model name
41 # This saves the best model
42 # FIT THE MODEL
---> 43 history = model.fit(train_data_generator,
44 epochs=num_epochs,
45 callbacks=callbacks_list,
46 validation_data=valid_data_generator)
47 #PLOT HISTORY
48 # :
49 # :
50
51 # LOAD BEST MODEL to evaluate the performance of the model
52 model.load_weights("/saved_models/model_"+str(fold_var)+".h5")
File ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File ~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
53 ctx.ensure_initialized()
---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'sequential_2/flatten_2/Reshape' defined at (most recent call last):
File "C:\Users\warid\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\warid\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Users\warid\anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\Users\warid\anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Users\warid\anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
self._run_once()
File "C:\Users\warid\anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
handle._run()
File "C:\Users\warid\anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
await self.process_one()
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
await dispatch(*args)
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
await result
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
reply_content = await reply_content
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 360, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\warid\anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
result = self._run_cell(
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
return runner(coro)
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3106, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3309, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "C:\Users\warid\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\warid\AppData\Local\Temp\ipykernel_37076\2928028949.py", line 43, in <cell line: 7>
history = model.fit(train_data_generator,
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 1051, in train_function
return step_function(self, iterator)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 1030, in run_step
outputs = model.train_step(data)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 889, in train_step
y_pred = self(x, training=True)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\training.py", line 490, in __call__
return super().__call__(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\sequential.py", line 374, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\functional.py", line 458, in call
return self._run_internal_graph(
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\functional.py", line 596, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\warid\anaconda3\lib\site-packages\keras\layers\reshaping\flatten.py", line 98, in call
return tf.reshape(inputs, flattened_shape)
Node: 'sequential_2/flatten_2/Reshape'
Input to reshape is a tensor with 4194304 values, but the requested shape requires a multiple of 100352
[[{{node sequential_2/flatten_2/Reshape}}]] [Op:__inference_train_function_37893]

How can I solve this kind of error, I'm using word embedding word2vec: InvalidArgumentError: Graph execution error:

i am trying to train a model using cnn method with tensorflow and keras, but i keep getting the error bellow this code , could anyone help me or give me at least a peace of advice?
#Convolutional Neural Network (CNN) with Word2Vec
CNN_Word2Vec_model = Sequential([
Embedding(input_dim =word2Vec_embedding_matrix.shape[0], input_length=max_len, output_dim=word2Vec_embedding_matrix.shape[1],weights=[word2Vec_embedding_matrix], trainable=False),
SpatialDropout1D(0.5),
# ... 100 filters with a kernel size of 4 so that each convolution will consider a window of 4 word embeddings
Conv1D(filters=100, kernel_size=4, padding='same', activation='relu'),
#**batch normalization layer** normalizes the activations of the previous layer at each batch,
#i.e. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1.
#It will be added after the activation function between a convolutional and a max-pooling layer.
BatchNormalization(),
GlobalMaxPool1D(),
Dropout(0.2),
Dense(50, activation = 'relu'),
Dense(5, activation = 'softmax')
])
#Customized the evaluation to analyse the model in terms of accuracy and mean value accuracy
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
CNN_Word2Vec_model.compile(loss="sparse_categorical_crossentropy", optimizer=Adam(0.01), metrics=['accuracy'])
#CNN_Word2Vec_model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(0.01), metrics=['accuracy', mean_pred, fmeasure, precision, auroc, recall])
CNN_Word2Vec_model.summary()
When I feed data to CNN model with word embedding (word2vec) in the below code then I got the error, so please how can I resolve it.
CNN_Word2Vec_model_fit = CNN_Word2Vec_model.fit(X_tra, y_tra, batch_size=batch_size2, epochs=num_epochs, validation_data=(X_val, y_val), callbacks=[early])
the full error is below:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-48-7214f2a862e6> in <module>()
----> 1 CNN_Word2Vec_model_fit = CNN_Word2Vec_model.fit(X_tra, y_tra, batch_size=batch_size2, epochs=num_epochs, validation_data=(X_val, y_val), callbacks=[early])
2 #CNN_Word2Vec_model_fit = CNN_Word2Vec_model.fit(X_tra, y_tra, batch_size=batch_size2, epochs=num_epochs, validation_data=(X_val, y_val))
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'sequential/embedding/embedding_lookup' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-47-169eb83cb089>", line 2, in <module>
CNN_Word2Vec_model_fit = CNN_Word2Vec_model.fit(X_tra, y_tra, batch_size=batch_size2, epochs=num_epochs, validation_data=(X_val, y_val))
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
Node: 'sequential/embedding/embedding_lookup'
indices[233,187] = 10690 is not in [0, 8262)
[[{{node sequential/embedding/embedding_lookup}}]] [Op:__inference_train_function_5282]
When I ran into this it was because the shape of my data and the model input were incompatible/ I think it can also be caused by the the training data shape and model output being incompatible. So my recommendation would be to check the shape of your inputs/outputs and make sure they are what you want.

Have you experienced an error in Keras with a custom generator to handle double input (img + size) while training the model?

The goal is to categorize pottery using both images and a size.
I'm using Tensorflow version 2.8.0
I created a custom generator to return ((img, size),classification) as follows:
def __init__(self,input_gen1,input_gen2,
batch_size,
shuffle=False):
self.batch_size = batch_size
self.shuffle = shuffle
self.gen = input_gen1
self.measures = input_gen2
def __len__(self):
return len(self.gen)
def on_epoch_end(self):
pass
def __getitem__(self, index):
filenames_np = np.vectorize(os.path.basename)(np.array(self.gen.filenames[index : index + self.gen.batch_size]))
measures_of_files = np.vectorize(self.measures.get)(filenames_np)
return (self.gen[index][0],measures_of_files),self.gen[index][1]
and this is how the generator is used composing flow_from_directory and a dictionary filename-size
TRAINING_DIR = "/tmp/Anfore/training/"
train_datagen = ImageDataGenerator(rescale=1./255,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
seed = 1
train_generator = train_datagen.flow_from_directory(TRAINING_DIR,
batch_size=64,
class_mode='categorical',
target_size=(150, 150),
seed=seed)
VALIDATION_DIR = "/tmp/Anfore/testing/"
validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,
batch_size=64,
class_mode='categorical',
target_size=(150, 150))
testCustomDataGen_for_train = CustomDataGen(train_generator,dict_measures,batch_size=64)
testCustomDataGen_for_validation = CustomDataGen(validation_generator,dict_measures,batch_size=64)
The model is composed of pretrained InceptionV3 stripped of the last few layers and coupled with a simple Dense layer with Concatenate to classify the results.
weights_url = "https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"
weights_file = "inception_v3.h5"
urllib.request.urlretrieve(weights_url, weights_file)
# Instantiate the model
pre_trained_model = InceptionV3(input_shape=(150, 150, 3),
include_top=False,
weights=None)
# load pre-trained weights
pre_trained_model.load_weights(weights_file)
# freeze the layers
for layer in pre_trained_model.layers:
layer.trainable = False
# pre_trained_model.summary()
last_layer = pre_trained_model.get_layer('mixed7')
last_output = last_layer.output
from keras.layers import *
from keras.utils.vis_utils import plot_model
model2 = Sequential()
model2.add(Dense(1, input_shape=(1,), activation="relu"))
# here I can join the 2 models
x = layers.Conv2D(128,kernel_size=(3,3),activation='relu',padding='same')(last_output)
x = layers.GlobalAveragePooling2D()(x)
mergedOut = Concatenate()([x,model2.output])
x = layers.Dense(12, activation="softmax", name="classification")(mergedOut)
model = Model([pre_trained_model.input,model2.input], x)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['acc'],
run_eagerly=False)
The schema of the model is the following:
The problem is that when I do the training
history = model.fit(
testCustomDataGen_for_train,
validation_data=testCustomDataGen_for_validation,
epochs=150,
verbose=1)
I get an error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-26-4d4e59a0e1c6> in <module>()
3 validation_data=testCustomDataGen_for_validation,
4 epochs=150,
----> 5 verbose=1)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Graph execution error:
Detected at node 'gradient_tape/model_7/concatenate_9/ConcatOffset' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-26-4d4e59a0e1c6>", line 5, in <module>
verbose=1)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
tmp_logs = self.train_function(iterator)
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 863, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 531, in minimize
loss, var_list=var_list, grad_loss=grad_loss, tape=tape)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 583, in _compute_gradients
grads_and_vars = self._get_gradients(tape, loss, var_list, grad_loss)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 464, in _get_gradients
grads = tape.gradient(loss, var_list, grad_loss)
Node: 'gradient_tape/model_7/concatenate_9/ConcatOffset'
All dimensions except 1 must match. Input 1 has shape [64 1] and doesn't match input 0 with shape [28 128].
[[{{node gradient_tape/model_7/concatenate_9/ConcatOffset}}]] [Op:__inference_train_function_37647]
Maybe I'm too much of a newbie but I don't get the sense of the error. Can anybody give me a hint on where I can intervene to address the issue?
The colab notebook is here:
https://colab.research.google.com/drive/17nIpC4OUy5gk0AnVhwNawfel4-HjS_h4?usp=sharing
Any help is warmly welcome

Categories