在tens中实现自定义mse损失函数

2024-06-11 13:59:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在TensorFlow上实现一个定制的损失函数来计算循环均方误差损失。在

我取真实值和预测值的差值,y和y预测值都是向量(1D)。我添加了另一个变量2*j*pi,其中j的范围是-20到20。然而,在计算这一行代码时似乎有一个问题。在

err_matrix = tf.Variable(np.zeros((np.shape(yPredict)[0], np.shape(yPredict)[1], k.shape[0])))

以下是错误消息:

^{pr2}$

这是完整的函数和函数调用,以及它如何链接到优化器。在

功能:

def wmse(yPredict,y):
    k = tf.constant(np.array(range(-20, 21)))
    err_matrix = tf.Variable(np.zeros((np.shape(yPredict)[0], np.shape(yPredict)[1], k.shape[0])))
    for j in range(1, k.shape[0]):
        err_matrix[:, j] += tf.subtract(tf.subtract(yPredict,y), tf.constant(2*j*tf.constant(np.pi)))
    errs = tf.reduce_min(err_matrix, axis=1)
    std_CNN_errors = tf.sqrt(tf.reduce_mean(tf.square(errs)))
    return std_CNN_errors

函数调用:

cost_function = wmse(network_outputs, outputs)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost_function, var_list=tf.trainable_variables())

有人能帮我做这个吗?谢谢!在


Tags: 函数tfnppizerosvariablematrixerr