简体   繁体   English

如何在张量流中的切片张量上使用tf.clip_by_value()?

[英]How to use tf.clip_by_value() on sliced tensor in tensorflow?

I am using RNN to predict Humidity and Temperature for next hour based on the Humidity and Temperature values for last 24 hours. 我使用RNN根据过去24小时的湿度和温度值预测下一小时的湿度和温度。 To train the model my input and output tensors are in shape of [24, 2] as shown below: 为了训练模型,我的输入和输出张量的形状为[24,2],如下所示:

[[23, 78],
 [24, 79],
 [25, 78],
 [23, 81],
  .......
 [27, 82],
 [21, 87],
 [28, 88],
 [23, 90]]

Here I want to clip the values of only Humidity column(second) between 0 and 100 as it cant go beyond that. 在这里,我想将仅湿度列(秒)的值剪切在0到100之间,因为它不能超越它。

The code I am using for that purpose is 我为此目的使用的代码是

.....
outputs[:,1] = tf.clip_by_value(outputs[:,1], 0, 100)
.....

And getting the following error: 并收到以下错误:

'Tensor' object does not support item assignment

What should be right way to use tf.clip_by_value() only to one column? 将tf.clip_by_value()仅用于一列的正确方法是什么?

I think the most straightforward (but maybe not optimal) way is to split outputs along the second dimension using tf.split , then apply the clipping and concatenate back (if needed). 我认为最简单(但可能不是最优)的方法是使用tf.split沿第二维分割outputs ,然后应用裁剪并连接回来(如果需要)。

temperature, humidity = tf.split(output, 2, axis=1)
humidity = tf.clip_by_value(humidity, 0, 100)

# optional concat
clipped_output = tf.concat([temperature, humidity], axis=1)

If your outputs is a variable, you can use tf.assign : 如果outputs是变量,则可以使用tf.assign

tf.assign(outputs[:,1], tf.clip_by_value(outputs[:,1], 0, 100))

import tensorflow as tf
a = tf.Variable([[23, 78],
 [24, 79],
 [25, 78],
 [23, 81],
 [27, 82],
 [21, 87],
 [28, 88],
 [23, 90]])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    clipped_value = tf.clip_by_value(a[:,1], 80, 85)
    sess.run(tf.assign(a[:,1], clipped_value))
    print(sess.run(a))

#[[23 80]
# [24 80]
# [25 80]
# [23 81]
# [27 82]
# [21 85]
# [28 85]
# [23 85]]

The following is not documented on the man page https://www.tensorflow.org/api_docs/python/tf/clip_by_value , but in my tests it seems to work: clip_by_value should support broadcasting. 手册页https://www.tensorflow.org/api_docs/python/tf/clip_by_value中未记录以下内容,但在我的测试中似乎有效:clip_by_value应支持广播。 If so, the easiest (as in: not creating temporary tensors) way to do this clipping is the following: 如果是这样,最简单的方法(如:不创建临时张量)进行此剪辑的方法如下:

outputs = tf.clip_by_value(outputs, [[-2147483647, 0]], [[2147483647, 100]])

Here I'm assuming you're using tf.int32 dtype, hence the min and max values for the field you don't want to clip. 在这里,我假设您正在使用tf.int32 ,因此您不想剪切的字段的最小值和最大值。 Admittedly it's not super nice, it looks better for floats where you can use -numpy.inf and numpy.inf instead. 不可否认,它不是超级好看,它看起来更好,你可以使用-numpy.infnumpy.inf

暂无
暂无

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

相关问题 RNN 的 tf.clip_by_value 和 tf.clip_by_global_norm 之间的区别以及如何确定要剪辑的最大值? - Difference between tf.clip_by_value and tf.clip_by_global_norm for RNN's and how to decide max value to clip on? 如何使用tf.gather_nd在tensorflow中切片张量? - How to use tf.gather_nd to slice a tensor in tensorflow? 如何在TensorFlow中将tf.equal()与shape =(1,1)的标签张量一起使用? - How to use tf.equal() with a label tensor of the shape=(1, 1) in TensorFlow? 如何在TensorFlow中更改不是tf.Variable的张量的值? - How to change the value of a tensor which is not a tf.Variable in TensorFlow? 如何使用另一个数组的元素作为索引来切割张量流中的张量? - How can a tensor in tensorflow be sliced ​using elements of another array as an index? 如何在公式中使用张量流张量值? - How to use a tensorflow tensor value in a formula? 如何在 Tensorflow 版本 1 中使用 tf.make_tensor_proto 将张量转换为 ndarray 和 session 建立? - How to use tf.make_tensor_proto to convert tensor to a ndarray in Tensorflow version 1 with session establish? Tensorflow tf.reduce_min 如何从某些索引而不是整个张量中获取最小值 - Tensorflow tf.reduce_min how to get the minimal value from certain indexs instead of the whole tensor 如何在张量流中使用张量索引另一个张量 - How to use a tensor for indexing another tensor in tensorflow Tensorflow:如何修改张量值 - Tensorflow: How to modify the value in tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM