Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何实现一个可以保留几个小数的层?_Python_Tensorflow - Fatal编程技术网

Python 我如何实现一个可以保留几个小数的层?

Python 我如何实现一个可以保留几个小数的层?,python,tensorflow,Python,Tensorflow,我想实现,在输出层保留两个十进制函数。因为我想在两个卷积层之间使用它,所以我想用它来实现这一点。 但因为它保留的两个小数经常溢出,我不知道如何解决它 import tensorflow as tf input = tf.Variable([3.5115155, 3.365, 3.38115155, 3.81151536, 3.38115159, 3.38115158, 3.398115155], dtype=tf.float32) @tf.custom_gradient def round_

我想实现,在输出层保留两个十进制函数。因为我想在两个卷积层之间使用它,所以我想用它来实现这一点。 但因为它保留的两个小数经常溢出,我不知道如何解决它

import tensorflow as tf

input = tf.Variable([3.5115155, 3.365, 3.38115155, 3.81151536, 3.38115159, 3.38115158, 3.398115155], dtype=tf.float32)

@tf.custom_gradient
def round_test(x):
   def grad(dy):
     return 1.0*dy
   return tf.math.round(x * 100)/100, grad

output_clip = round_test(input)

grad_clip = tf.gradients(output_clip, input)

with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   print("input:", sess.run(input))
   print("output_clipping:", sess.run(output_clip))
   print("with clipping:", sess.run(grad_clip)[0])
这是一个错误。 输入:[3.5115156 3.365 3.3811514] 输出剪辑:[3.51 3.36 3.379999]

我预计roud_test3.3811514的输出为3.38,但实际输出为3.379999 我只想保留两位小数。

试试tf.py_func:

结果是:

input: [3.5115156 3.365     3.3811514]
output_clipping: [3.51 3.36 3.38]
with clipping: [1. 1. 1.]
input: [3.5115156 3.365     3.3811514]
output_clipping: [3.51 3.36 3.38]
with clipping: [1. 1. 1.]