详解TensorFlow的 tf.metrics.mean_absolute_error 函数:计算平均绝对误差

  • Post category:Python

TensorFlow的tf.metrics.mean_absolute_error函数

tf.metrics.mean_absolute_error是TensorFlow中一个用于计算平均绝对误差(Mean Absolute Error,MAE)的函数。MAE是评估真实值和预测值之间差异的一种方法,它计算预测值和真实值之间的绝对误差的平均值。MAE的值越小,表示预测的结果与真实值越接近。

使用方法

tf.metrics.mean_absolute_error接受两个参数:labelspredictions,分别表示真实值和预测值。该函数返回两个Tensor:一个用于更新MAE的变量,另一个用于计算MAE的张量。我们使用tf.metrics.mean_absolute_error可以计算实例的平均绝对误差,并在训练过程中使用它来监测模型的性能指标。

例如,假设有一个回归问题,我们有一组输入x和对应的目标值y(真实值)。定义如下的线性回归模型:

import tensorflow as tf

x = tf.placeholder(tf.float32, name='input')
y = tf.placeholder(tf.float32, name='target')

w = tf.Variable(tf.zeros([1]), name='weights')
b = tf.Variable(tf.zeros([1]), name='bias')

y_pred = tf.multiply(x, w) + b

在模型训练过程中,我们将使用tf.metrics.mean_absolute_error函数来计算平均绝对误差。初始化平均绝对误差的变量:

mae, update_mae = tf.metrics.mean_absolute_error(labels=y, predictions=y_pred)

使用update_mae计算MAE并获取MAE的值:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    for i in range(num_iterations):
        _, _, mae_val = sess.run([train_op, update_op, mae],
                                 feed_dict={x: input_data, y: target_data})

其中,train_op是训练模型的操作,update_op是更新平均绝对误差的操作。在循环中不断执行train_opupdate_op操作,每个轮次结束后,使用mae_val计算MAE值。

实例说明

下面给出两个实例说明:

实例1

假设我们有一个数据集,其中包含100个样本。每个样本由2个特征和1个目标值组成。我们可以使用如下的代码生成数据集:

import numpy as np

np.random.seed(0)

num_samples = 100
num_features = 2
x = np.random.randn(num_samples, num_features)
w = np.array([2.0, 3.0])
b = 4
y = np.dot(x, w) + b + np.random.randn(num_samples)

我们可以使用如下的代码来训练一个线性回归模型,计算平均绝对误差:

import tensorflow as tf

x_ph = tf.placeholder(tf.float32, shape=[None, num_features], name='x')
y_ph = tf.placeholder(tf.float32, shape=[None], name='y')

w = tf.Variable(tf.zeros([num_features]), name='w')
b = tf.Variable(tf.zeros([1]), name='b')

y_pred = tf.reduce_sum(tf.multiply(x_ph, w), axis=1) + b

mae, update_mae = tf.metrics.mean_absolute_error(labels=y_ph, predictions=y_pred)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(loss)

batch_size = 10
num_batches = num_samples // batch_size
num_epochs = 50

init_op = tf.global_variables_initializer()
init_local_op = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    sess.run(init_local_op)
    for i in range(num_epochs):
        for j in range(num_batches):
            batch_x = x[j*batch_size:(j+1)*batch_size]
            batch_y = y[j*batch_size:(j+1)*batch_size]
            _, _, mae_val = sess.run([train_op, update_op, mae], feed_dict={x_ph: batch_x, y_ph: batch_y})
        print("Epoch %d: MAE=%.4f" % (i+1, mae_val))

在训练结束后,我们可以计算测试集上的MAE来评估模型的性能。

实例2

假设我们有一个二分类问题,需要预测患者是否患有糖尿病。我们使用如下的代码生成模拟数据:

import numpy as np

np.random.seed(0)

num_samples = 1000
num_features = 8
x = np.random.randn(num_samples, num_features)
w = np.array([2.0, 3.0, 1.0, 4.0, 5.0, 6.0, 7.0, 8.0])
b = -5.0
y = np.dot(x, w) + b + np.random.randn(num_samples)
y = (y > 0).astype(int)

我们可以使用如下的代码来训练一个逻辑回归模型,计算平均绝对误差:

import tensorflow as tf

x_ph = tf.placeholder(tf.float32, shape=[None, num_features], name='x')
y_ph = tf.placeholder(tf.int32, shape=[None], name='y')

w = tf.Variable(tf.zeros([num_features]), name='w')
b = tf.Variable(tf.zeros([1]), name='b')

y_logits = tf.reduce_sum(tf.multiply(x_ph, w), axis=1) + b
y_pred = tf.nn.sigmoid(y_logits)

mae, update_mae = tf.metrics.mean_absolute_error(labels=y_ph, predictions=y_pred)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(loss)

batch_size = 10
num_batches = num_samples // batch_size
num_epochs = 50

init_op = tf.global_variables_initializer()
init_local_op = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    sess.run(init_local_op)
    for i in range(num_epochs):
        for j in range(num_batches):
            batch_x = x[j*batch_size:(j+1)*batch_size]
            batch_y = y[j*batch_size:(j+1)*batch_size]
            _, _, mae_val = sess.run([train_op, update_op, mae], feed_dict={x_ph: batch_x, y_ph: batch_y})
        print("Epoch %d: MAE=%.4f" % (i+1, mae_val))

在训练结束后,我们可以计算测试集上的MAE来评估模型的性能。注意,在分类问题中,MAE可能不是最佳的性能指标。更适合分类问题的性能指标是准确率、召回率、F1-score等。