tf.py_func,自定义张量流函数仅应用于张量中的第一个元素

时间:2022-01-24 01:41:46

I am new to tensorflow and was playing around with a deep learning network. I wanted to do a custom rounding off on all the weights after each iteration. As the round function in tensorflow library doesn't give you the option to round the values down to a certain number of decimal points. So I wrote this

我是张力流的新手,正在玩一个深度学习网络。我希望在每次迭代后对所有权重进行自定义舍入。由于tensorflow库中的round函数没有为您提供将值舍入到一定数量的小数点的选项。所以我写了这个

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops

np_prec = lambda x: np.round(x,3).astype(np.float32)
def tf_prec(x,name=None):
     with ops.name_scope( "d_spiky", name,[x]) as name:
          y = tf.py_func(np_prec,
                         [x],
                         [tf.float32],
                         name=name,
                         stateful=False)
          return y[0]
with tf.Session() as sess:

     x = tf.constant([0.234567,0.712,1.2,1.7])
     y = tf_prec(x)
     y = tf_prec(x)
     tf.global_variables_initializer

     print(x.eval(), y.eval())

The output I got was this

我得到的输出是这个

[ 0.234567    0.71200001  1.20000005  1.70000005] [ 0.235       0.71200001  1.20000005  1.70000005]

So the custom rounding off worked only on the first item in the tensor and I am not sure about what I am doing wrong. Thanks in advance.

因此,自定义四舍五入仅适用于张量中的第一项,我不确定我做错了什么。提前致谢。

1 个解决方案

#1


1  

The error here because of the following line,

这里的错误是因为以下行,

np_prec = lambda x: np.round(x,3).astype(np.float32)

you are casting the output to np.float32. You can verify the error by the following code,

您正在将输出转换为np.float32。您可以通过以下代码验证错误,

print(np.round([0.234567,0.712,1.2,1.7], 3).astype(np.float32)) #prints [ 0.235       0.71200001  1.20000005  1.70000005]

The default output of np.round is float64. Moreover, you also have to change the Tout argument in tf.py_func to float64.

np.round的默认输出是float64。此外,您还必须将tf.py_func中的Tout参数更改为float64。

I have given the following code with the above fix and commented where necessary.

我已经使用上面的修复程序给出了以下代码,并在必要时进行了注释。

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops

np_prec = lambda x: np.round(x,3)
def tf_prec(x,name=None):
     with ops.name_scope( "d_spiky", name,[x]) as name:
          y = tf.py_func(np_prec,
                         [x],
                         [tf.float64], #changed this line to tf.float64
                         name=name,
                         stateful=False)
          return y[0]
with tf.Session() as sess:

     x = tf.constant([0.234567,0.712,1.2,1.7],dtype=np.float64) #specify the input data type np.float64
     y = tf_prec(x)
     y = tf_prec(x)
     tf.global_variables_initializer

     print(x.eval(), y.eval())

Hope this helps.

希望这可以帮助。

#1


1  

The error here because of the following line,

这里的错误是因为以下行,

np_prec = lambda x: np.round(x,3).astype(np.float32)

you are casting the output to np.float32. You can verify the error by the following code,

您正在将输出转换为np.float32。您可以通过以下代码验证错误,

print(np.round([0.234567,0.712,1.2,1.7], 3).astype(np.float32)) #prints [ 0.235       0.71200001  1.20000005  1.70000005]

The default output of np.round is float64. Moreover, you also have to change the Tout argument in tf.py_func to float64.

np.round的默认输出是float64。此外,您还必须将tf.py_func中的Tout参数更改为float64。

I have given the following code with the above fix and commented where necessary.

我已经使用上面的修复程序给出了以下代码,并在必要时进行了注释。

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops

np_prec = lambda x: np.round(x,3)
def tf_prec(x,name=None):
     with ops.name_scope( "d_spiky", name,[x]) as name:
          y = tf.py_func(np_prec,
                         [x],
                         [tf.float64], #changed this line to tf.float64
                         name=name,
                         stateful=False)
          return y[0]
with tf.Session() as sess:

     x = tf.constant([0.234567,0.712,1.2,1.7],dtype=np.float64) #specify the input data type np.float64
     y = tf_prec(x)
     y = tf_prec(x)
     tf.global_variables_initializer

     print(x.eval(), y.eval())

Hope this helps.

希望这可以帮助。