简体   繁体   中英

tf.py_func , custom tensorflow function getting applied to only the first element in the tensor

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

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.

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,

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM