Machine learning code does not proceed after epoch one and gives me multiple errors

Question:

The code:

#Libraries to import:
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.datasets import mnist
np.random.seed(0)

#Downloading data
(x_train, y_train), (x_test, y_test) = mnist.load_data() 

#Categorizing data:
y_train = keras.utils.to_categorical(y_train, 15) 
y_test = keras.utils.to_categorical(y_test, 15) 

#Normalizing
x_train = x_train/255 
x_test = x_test/255

#Reshaping
x_train = x_train.reshape(x_train.shape[0], -1) 
x_test = x_test.reshape(x_test.shape[0], -1) 

#The neural network
model = Sequential()
model.add(Dense(units=128, input_shape=(784,), activation='relu'))
model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#Training
model.fit(x=x_train, y=y_train, batch_size=512, epochs=10)

Error messages receives:

Error messages

2022-10-07 19:33:01.890445: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘cudnn64_8.dll’; dlerror: cudnn64_8.dll not found
2022-10-07 19:33:01.890693: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1934] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices…
2022-10-07 19:33:01.891612: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/10
Traceback (most recent call last):
File "C:UsersalhosPycharmProjectsscience_festivalyes.py", line 41, in
model.fit(x=x_train, y=y_train, batch_size=512, epochs=10)
File "C:Usersalhosanaconda3libsite-packageskerasutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersalhosAppDataLocalTemp_autograph_generated_fileyl92dvl3.py", line 15, in tf__train_function
retval
= ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:

File "C:Usersalhosanaconda3libsite-packageskerasenginetraining.py", line 1160, in train_function  *
    return step_function(self, iterator)
File "C:Usersalhosanaconda3libsite-packageskerasenginetraining.py", line 1146, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:Usersalhosanaconda3libsite-packageskerasenginetraining.py", line 1135, in run_step  **
    outputs = model.train_step(data)
File "C:Usersalhosanaconda3libsite-packageskerasenginetraining.py", line 994, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:Usersalhosanaconda3libsite-packageskerasenginetraining.py", line 1052, in compute_loss
    return self.compiled_loss(
File "C:Usersalhosanaconda3libsite-packageskerasenginecompile_utils.py", line 265, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:Usersalhosanaconda3libsite-packageskeraslosses.py", line 152, in __call__
    losses = call_fn(y_true, y_pred)
File "C:Usersalhosanaconda3libsite-packageskeraslosses.py", line 272, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:Usersalhosanaconda3libsite-packageskeraslosses.py", line 1990, in categorical_crossentropy
    return backend.categorical_crossentropy(
File "C:Usersalhosanaconda3libsite-packageskerasbackend.py", line 5529, in categorical_crossentropy
    target.shape.assert_is_compatible_with(output.shape)

ValueError: Shapes (None, 15) and (None, 10) are incompatible
Asked By: Saucter

||

Answers:

In mnist you have 10 targets 0-9, So you need to provide 10 while to_categirical like

#Categorizing data:
y_train = keras.utils.to_categorical(y_train, 10) 
y_test = keras.utils.to_categorical(y_test, 10) 
Answered By: Mudasir Habib
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.