简体   繁体   English

Tensorflow:最小化int64数据上的L2损失而不转换为float32,因为转换给出“无梯度”错误

[英]Tensorflow: minimize L2 loss on int64 data without casting to float32 because casting gives “no gradients” error

I am running into the error:"No gradients provided for any variable" when I cast my tensor to float32. 我遇到错误:“当我将张量转换为float32时,没有为任何变量提供渐变”。 But without casting, I get the error that the expected type is float and not int. 但是没有强制转换,我得到的错误是期望的类型是float而不是int。 So, either way, I can't seem to find a way to proceed... 所以无论如何,我似乎无法找到继续前进的方法......

In my setting, I am trying to minimize the squared error of the difference of two tensors. 在我的设置中,我试图最小化两个张量的差异的平方误差。

softmax_w = tf.Variable(tf.zeros([SIZE_LSTM_UNITS, NUM_CLASSES], dtype=tf.float32))
softmax_b = tf.Variable(tf.zeros([NUM_CLASSES], dtype=tf.float32))
logits = tf.matmul(out, softmax_w) + softmax_b

If I compute the loss with casting as below: 如果我用铸造计算损失如下:

predDiff = tf.cast(tf.sub(tf.arg_max(logits, 1), tf.arg_max(train_labels, 1)), tf.float32)
l2loss = tf.nn.l2_loss(predDiff)
trainStep = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(l2loss)

where, logits and train_labels are 1-hot vectors, then I get the following error: 其中,logits和train_labels是1-hot向量,然后我得到以下错误:

trainStep = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(l2loss) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 198, in minimize name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 309, in apply_gradients (converted_grads_and_vars,)) ValueError: No gradients provided for any variable: ((None, < tensorflow.python.ops.variables.Variable object at 0x7f2c7363bf90 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce284e9d0 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce284e510 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce26cf050 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce26cf450 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce2c9d510 >), (None, < tensorflow.python.ops.variables.Variable object at 0x7f2ce287ae90 >)) trainStep = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(l2loss)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py”,第198行,最小化名称= name)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py”,第309行,在apply_gradients(converted_grads_and_vars,)中)ValueError:没有为任何变量提供渐变:( (无,<tensorflow.python.ops.variables.Variable对象位于0x7f2c7363bf90>),(无,<tensorflow.python.ops.variables.Variable对象位于0x7f2ce284e9d0>),(无,<tensorflow.python.ops.variables。变量对象位于0x7f2ce284e510>),(无,<tensorflow.python.ops.variables.Variable对象位于0x7f2ce26cf050>),(无,<tensorflow.python.ops.variables.Variable对象位于0x7f2ce26cf450>),(无,<tensorflow .python.ops.variables.Variable对象位于0x7f2ce2c9d510>),(无,<tensorflow.python.ops.variables.Variable对象位于0x7f2ce287ae90>))

Instead, if I compute the loss without casting as below: 相反,如果我计算损失而不进行铸造,如下所示:

predDiff = tf.sub(tf.arg_max(logits, 1), tf.arg_max(train_labels, 1))

then, I get the following error: 然后,我收到以下错误:

trainStep = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(l2loss) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 196, in minimize grad_loss=grad_loss) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 238, in compute_gradients self._assert_valid_dtypes([loss]) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 379, in _assert_valid_dtypes dtype, t.name, [v for v in valid_dtypes])) ValueError: Invalid type tf.int64 for L2Loss:0, expected: [tf.float32, tf.float64, tf.float16]. trainStep = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(l2loss)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py”,第196行,最小化grad_loss = grad_loss)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py”,第238行,在compute_gradients中self._assert_valid_dtypes([loss])文件“/ usr / local / lib / python2.7 / dist-packages / tensorflow / python / training / optimizer.py“,第379行,_assert_valid_dtypes dtype,t.name,[v for valid_dtypes中的v]))ValueError:L2Loss的类型为tf.int64无效:0,预期:[tf.float32,tf.float64,tf.float16]。

However, if I use the Cross Entropy like below, then everything goes fine. 但是,如果我像下面这样使用Cross Entropy,那么一切都会好起来的。

crossEnt = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, train_labels))

However, I would like to use the L2Loss because eventually I am computing the RMSE to compare the performance. 但是,我想使用L2Loss,因为最终我正在计算RMSE来比较性能。 I'm not sure if I am missing something obvious. 我不确定我是否遗漏了一些明显的东西。 Any help would be appreciated. 任何帮助,将不胜感激。

All the weights in your network must be learnable. 网络中的所有权重都必须是可学习的。 In order for this to be true, the ops must be differentiable - we must be able to apply the gradients. 为了实现这一点,操作必须是可区分的 - 我们必须能够应用渐变。 We cannot apply gradients on an x - y function from integers to integers, so I think the issue is in where you are casting to float. 我们不能在x - y函数上应用从整数到整数的渐变,所以我认为问题出在你要浮动的地方。

Instead of this: 而不是这个:

predDiff = tf.cast(tf.sub(tf.arg_max(logits, 1), tf.arg_max(train_labels, 1)), tf.float32)

Try casting before applying arg_max and sub : 在应用arg_maxsub之前尝试转换:

float_logits = tf.cast(logits, tf.float32)
float_labels = tf.cast(train_labels, tf.float32)
predDiff = tf.sub(tf.arg_max(float_logits, 1), tf.arg_max(float_labels, 1)))

This way, we can actually calculate and apply gradients for sub and arg_max. 这样,我们实际上可以计算并应用sub和arg_max的渐变。

Not sure if this would help but since your predictions and targets are already ints maybe this could work as both tf.subtract and tf.multiply work with ints: 不确定这是否会有所帮助,但由于您的预测和目标已经是整数,这可能会起作用,因为tf.subtract和tf.multiply都可以使用整数:

self.diff = tf.subtract(self.predictions, self.targets) # Compute difference
self.diff = tf.multiply(self.diff,self.diff, name='diff') # Square the difference
self.loss = tf.reduce_sum(self.diff) # Compute the sum

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KERAS 自定义丢失中的错误“类型错误:传递给参数 'reduction_indices' 的值的数据类型 float32 不在允许值列表中:int32,int64” - ERROR IN KERAS CUSTOM LOSS “TypeError: Value passed to parameter 'reduction_indices' has DataType float32 not in list of allowed values: int32, int64” NumPy的铸造FLOAT32到float64 - Numpy casting float32 to float64 Tensorflow-从complex64转换为2x float32 - Tensorflow - Casting from complex64 to 2x float32 Tensorflow类型错误:传递给参数&#39;shape&#39;的值具有DataType float32不在允许值列表中:int32,int64 - Tensorflow Type Error: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64 Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为 float32 层的 dtype - Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32 Pandas 如何用 int64 null 标记 -999999 替换<na>没有铸造浮动?</na> - Pandas how to replace int64 null marker -999999 with <NA> without casting to float? attr&#39;T&#39;的数据类型float32不在允许值列表中:int32,int64 - DataType float32 for attr 'T' not in list of allowed values: int32, int64 TypeError:DataType float32 for attr&#39;Tindices&#39;不在允许值列表中:int32,int64 - TypeError:DataType float32 for attr 'Tindices' not in list of allowed values: int32, int64 在张量流中使用CNN的信号处理。 TypeError:attr&#39;Tlabels&#39;的DataType float32不在允许值列表中:int32,int64 - Signal processing using CNN in tensorflow. TypeError: DataType float32 for attr 'Tlabels' not in list of allowed values: int32, int64 警告:tensorflow:Layer my_model 正在将输入张量从 dtype float64 转换为 float32 层的 dtype,这是 TensorFlow 2 中的新行为 - WARNING:tensorflow:Layer my_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM