本周课程的主要内容有:
-
即刻执行
C,Java,Python都用了即刻求值.
tensorflow2 默认使用了eager execution
-
模型的训练与评估
训练数据的加载:
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train = x_train.reshape(60000, 784).astype('float32') / 255 x_test = x_test.reshape(10000, 784).astype('float32') / 255
模型的构造:
inputs = keras.Input(shape=(784,), name="digits") x = layers.Dense(512, activation="relu", name="dense_1")(inputs) x = layers.Dense(128, activation="relu", name="dense_2")(x) outputs = layers.Dense(10, activation="softmax", name="predictions")(x) model = keras.Model(inputs=inputs, outputs=outputs)
编译:
model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["sparse_categorical_accuracy"], )
训练模型:
history = model.fit( x_train, y_train, batch_size=1024, epochs=10, validation_data=(x_val, y_val), )