我已經撰寫了人工神經網路代碼來解決 Keggale Dog and Cats Kernal 問題,但是在訓練期間不知何故,它顯示 loss=nan 和差的準確性。我的代碼可以在https://www.kaggle.com/dilipkumar2k6/dogs-vs-cats-with-new-kernel/notebook找到
以下是有關錯誤的詳細資訊
from tensorflow import keras
# First apply Artificial neural network (ANN)
ann = keras.Sequential([
keras.layers.Flatten(input_shape=(IMG_SIZE, IMG_SIZE, 3)), # Flaten 3d to 1d
keras.layers.Dense(3000, activation='relu'), # more hidden layer gives better perf
keras.layers.Dense(1000, activation='relu'), # more hidden layer gives better perf
keras.layers.Dense(100, activation='relu'), # more hidden layer gives better perf
keras.layers.Dense(2, activation='sigmoid')
])
ann.compile(optimizer='SGD', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ann.fit(train_X, train_y, epochs=10)
錯誤
Epoch 1/10
438/438 [==============================] - 2s 2ms/step - loss: nan - accuracy: 5.0000e-04
Epoch 2/10
438/438 [==============================] - 1s 2ms/step - loss: nan - accuracy: 0.0000e 00
uj5u.com熱心網友回復:
在使用時sigmoid在輸出層中使用激活函式對我來說似乎有點奇怪sparse_categorical_crossentropy(盡管它也可以作業)。無論如何,我認為你應該考慮改變這一行:
keras.layers.Dense(2, activation='sigmoid')
到
keras.layers.Dense(1, activation='sigmoid')
并使用tf.keras.losses.BinaryCrossentropy(). 或者將您的激活函式更改為softmax并保持其余部分不變。
您還應該考慮重新設計模型并tf.keras.layers.Conv2D在展平資料之前使用至少一層。這是一個作業示例:
import tensorflow_datasets as tfds
import tensorflow as tf
ds, ds_info = tfds.load('cats_vs_dogs', split='train', with_info=True)
normalization_layer = tf.keras.layers.Rescaling(1./255)
def resize_inputs(data):
images, labels = data['image'], data['label']
images = tf.image.resize(normalization_layer(images),[64, 64], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return images, labels
ds = ds.map(resize_inputs).batch(64)
ann = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, kernel_size=3, input_shape=(64, 64, 3)),
tf.keras.layers.Flatten(), # Flaten 3d to 1d
tf.keras.layers.Dense(200, activation='relu'), # more hidden layer gives better perf
tf.keras.layers.Dense(100, activation='relu'), # more hidden layer gives better perf
tf.keras.layers.Dense(50, activation='relu'), # more hidden layer gives better perf
tf.keras.layers.Dense(1, activation='sigmoid')
])
ann.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])
ann.fit(ds, epochs=10)
Epoch 1/10
364/364 [==============================] - 58s 140ms/step - loss: 0.8692 - accuracy: 0.5902
Epoch 2/10
364/364 [==============================] - 51s 141ms/step - loss: 0.6155 - accuracy: 0.6559
Epoch 3/10
364/364 [==============================] - 51s 141ms/step - loss: 0.5708 - accuracy: 0.7009
Epoch 4/10
364/364 [==============================] - 51s 140ms/step - loss: 0.5447 - accuracy: 0.7262
...
你可以試驗這個例子,找出最適合你的激活函式、損失函式和輸出節點數量的組合。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408857.html
標籤:
上一篇:使用tensorflowio在訓練/測驗子集中拆分自定義二進制資料集
下一篇:交叉熵驗證損失是一條直線
