Tensorflow中数组操作中定义的操作是否已定义梯度? [英] Do the operations defined in array ops in Tensorflow have gradient defined?

查看:272
本文介绍了Tensorflow中数组操作中定义的操作是否已定义梯度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道此链接,定义了渐变.我问是因为我正在实现自定义损失函数,当我运行它时,我总是会遇到此错误:

I want to know whether the tensorflow operations in this link, have a gradient defined. I am asking because I am implementing a custom loss function and when I run it I always have this error :

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函数:

This is my custom Loss function:

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()])

谢谢.

推荐答案

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

Most ops have a defined gradient. There are some ops for which a gradient is not defined and the error message you get gives you some examples.

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

Having said that, there are couple of mistakes I see in your code :

  1. final_loss被定义为tf.constant,但是您正在尝试增加它.
  2. 您正在从range
  3. 中获取元组
  4. error被定义为tf.constant,但是您正在尝试增加它.
  5. 请勿在batch_size上以这种方式使用for循环.而是使用TensorFlow函数直接处理batch尺寸.这样,您就可以扩散节点.
  6. 您编写代码的方式使我认为您将TensorFlow视为纯python.它不是.您定义图,然后在会话中执行它.因此,在函数中使用TF函数仅定义计算.
  1. final_loss is defined as tf.constant, but you are trying to increment it.
  2. You are taking a tuple from range
  3. error is defined as tf.constant, but you are trying to increment it.
  4. Don't use for loop in this way over batch_size. Instead use TensorFlow functions to handle batch dimension directly. This way you are just proliferating your nodes.
  5. The way you have written your code makes me think that you're thinking of TensorFlow as pure python. It is not. You define the graph and then you execute it inside a session. So, in the function use TF functions to just define the computations.

这篇关于Tensorflow中数组操作中定义的操作是否已定义梯度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆