python - NaN loss output when training DNN with Keras - Stack Overflow
I am trying to do an exercise from Hands-On ML by Aurelien Geron. However when I tried using my own solution and copy and pasting the solution from the answer key, I keep getting this outcome that is not supposed to happen. The loss and val_loss at each epoch is nan and the accuracy is not increasing at all. This exercise is to try and build a DNN with 20 hidden layers on the CIFAR10 dataset.
Here is the code:
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
X_val, y_val = X_train[:5000], y_train[:5000]
X_train, y_train = X_train[5000:], y_train[5000:]
tf.random.set_seed(42)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=[32,32,3]))
model.add(tf.keras.layers.Flatten())
## 20 Hidden layers
for _ in range(20):
model.add(tf.keras.layers.Dense(100, activation='swish',
kernel_initializer='he_normal'))
# Output layer
model.add(tf.keras.layers.Dense(10, activation='softmax'))
optimizer = tf.keras.optimizers.Nadam(learning_rate=5e-6)
modelpile(optimizer=opimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
import datetime
early_stopping = tf.keras.callbacks.EarlyStopping(patience=20,
restore_best_weights=True)
log_dir = "logs/my_cifar10_model/" + datetime.datetime.now().strftime("%Y%m%d-
%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir)
model.fit(X_train, y_train, epochs=100, callbacks=[early_stopping,
tensorboard_callback],
validation_data=(X_val, y_val))
And here is the output:
Epoch 1/100
1407/1407 ━━━━━━━━━━━━━━━━━━━━ 8s 3ms/step - accuracy: 0.1005 - loss: nan -
val_accuracy: 0.0996 - val_loss: nan
Epoch 2/100
1407/1407 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.1007 - loss: nan -
val_accuracy: 0.0996 - val_loss: nan
Epoch 3/100
1407/1407 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.1007 - loss: nan -
val_accuracy: 0.0996 - val_loss: nan
...
Epoch 20/100
1407/1407 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.1007 - loss: nan -
val_accuracy: 0.0996 - val_loss: nan
Epoch 21/100
1407/1407 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.1007 - loss: nan -
val_accuracy: 0.0996 - val_loss: nan
<keras.src.callbacks.history.History at 0x7801cde19fa0>
There are no actually errors rasied by TF or python.
How to resolve this?
最新文章
- 评论:Android - 谷歌的一剂药-搜狐滚动
- html - HTML5 Canvas: How to draw on Canvas from Java or C++ server? - Stack Overflow
- reactjs - Why getting error 'Objects are not valid as a React child' in turborepo ui tests? - Stack Overflow
- visual studio code - Why am I getting “Type annotations can only be used in TypeScript files” with tsx files? - Stack Overflow
- tensorflow - How can I use training data into my custom metric? - Stack Overflow
- javascript - TypeError with firebase authentication - Stack Overflow
- python - ModuleNotFoundError: No module named 'llama_index.text_splitter' - Stack Overflow
- Querying the Google Patent Data on Big Query processes way too much data - Stack Overflow
- swiftui - Swift Mocking a throwing function - Stack Overflow
- Keeping Data In Denormalized NoSql Databases (CassandraScyllaDb) Tables In Sync? - Stack Overflow
- spring boot - Hibernate search backend elastic search with multiple data bases - Stack Overflow
- php - How to return blob in Joomla 5 API - Stack Overflow
- android - How call Authenticator.authenticate with custom respond in okhttp? - Stack Overflow
- Angular lib with route - Stack Overflow
- php - Laravel Defer on API requests - Stack Overflow
- React Native Build Error on Windows: "EMFILE: too many open files" During `assembleRelease` - Stack Overflow
- javascript - Save andor Apply Background Image on click - Stack Overflow