写一段Python代码,关于深度学习。
时间: 2023-03-14 17:53:14 浏览: 86
答:
import tensorflow as tf
mnist = tf.keras.datasets.mnist#Loads the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()#Normalizes the data
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)#Defines a Sequential model
model = tf.keras.models.Sequential()#Adds an input layer
model.add(tf.keras.layers.Flatten())#Adds two hidden layers
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))#Adds an output layer
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))#Compiles the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])#Trains the model
model.fit(x_train, y_train, epochs=3)#Evaluates the model
val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)
阅读全文