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
最满意答案
这是您正在尝试在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')
相关问答
更多-
您可以使用box3.setFromObject(Object3D)来确定对象的边界框以及object3D的中心。 看看Threejs:计算旋转文本的边界框和Three.js如何获取网格的位置? 。 我想他们会帮助你。 You can use box3.setFromObject(Object3D) to figure out the bounding box of your object and from that the center of your object3D. Take a look at Th ...
-
你可以使用这个功能: var alwaysLookAtCamera = function () { var worldPos = new THREE.Vector3(); if (+THREE.REVISION > 85) { return function (object, camera) { object.onBeforeRender = function() { worldPos.setFromMatrixPosi ...
-
THREE.Quaterion对象包含插值方法。 因此,计算原始面法线的四元数值,即从setFromRotationMatrix(ma)到达此处的四元数,然后重复应用THREE.Quaterion.slerp()以获取中间值,这可以应用于cube.quaternion The THREE.Quaterion object contains methods for interpolation. So calculate the quaternion value of the original face nor ...
-
Aframe,无法获取相机THREE.js object3D的引用(Aframe, Can not get a reference to the cameras THREE.js object3D)[2022-08-05]
这是您正在尝试在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/boilerp ... -
如果将对象a添加到另一个对象b( THREE.Group或任何其他THREE.Object3D ),则a的位置总是被解释为与对象b的原点和变换相关。 这是拥有这个层次结构的全部要点(否则当它旋转时,对象不能与父对象一起旋转,并且你的旋转木马会很无聊)。 你想要的是在世界空间中指定坐标(“无论旋转旋转木马的方式,我都希望这匹马的确切位置”),并将这个位置重新解释为相对于旋转木马的位置,去圆。 您可以使用worldToLocal功能在three.js中执行此worldToLocal 。 所以在你的例子中: va ...
-
您应该能够简单地将spinnning sprite 添加到使用add()函数: //create an empty 'container'/Object3D var spinningSprites = new Object3D(); //add elements to it: for(var i = 0 ; i < numSprites; i++) spinningSprites.add(yourParticleObjectInstsance); //lastly add the whole contain ...
-
自定义网格上的光线投射在我身边很好用,所以请看看我的RotateTool 。 它正在使用叠加场景,我尝试在自定义网格上设置透明度,工作正常。 在下面的代码中,如果我用sceneAfter替换叠加,它也可以正常工作,我可以使用raycaster检测鼠标点击Gizmo: createGizmo (center, euler, size, radius, color, range, axis) { var material = new GizmoMaterial({ color: color ...
-
如果您的父Object3D具有多个子网格物体,并且您想通过光线投射选择父级,则可以执行以下操作: 将父对象添加到对象数组: var objects = []; ... objects.push( root_parent_object ); 向每个子对象添加指向根父对象的指针: child.userData.parent = root_parent_object; 将递归标志传递给intersectObjects() 。 var intersects = raycaster.intersectObject ...
-
问题最终是在THREE.js视野中代表垂直观察区域。 我一直认为像Maya和其他应用程序一样使用视场作为水平观看区域。 将我通过纵横比获得的FOV乘以正确的水平视野,这导致摄像机距离为〜92。 The issue ended up being that in THREE.js field of view represents the vertical viewing area. I had been assuming that THREE like Maya and other applications u ...
-
使用lookAtWorld添加 THREE.Object3D.prototype.lookAtWorld = function( vector ) { this.parent.worldToLocal( vector ); this.lookAt( vector ); } 或者像以下一样破解它: function animate() { /* ... */ var vector = plane.parent.worldToLocal( camera.getWorldPositi ...