使用内置方法进行训练和评估
利用minist数据举例:
#定义模型
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
#加载数据以及处理数据
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
#原始数据[6000,28,28]
x_train = x_train.reshape(60000, 784).astype("float32") / 255.
x_test = x_test.reshape(10000, 784).astype("float32") / 255.
y_train = y_train.astype("float32")
y_test = y_test.astype("float32")
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
#指定优化,计算损失的方法
model.compile(
optimizer=keras.optimizers.RMSprop(),
loss=keras.losses.SparseCategoricalCrossentropy(),
metrics=[keras.metrics.SparseCategoricalAccuracy()],
)#metrics参数为列表时,可以设置多个评价函数
#训练模型
history = model.fit(x_train,y_train,batch_size=64,epochs=2,validation_data=(x_val, y_val))
#评估模型
results = model.evaluate(x_test, y_test, batch_size=128)
print("test loss, test acc:", results)
如果需要自定义metric方法,可按一下步骤定义类:
class CategoricalTruePositives(keras.metrics.Metric):
def __init__(self, name="categorical_true_positives", **kwargs):
super(CategoricalTruePositives, self).__init__(name=name, **kwargs)
pass
def update_state(self, y_true, y_pred, sample_weight=None):#使用目标 y_true 和模型预测 y_pred 更新状态变量。
pass
def result(self):#使用状态变量来计算最终结果。
pass
def reset_states(self):#用于重新初始化指标的状态
pass
自动截取从训练集中选取验证集:
model = get_compiled_model()
model.fit(x_train, y_train, batch_size=64, validation_split=0.2, epochs=1)#选取20%的训练集作为验证集
tf.data.Dataset:
(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.
y_train = y_train.astype("float32")
y_test = y_test.astype("float32")
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(64)
test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_dataset = test_dataset.batch(64)
#fit中validation_data=可用于设置验证集
#validation_steps=10,每训练10次验证一次
model.fit(train_dataset, epochs=3)
#print("Evaluate")
result = model.evaluate(test_dataset)
对于多输出以及多输入情况的网络需要定义多个loss方法和metrics方法,例如:
model.compile(
optimizer=keras.optimizers.RMSprop(1e-3),
loss={
"score_output": keras.losses.MeanSquaredError(),
"class_output": keras.losses.CategoricalCrossentropy(),
},
metrics={
"score_output": [
keras.metrics.MeanAbsolutePercentageError(),
keras.metrics.MeanAbsoluteError(),
],
"class_output": [keras.metrics.CategoricalAccuracy()],
},
#为score_output赋予两倍的权重
loss_weights={"score_output": 2.0, "class_output": 1.0},
)