Question: import numpy as np import matplotlib.pyplot as plt from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense from keras.utils import to_categorical #
import numpy as np import matplotlib.pyplot as plt from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense from keras.utils import to_categorical # Load the MNIST dataset (X_train, y_train), (X_test, y_test) = mnist.load_data() # Normalize the input data X_train = X_train.astype('float32') / 255.0 X_test = X_test.astype('float32') / 255.0 # Flatten the input data X_train = X_train.reshape(X_train.shape[0], -1) X_test = X_test.reshape(X_test.shape[0], -1) # One-hot encode the target variable y_train = to_categorical(y_train) y_test = to_categorical(y_test) # Define the neural network architecture model = Sequential() model.add(Dense(128, input_dim=X_train.shape[1], activation='relu')) model.add(Dense(10, activation='softmax')) # Compile the model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train the model history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=32) # Plot the accuracy and loss results plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.plot(history.history['accuracy'], label='train') plt.plot(history.history['val_accuracy'], label='test') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend() plt.subplot(1, 2, 2) plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.show() can this code work in python language
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
