关于3-12中的tf.reduce_mean(keras.losses.mean_squared_error(y_batch,y_pred))
来源:3-12 tf.GradientTape与tf.keras结合使用
慕田峪1171714
2019-06-25
y_batch和y_pred长度是32,y_batch的shape是(32,),而y_pred的shape却是(32,1),这样使用
keras.losses.mean_squared_error(y_batch,y_pred)并不是对应元素相减然后平方然后平均,而是y_pred中的每一个元素都会和y_batch全部元素进行mse,这个地方很奇怪,损失函数不应该是预测值与对应的真值相减然后平方然后平均吗?我不理解这里的处理
写回答
1回答
-
同学你好,感谢你的细心发现,应该是程序出了问题,做一下修改把y_pred的第二个维度给去掉就可以了
# 1. batch 遍历训练集 metric # 1.1 自动求导 # 2. epoch结束 验证集 metric epochs = 100 batch_size = 32 steps_per_epoch = len(x_train_scaled) // batch_size optimizer = keras.optimizers.SGD() metric = keras.metrics.MeanSquaredError() def random_batch(x, y, batch_size=32): idx = np.random.randint(0, len(x), size=batch_size) return x[idx], y[idx] model = keras.models.Sequential([ keras.layers.Dense(30, activation='relu', input_shape=x_train.shape[1:]), keras.layers.Dense(1), ]) for epoch in range(epochs): metric.reset_states() for step in range(steps_per_epoch): x_batch, y_batch = random_batch(x_train_scaled, y_train, batch_size) with tf.GradientTape() as tape: y_pred = model(x_batch) y_pred = tf.squeeze(y_pred, 1) loss = keras.losses.mean_squared_error(y_batch, y_pred) metric(y_batch, y_pred) grads = tape.gradient(loss, model.variables) grads_and_vars = zip(grads, model.variables) optimizer.apply_gradients(grads_and_vars) print("\rEpoch", epoch, " train mse:", metric.result().numpy(), end="") y_valid_pred = model(x_valid_scaled) y_valid_pred = tf.squeeze(y_valid_pred, 1) valid_loss = keras.losses.mean_squared_error(y_valid_pred, y_valid) print("\t", "valid mse: ", valid_loss.numpy())
30
相似问题