我正在尝试在TensorFlow上实现自定义丢失函数来计算循环均方误差损失。
我正在考虑真实值和预测值的差异,y和yPredict都是矢量(1D)。我正在添加另一个变量2 * j * pi,其中j的范围是-20到20.但是,在计算这行代码时似乎存在问题。
err_matrix = tf.Variable(np.zeros((np.shape(yPredict)[0], np.shape(yPredict)[1], k.shape[0])))
以下是错误消息:
ValueError: slice index 1 of dimension 1 out of bounds. for 'strided_slice' (op: 'StridedSlice') with input shapes: [100,1,41], [2], [2], [2] and with computed input tensors: input[1] = <0 1>, input[2] = <0 2>, input[3] = <1 1>.
这是完整函数和函数调用以及它如何链接到优化器。
功能:
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())
有人可以帮帮我吗?谢谢!