Tensorflow中数组操作中定义的操作是否已定义梯度?

时间:2018-09-10 15:28:12

标签: tensorflow keras loss-function gradients

我想知道this link中的张量流操作是否定义了梯度。我问是因为我正在实现自定义损失函数,当我运行它时,我总是会遇到此错误:

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

这是我自定义的Loss函数:

def calculate_additional_loss(y_true,y_pred):
#additional loss
x_decoded_normalized = original_dim* y_pred
#y_true = K.print_tensor(y_true, message='y_true = ')
#y_pred = K.print_tensor(y_pred, message='y_pred = ')
error = tf.constant(0, dtype= tf.float32)
additional_loss= tf.constant(0, dtype= tf.float32)
final_loss= tf.constant(0, dtype= tf.float32)
for k in range(batch_size):
    #add padding
    reshaped_elem_1 = K.reshape(x_decoded_normalized[k], [DIM,DIM])

    a = K.reshape(reshaped_elem_1[:,DIM-1], [DIM,1])
    b = K.reshape(reshaped_elem_1[:,1], [DIM,1])

    reshaped_elem_1 = tf.concat ([b,reshaped_elem_1], axis= 1)
    reshaped_elem_1 = tf.concat ([reshaped_elem_1,a], axis= 1)

    c= K.reshape(reshaped_elem_1[DIM-1,:], [1,DIM+2])
    d= K.reshape(reshaped_elem_1[1,:], [1,DIM+2])
    reshaped_elem_1 = tf.concat ([d,reshaped_elem_1],axis=0)
    reshaped_elem_1 = tf.concat ([reshaped_elem_1,c],axis=0)

    for (i,j) in range(reshaped_elem_1.shape[0],reshaped_elem_1.shape[1]):
        error = tf.add(error, tf.pow((reshaped_elem_1[i,j]- 
                       reshaped_elem_1[i,j+1]),-2), 
                       tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i,j- 
                       1]),-2), tf.pow((reshaped_elem_1[i,j]- 
                       reshaped_elem_1[i-1,j]),-2), 
                       tf.pow((reshaped_elem_1[i,j]-reshaped_elem_1[i+1,j]),-2))
    additional_loss = tf.add(additional_loss, tf.divide(error, original_dim))
final_loss += tf.divide(additional_loss, batch_size)
print('final_loss', final_loss)
return final_loss

这就是我所说的:

models = (encoder, decoder)
additional_loss = calculate_additional_loss(inputs,outputs)
vae.add_loss(additional_loss)
vae.compile(optimizer='adam')
vae.summary()

plot_model(vae,to_file='vae_mlp.png',show_shapes=True)
vae.fit(x_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, None), verbose = 1, callbacks=[CustomMetrics()])

谢谢。

1 个答案:

答案 0 :(得分:1)

大多数操作具有定义的渐变。有些操作未定义渐变,错误消息给出了一些示例。

话虽如此,我在您的代码中看到了几个错误:

  1. final_loss被定义为tf.constant,但是您正在尝试增加它。
  2. 您正在从range中获取元组
  3. error被定义为tf.constant,但是您正在尝试增加它。
  4. 请勿在{{1​​}}上以这种方式使用for循环。而是使用TensorFlow函数直接处理batch_size维度。这样,您只是在扩展节点。
  5. 您编写代码的方式使我认为您将TensorFlow视为纯python。它不是。您定义图,然后在会话中执行它。因此,在函数中使用TF函数仅定义计算。