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
Related
I am trying to build a neural network. I have a dataframe of 1460 by 73 as a train (X_train), an 1460 (Y_train) for the results . The df is corrected with no blank null or empty values and all features have been turned to float. However when i try to fit the model i get an error that i can't understand. Could someone please help me understand what the problem is?
X_test = np.array(test_df).astype(np.float32)
Y_train = np.array(train_df['SalePrice']).astype(np.float32)
train_df = train_df.drop(['SalePrice'], axis=1)
X_train = np.array(train_df).astype(np.float32)
X_train.shape, X_test.shape,Y_train.shape
((1460, 73), (1459, 73), (1460,))
logging.getLogger("tensorflow").setLevel(logging.ERROR)
tf.random.set_seed(1234)
model = Sequential(
[
Dense(units=74, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.1)),
Dense(units=37, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.1)),
Dense(units=1, activation='linear')
], name="Complex"
)
model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-2),
)
model.fit(
X_train, Y_train,
epochs=1000
)
model.predict(test_df)
model.summary()
the error i get is this :
Epoch 1/1000
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
Input In [91], in <cell line: 18>()
4 model = Sequential(
5 [
6 Dense(units=74, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.1)),
(...)
9 ], name="Complex"
10 )
11 model.compile(
12
13 loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
14 optimizer=tf.keras.optimizers.Adam(learning_rate=1e-2),
15 )
---> 18 model.fit(
19 X_train, Y_train,
20 epochs=1000
21 )
22 #logit = model(X_test)
23 #f_x=tf.nn.softmax(logit)
25 model.predict(test_df)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\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 ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\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 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel_launcher.py", line 17, in <module>
app.launch_new_instance()
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\traitlets\config\application.py", line 976, in launch_instance
app.start()
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\kernelapp.py", line 712, in start
self.io_loop.start()
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 603, in run_forever
self._run_once()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 1899, in _run_once
handle._run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\kernelbase.py", line 508, in dispatch_queue
await self.process_one()
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\kernelbase.py", line 497, in process_one
await dispatch(*args)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\kernelbase.py", line 404, in dispatch_shell
await result
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\kernelbase.py", line 728, in execute_request
reply_content = await reply_content
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\ipkernel.py", line 383, in do_execute
res = shell.run_cell(
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\zmqshell.py", line 528, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\interactiveshell.py", line 2881, in run_cell
result = self._run_cell(
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\interactiveshell.py", line 2936, in _run_cell
return runner(coro)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\interactiveshell.py", line 3135, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\interactiveshell.py", line 3338, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\interactiveshell.py", line 3398, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\Manos\AppData\Local\Temp\ipykernel_11152\1723988500.py", line 18, in <cell line: 18>
model.fit(
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1051, in train_function
return step_function(self, iterator)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 1030, in run_step
outputs = model.train_step(data)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\training.py", line 948, in compute_loss
return self.compiled_loss(
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\losses.py", line 243, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\losses.py", line 1860, in sparse_categorical_crossentropy
return backend.sparse_categorical_crossentropy(
File "C:\Users\Manos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\backend.py", line 5238, in sparse_categorical_crossentropy
res = tf.nn.sparse_softmax_cross_entropy_with_logits(
Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
Received a label value of 611657 which is outside the valid range of [0, 1). Label values: 163500 240000 239500 130000 277000 79000 225000 220000 157000 204000 134000 168000 611657 92000 240000 124900 280000 200000 87000 158500 110000 119900 109500 157000 133000 91500 201800 83000 52500 266000 147500 93000
[[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_1591]
I am getting multiple error while running my code can't able to understand it is because of dataset or some architecture issue.
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.layers import Dense,concatenate,Activation,Dropout,Input,LSTM,Flatten
from tensorflow.keras import regularizers,initializers,optimizers,Model
tokenizeressay = Tokenizer(oov_token="<OOV>")
# generate word indexes
tokenizeressay.fit_on_texts(X_train.essay)
# generate sequences
tokenisedessayTrain = tokenizeressay.texts_to_sequences(X_train.essay)
tokenisedessayTest = tokenizeressay.texts_to_sequences(X_test.essay)
tokenisedessayCV = tokenizeressay.texts_to_sequences(X_cv.essay)
tokenisedTrainessay = pad_sequences(tokenisedessayTrain,maxlen=350)
tokenisedTestessay = pad_sequences(tokenisedessayTest,maxlen=350)
tokenizedCvessay = pad_sequences(tokenisedessayCV,maxlen=350)
import pickle
with open("C:\\Users\\Administrator\\Downloads\\glove_vectors", 'rb') as f:
glove = pickle.load(f)
size_glove = 300 # glove vectors are 300 dims
size_vocab = len(list(tokenizeressay.word_counts.keys()))
word_Weights = np.zeros((size_vocab+1, size_glove))
for word, i in tokenizeressay.word_index.items():
embedding_vector = glove.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
word_Weights[i-1] = embedding_vector
print("Max Length on Essay")
print(X_train.essay.apply(lambda x : len(x.split(' '))).max())
print("Word Item Length:")
print(len(tokenizeressay.word_index.items()))
print("Max Length we are taken will be 350")
Max Length on Essay
339
Word Item Length:
49330
Max Length we are taken will be 350
#LSTM and get the LSTM output and Flatten that output. ----> Accoridng to the assignment
# I have choose 128 Units
essayInput = Input(shape=(350,),dtype='int32',name='essayInput')
embeddedEssay = Embedding(input_dim=(len(tokenizeressay.word_index.items())),output_dim=300,name='embeddedEssay',weights=[word_Weights],trainable=False)(essayInput)
essayLSTM = LSTM(units=128, return_sequences=True)(embeddedEssay)
essayOut = Flatten()(essayLSTM)
from tensorflow.keras.layers import concatenate
concat_layer = concatenate(inputs=[essayOut],name="concat_layer")
from tensorflow.keras.layers import Dropout,BatchNormalization
AfterConcatLayer = Dense(256,activation='relu',kernel_regularizer=regularizers.l2(0.001),kernel_initializer=initializers.he_normal())(concat_layer)
AfterConcatLayer = Dropout(0.5)(AfterConcatLayer)
AfterConcatLayer = Dense(128,activation='relu',kernel_regularizer=regularizers.l2(0.001),kernel_initializer=initializers.he_normal())(AfterConcatLayer)
AfterConcatLayer = Dropout(0.5)(AfterConcatLayer)
AfterConcatLayer = BatchNormalization()(AfterConcatLayer)
AfterConcatLayer = Dense(64,activation='relu',kernel_regularizer=regularizers.l2(0.001),kernel_initializer=initializers.he_normal())(AfterConcatLayer)
AfterConcatLayer = Dropout(0.5)(AfterConcatLayer)
SoftmaxOutput = Dense(2, activation = 'softmax')(AfterConcatLayer)
model1 = Model([essayInput], SoftmaxOutput)
model1.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=0.0006,decay = 1e-4),metrics=[auc])
print(model1.summary())
checkpoint_path ="C:\\Windows_Old\\Learnings\\MachineLearning\\CheckPoints.hdf5"
cp_callback = ModelCheckpoint(filepath=checkpoint_path,save_best_only=True, save_weights_only=True, verbose=1,monitor='val_auc')
#Delete THIS CODE
import os
from keras.utils import to_categorical
if os.path.isfile(checkpoint_path):
model1.load_weights(checkpoint_path)
#essayInput,SchoolStateInput,TeacherPrefixInput,CleanCategoriesInput,CleanSubCategoriesInput,ProjectGradeInput,PriceProjectNumberInput
model1.fit([tokenisedTrainessay],to_categorical(y_train), epochs=50,verbose=2,batch_size=512,validation_split=0.3,callbacks = [cp_callback])
I am using DonorChoose Dataset. You can download the same from this Link
Error I am getting is:
InvalidArgumentError Traceback (most recent call last)
Input In [23], in <cell line: 10>()
7 model1.load_weights(checkpoint_path)
8 #essayInput,SchoolStateInput,TeacherPrefixInput,CleanCategoriesInput,CleanSubCategoriesInput,ProjectGradeInput,PriceProjectNumberInput
---> 10 model1.fit([tokenisedTrainessay],to_categorical(y_train), epochs=50,verbose=2,batch_size=512,validation_split=0.3,callbacks = [cp_callback])
File C:\ProgramData\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 C:\ProgramData\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:
Different Error: Invalid Argument Error
InvalidArgumentError: Graph execution error:
Detected at node 'model/embeddedEssay/embedding_lookup' defined at (most recent call last):
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\ProgramData\Anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
self._run_once()
File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
handle._run()
File "C:\ProgramData\Anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
await self.process_one()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
await dispatch(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
await result
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
reply_content = await reply_content
File "C:\ProgramData\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:\ProgramData\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
result = self._run_cell(
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
return runner(coro)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\ProgramData\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:\ProgramData\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:\ProgramData\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\Administrator\AppData\Local\Temp\2\ipykernel_4140\2952872782.py", line 10, in <cell line: 10>
model1.fit([tokenisedTrainessay],to_categorical(y_train), epochs=50,verbose=2,batch_size=512,validation_split=0.3,callbacks = [cp_callback])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1606, in fit
val_logs = self.evaluate(
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1947, in evaluate
tmp_logs = self.test_function(iterator)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1727, in test_function
return step_function(self, iterator)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1713, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1701, in run_step
outputs = model.test_step(data)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1665, in test_step
y_pred = self(x, training=False)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 557, in __call__
return super().__call__(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1097, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\functional.py", line 510, in call
return self._run_internal_graph(inputs, training=training, mask=mask)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\functional.py", line 667, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1097, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\core\embedding.py", line 208, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'model/embeddedEssay/embedding_lookup'
indices[413,323] = 49330 is not in [0, 49330)
[[{{node model/embeddedEssay/embedding_lookup}}]] [Op:__inference_test_function_5617]
I am kinda stuck in this from last three days.
I am have tries to increase the dim by 1 but failed.
Code Snippet attached
essayInput = Input(shape=(350,),dtype='int32',name='essayInput')
embeddedEssay = Embedding(input_dim=(len(tokenizeressay.word_index.items())+1),output_dim=300,name='embeddedEssay',weights=[word_Weights],trainable=False)(essayInput)
essayLSTM = LSTM(units=128, return_sequences=True)(embeddedEssay)
essayOut = Flatten()(essayLSTM)
Error I am getting while doing this.
ValueError Traceback (most recent call last)
Input In [25], in <cell line: 5>()
1 #LSTM and get the LSTM output and Flatten that output. ----> Accoridng to the assignment
2 # I have choose 128 Units
4 essayInput = Input(shape=(350,),dtype='int32',name='essayInput')
----> 5 embeddedEssay = Embedding(input_dim=(len(tokenizeressay.word_index.items())+1),output_dim=300,name='embeddedEssay',weights=[word_Weights],trainable=False)(essayInput)
6 essayLSTM = LSTM(units=128, return_sequences=True)(embeddedEssay)
7 essayOut = Flatten()(essayLSTM)
File C:\ProgramData\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 C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py:1772, in Layer.set_weights(self, weights)
1770 ref_shape = param.shape
1771 if not ref_shape.is_compatible_with(weight_shape):
-> 1772 raise ValueError(
1773 f"Layer {self.name} weight shape {ref_shape} "
1774 "is not compatible with provided weight "
1775 f"shape {weight_shape}."
1776 )
1777 weight_value_tuples.append((param, weight))
1778 weight_index += 1
ValueError: Layer embeddedEssay weight shape (49331, 300) is not compatible with provided weight shape (49330, 300).
As stated in the error,
ValueError: Layer embeddedEssay weight shape (49331, 300) is not compatible with provided weight shape (49330, 300).
The error has occured due to shape mismatch, hence try increasing the input dimension of the Embedding layer like below.
embeddedEssay = Embedding(input_dim=(len(tokenizeressay.word_index.items()))+1,output_dim=300,name='embeddedEssay',weights=[word_Weights],trainable=False)(essayInput)
Also increase the vocabulary size as follows in order to avoid the error.
size_vocab = len(list(tokenizeressay.word_counts.keys()))+1
Kindly refer to this gist for the complete code and this example for more information on the error. Thank you!
Im trying to use Keras model to recommend visits to some content based on his previous visits and other users behaviours. I have done something similar with a book recommendator system with a rating of 1-5.
user_id content_id visit
1 1 1
2 1 0
3 1 0
1 2 1
2 2 0
3 2 1
I create my embeddings
from keras.layers import Input, Embedding, Flatten, Dot, Dense
from keras.models import Model
from sklearn.model_selection import train_test_split
from keras.models import load_model
# content embedding path
content_input = Input(shape=[1], name="Content-Input")
content_embedding = Embedding(n_content+1, 10, name="Content-Embedding")(content_input)
content_vec = Flatten(name="Flatten-Contents")(content_embedding)
# user embedding path
user_input = Input(shape=[1], name="User-Input")
user_embedding = Embedding(n_users+1, 10, name="User-Embedding")(user_input)
user_vec = Flatten(name="Flatten-Users")(user_embedding)
# dot product and creating model
prod = Dot(name="Dot-Product", axes=1)([content_vec, user_vec])
model = Model([user_input, content_input], prod)
model.compile('adam', 'mean_squared_error')
And I try to create the model
train, test = train_test_split(visits, test_size=0.2, random_state=42)
if os.path.exists('regression_model.h5'):
model = load_model('regression_model.h5')
else:
history = model.fit([train.user_id, train.book_id], train.rating, epochs=5, verbose=1)
model.save('regression_model.h5')
plt.plot(history.history['loss'])
plt.xlabel("Epochs")
plt.ylabel("Training Error")
But then I get a InvalidArgumentError: Graph execution error.
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/var/folders/y8/4bdyq9hn6p58g8jy30g6vk000000gn/T/ipykernel_85241/1014936592.py in <module>
2 model = load_model('regression_model.h5')
3 else:
----> 4 history = model.fit([train.user_id, train.public_id], train.visit, epochs=5, verbose=1)
5 model.save('regression_model.h5')
6 # plt.plot(history.history['loss'])
~/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py in 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
~/.pyenv/versions/3.7.3/lib/python3.7/site-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 'model_10/content-Embedding/embedding_lookup' defined at (most recent call last):
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel_launcher.py", line 17, in <module>
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/traitlets/config/application.py", line 976, in launch_instance
app.start()
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 712, in start
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 199, in start
.. versionadded:: 4.1
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/asyncio/base_events.py", line 539, in run_forever
self._run_once()
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once
handle._run()
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 508, in dispatch_queue
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 497, in process_one
'language_info': self.language_info,
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 404, in dispatch_shell
# FIXME: on rare occasions, the flush doesn't seem to make it to the
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 728, in execute_request
while True:
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 390, in do_execute
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 528, in run_cell
)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2915, in run_cell
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2960, in _run_cell
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3186, in run_cell_async
except Exception:
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3377, in run_ast_nodes
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3457, in run_code
File "/var/folders/y8/4bdyq9hn6p58g8jy30g6vk000000gn/T/ipykernel_85241/1014936592.py", line 4, in <module>
history = model.fit([train.user_id, train.public_id], train.visit, epochs=5, verbose=1)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/training.py", line 490, in __call__
return super().__call__(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/functional.py", line 459, in call
inputs, training=training, mask=mask)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/functional.py", line 596, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/Users/xxx/.pyenv/versions/3.7.3/lib/python3.7/site-packages/keras/layers/core/embedding.py", line 199, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'model_10/content-Embedding/embedding_lookup'
indices[10,0] = 21530 is not in [0, 21025)
[[{{node model_10/content-Embedding/embedding_lookup}}]] [Op:__inference_train_function_6328]
I have tried changing the embedding dimensions, etc but no luck. Could it be that this model is not addapted to boolean data? Could I use another model? Or is the problem lying elsewhere?
Thank you?
I'm having multiple errors while running this vanilla rnn training code (code and errors shown below). I don't know if its because of my dataset or is it something else.
The data is made up of opcodes in assembly language.
The size of the training data is (2000, 53203).
This is vanilla RNN training code
import numpy as np
X_test = data[:200]
Y_test = np.array(Y_data[:200])
X_train = data[200:]
Y_train = np.array(Y_data[200:])
from tensorflow.keras.layers import SimpleRNN, Embedding, Dense
from tensorflow.keras.models import Sequential
model = Sequential()
model.add(Embedding(len(word_to_index), 32)) # 임베딩 벡터의 차원은 32
model.add(SimpleRNN(32)) # RNN 셀의 hidden_size는 32
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
It's Error Code...
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_13744/877033043.py in <module>
8
9 model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
---> 10 history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in 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
~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py 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:
InvalidArgumentError: Graph execution error:
Detected at node 'sequential_3/embedding_2/embedding_lookup' defined at (most recent call last):
File "C:\Users\Administrator\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Administrator\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Users\Administrator\anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\Users\Administrator\anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Users\Administrator\anaconda3\lib\asyncio\base_events.py", line 596, in run_forever
self._run_once()
File "C:\Users\Administrator\anaconda3\lib\asyncio\base_events.py", line 1890, in _run_once
handle._run()
File "C:\Users\Administrator\anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 457, in dispatch_queue
await self.process_one()
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 446, in process_one
await dispatch(*args)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 353, in dispatch_shell
await result
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 648, in execute_request
reply_content = await reply_content
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 353, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2901, in run_cell
result = self._run_cell(
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2947, in _run_cell
return runner(coro)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3172, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3364, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_13744/877033043.py", line 10, in <module>
history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1445, in fit
val_logs = self.evaluate(
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1756, in evaluate
tmp_logs = self.test_function(iterator)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1557, in test_function
return step_function(self, iterator)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1546, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1535, in run_step
outputs = model.test_step(data)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1499, in test_step
y_pred = self(x, training=False)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 490, in __call__
return super().__call__(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\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\Administrator\anaconda3\lib\site-packages\keras\engine\functional.py", line 458, in call
return self._run_internal_graph(
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\functional.py", line 596, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\layers\core\embedding.py", line 199, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'sequential_3/embedding_2/embedding_lookup'
indices[25,40743] = 575 is not in [0, 575)
[[{{node sequential_3/embedding_2/embedding_lookup}}]] [Op:__inference_test_function_4239]
I'm running this on JupyterLab
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