如何在 Tensorflow 中量化 tf.Variables 的值 [英] How to quantize the values of tf.Variables in Tensorflow
问题描述
我有一个类似的训练模型
I have a training model like
Y = w * X + b
其中 Y 和 X 是输出和输入占位符,w 和 b 是向量
我已经知道 w 的值只能是 0 或 1,而 b 仍然是 tf.float32.
定义时如何量化变量 w 的范围?
或
我可以有两种不同的学习率吗?w 的比率为 1 或 -1,b 的比率照常为 0.0001.
where Y and X are output and input placeholder, w and b are the vectors
I already know the value of w can only be 0 or 1, while b is still tf.float32.
How could I quantize the range of variable w when I define it?
or
Can I have two different learning rates? The rate for w is 1 or -1 and the rate for b is 0.0001 as usual.
推荐答案
在激活期间没有办法限制您的变量.但是您可以做的是在每次迭代后对其进行限制.这是一种使用 tf.where()
:
There is no way to limit your variable during the activation. But what you can do is to limit it after each iteration. Here is one way to do this with tf.where()
:
import tensorflow as tf
a = tf.random_uniform(shape=(3, 3))
b = tf.where(
tf.less(a, tf.zeros_like(a) + 0.5),
tf.zeros_like(a),
tf.ones_like(a)
)
with tf.Session() as sess:
A, B = sess.run([a, b])
print A, '\n'
print B
这会将 0.5 以上的所有内容转换为 1,将其他所有内容都转换为 0:
Which will convert everything above 0.5 to 1 and everything else to 0:
[[ 0.2068541 0.12682056 0.73839438]
[ 0.00512838 0.43465161 0.98486936]
[ 0.32126224 0.29998791 0.31065524]]
[[ 0. 0. 1.]
[ 0. 0. 1.]
[ 0. 0. 0.]]
这篇关于如何在 Tensorflow 中量化 tf.Variables 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!