首页 \ 问答 \ Tensorflow无法评估渐变(Tensorflow fails to evaluate gradients)

Tensorflow无法评估渐变(Tensorflow fails to evaluate gradients)

当我尝试使用tensorflow的boolean_mask函数和渐变优化器(在带有Anaconda 3.5的Windows x64上的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.]]

作为张量流动的初学者,我将非常感谢您的帮助,对定义的操作进行梯度评估时会出现什么问题?

最好的巴斯蒂安


I have a problem, when trying to use tensorflow's boolean_mask function together with the gradient optimizer (tensorflow version 0.12.1 on Windows x64 with Anaconda 3.5).

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)

I get the following error message:

---------------------------------------------------------------------------
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).

The operation seem to work fine, when using only variables:

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

Producing the correct output:

[[  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.]]

Being a beginner in tensorflow, I would highly appreciate your help, what is going wrong with the gradient evaluation of the defined operation?

Best Bastian


原文:https://stackoverflow.com/questions/41743093
更新时间:2023-07-30 13:07

最满意答案

这是您正在尝试在A帧示例之一( https://aframe.io/aframe/examples/boilerplate/hello-world/ )上完成的操作:

document.querySelector("[camera]").getObject3D('camera')

例


This is what you are trying to accomplish on one of the A-Frame examples (https://aframe.io/aframe/examples/boilerplate/hello-world/):

document.querySelector("[camera]").getObject3D('camera')

example

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)