編碼:
#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)
錯誤訊息接收:

2022-10-07 19:33:01.890445:W tensorflow/stream_executor/platform/default/dso_loader.cc:64] 無法加載動態庫“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] 無法打開某些 GPU 庫。如果您想使用 GPU,請確保正確安裝了上述缺少的庫。按照https://www.tensorflow.org/install/gpu上的指南進行操作了解如何為您的平臺下載和設定所需的庫。跳過注冊 GPU 設備... 2022-10-07 19:33:01.891612: I tensorflow/core/platform/cpu_feature_guard.cc:193] 此 TensorFlow 二進制檔案使用 oneAPI 深度神經網路庫 (oneDNN) 進行了優化,以使用以下 CPU性能關鍵操作中的指令:AVX AVX2 要在其他操作中啟用它們,請使用適當的編譯器標志重建 TensorFlow。Epoch 1/10 Traceback(最近一次呼叫最后一次):檔案“C:\Users\alhos\PycharmProjects\science_festival\yes.py”,第 41 行,在 model.fit(x=x_train, y=y_train, batch_size=512, epochs=10) 檔案“C:\Users\alhos\anaconda3\lib\site-packages\keras\utils\traceback_utils.py”,第 70 行,error_handler 從無檔案“C:\Users”中引發 e.with_traceback(filtered_tb) \alhos\AppData\Local\Temp_autograph_generated_fileyl92dvl3.py”,第 15 行,在 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:\Users\alhos\anaconda3\lib\site-packages\keras\engine\training.py", line 1160, in train_function *
return step_function(self, iterator)
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\engine\training.py", line 1146, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\engine\training.py", line 1135, in run_step **
outputs = model.train_step(data)
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\engine\training.py", line 994, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
return self.compiled_loss(
File "C:\Users\alhos\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\alhos\anaconda3\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\losses.py", line 272, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\losses.py", line 1990, in categorical_crossentropy
return backend.categorical_crossentropy(
File "C:\Users\alhos\anaconda3\lib\site-packages\keras\backend.py", line 5529, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 15) and (None, 10) are incompatible
uj5u.com熱心網友回復:
在 mnist你有 10 個目標 0-9,所以你需要提供 10 而 to_categirical 像
#Categorizing data:
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/511429.html
標籤:Python机器学习
