【问题标题】:Tensorflow fails to evaluate gradientsTensorflow 无法评估梯度
【发布时间】:2017-01-19 13:35:04
【问题描述】:

我在尝试将 tensorflow 的 boolean_mask 函数与梯度优化器(Windows x64 和 Anaconda 3.5 上的 tensorflow 版本 0.12.1)一起使用时遇到问题。

import tensorflow as tf
import numpy as np

# Test data
x_dat = np.arange(1, 13).reshape(4, 3).astype(np.float32)
y_dat = np.arange(1, 9).reshape(4, 2).astype(np.float32) 
map_x_on_y = np.array([[True, False], [False, False], [ True, True]])

# Test data
x = tf.placeholder(tf.float32, [None, 3])
y_ = tf.placeholder(tf.float32, [None, 2])

# Model: Take the column product of the elements of x using the rows of               
# map_x_on_y as a filter. 
# The two columns of map_x_on_y give the two columns of y

transpose_of_x = tf.transpose(x)
fn = lambda t: tf.reduce_prod(tf.boolean_mask(transpose_of_x, t), axis=0)
y = tf.stack([fn(i) for i in tf.unstack(map_x_on_y, axis=1)])

# Train the model
sum_of_squared_errors = tf.reduce_mean(tf.square(y_-y)) 
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(sum_of_squared_errors)
init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)
    feed_dict ={x: x_dat, y_: y_dat} 
    for i in range(10):   
        sess.run([train_step], feed_dict)

我收到以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-108-cc039c0c3ed9> in <module>()
    21 # Train the model
    22 sum_of_squared_errors = tf.reduce_mean(tf.square(y_-y))
    ---> 23 train_step =                 tf.train.GradientDescentOptimizer(0.1).minimize(sum_of_squared_errors)
    24 init = tf.global_variables_initializer()
    25 

C:\Anaconda3\lib\site-packages\tensorflow\python\training\optimizer.py in minimize(self, loss, global_step, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, name, grad_loss)
    274           "No gradients provided for any variable, check your graph for ops"
    275           " that do not support gradients, between variables %s and loss %s." %
    --> 276           ([str(v) for _, v in grads_and_vars], loss))
    277 
    278     return self.apply_gradients(grads_and_vars, global_step=global_step,

    ValueError: No gradients provided for any variable, check your graph for        ops that do not support gradients, between variables ['Tensor("Variable/read:0", shape=(3, 10), dtype=bool)', 'Tensor("Variable_1/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_2/read:0", shape=(3, 10), dtype=bool)', 'Tensor("Variable_3/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_4/read:0", shape=(3, 10), dtype=float64)', 'Tensor("Variable_5/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_6/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_7/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_8/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_9/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_10/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_11/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_12/read:0", shape=(3, 5), dtype=float64)', 'Tensor("Variable_13/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_14/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_15/read:0", shape=(5, 3), dtype=int32)', 'Tensor("Variable_16/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_17/read:0", shape=(5, 3), dtype=int32)', 'Tensor("Variable_18/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_19/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_20/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_21/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_22/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_23/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_24/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_25/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_26/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_27/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_28/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_29/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_30/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_31/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_32/read:0", shape=(3, 2), dtype=float32)', 'Tensor("Variable_33/read:0", shape=(3, 5), dtype=float32)', 'Tensor("Variable_34/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_35/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_36/read:0", shape=(3, 2), dtype=bool)', 'Tensor("Variable_37/read:0", shape=(5, 3), dtype=float32)', 'Tensor("Variable_38/read:0", shape=(3, 2), dtype=bool)'] and loss Tensor("Mean_1:0", shape=(), dtype=float32).

仅使用变量时,该操作似乎工作正常:

import tensorflow as tf
import numpy as np

x = tf.Variable(np.arange(1, 13).reshape(4, 3).astype(np.float32))
map_x_on_y = np.array([[True, False], [False, False], [ True, True]])

transpose_of_x = tf.transpose(x)
fn = lambda t: tf.reduce_prod(tf.boolean_mask(transpose_of_x, t), axis=0)
y = tf.stack([fn(i) for i in tf.unstack(map_x_on_y, axis=1)])

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(x.eval())
    print(map_x_on_y)
    print(y.eval())

产生正确的输出:

[[  1.   2.   3.]
 [  4.   5.   6.]
 [  7.   8.   9.]
 [ 10.  11.  12.]]
[[ True False]
 [False False]
 [ True  True]]
[[   3.   24.   63.  120.]
 [   3.    6.    9.   12.]]

作为 tensorflow 的初学者,非常感谢您的帮助,定义操作的梯度评估出了什么问题?

最佳巴斯蒂安

【问题讨论】:

    标签: python python-3.x tensorflow


    【解决方案1】:

    我设法解决了这个问题。我忘了在例子中添加一个参数来优化。

    import tensorflow as tf
    import numpy as np
    
    # Test data
    x_dat = np.arange(1, 13).reshape(4, 3).astype(np.float32)
    y_dat = np.arange(1, 9).reshape(4, 2).astype(np.float32)
    
    # Test data
    x = tf.placeholder(tf.float32, [None, 3])
    y_ = tf.placeholder(tf.float32, [None, 2])
    
    parameter = tf.Variable(tf.ones([3, 1], dtype=tf.float32))
    
    # Model: Take the column product of the elements of x using the rows of map_x_on_y as a filter. 
    # Map for projecting x on y
    map_x_on_y = tf.Variable(np.array([[True, False], [False, False], [ True, True]]))
    
    intermediate = tf.map_fn(lambda t: tf.div(t[0], tf.add(t[1], t[0])),
                           (tf.transpose(x), parameter), dtype=tf.float32)
    
    fn = lambda t: tf.reduce_prod(tf.boolean_mask(intermediate, t), axis=0)
    y = tf.transpose(tf.stack([fn(i) for i in tf.unstack(map_x_on_y, axis=1)]))
    
    
    # Train the model
    sum_of_squared_errors = tf.reduce_mean(tf.square(y-y_)) 
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(sum_of_squared_errors)
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:   
        sess.run(init)
        feed_dict ={x: x_dat, y_: y_dat} 
        for i in range(1):   
            sess.run([train_step], feed_dict)       
            print(sess.run(intermediate, feed_dict))
            print(sess.run(y, feed_dict))
    

    这产生了正确的输出:

    [[ 0.50838345  0.80531198  0.87862223  0.91182482]
     [ 0.66666669  0.83333331  0.8888889   0.91666669]
     [ 0.76236677  0.86516243  0.90587789  0.92770731]]
    [[ 0.38757464  0.76236677]
     [ 0.69672567  0.86516243]
     [ 0.79592443  0.90587789]
     [ 0.84590656  0.92770731]]
    

    不过,我仍然收到以下警告:

    C:\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py:91: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
      "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
    

    这是值得关注的警告吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多