Reputation: 23
I am looking to reduce the precision of a TensorFlow tensor using bitwise operations. For example, with a NumPy array, this can be achieved by the following,
a = np.array(5) # =[5]
b = np.right_shift(a, 1) # =[2]
c = np.left_shift(b, 1) # =[4]
Is there a way to do this with TensorFlow?
Upvotes: 2
Views: 185
Reputation: 126
According to the documentation on the Tensorflow website:
https://www.tensorflow.org/api_docs/python/tf/bitwise
tf.bitwise.left_shift(x, y, name=None)
x: A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
y: A Tensor. Must have the same type as x.
name: A name for the operation (optional).
Here's an example:
from tensorflow.python.ops import bitwise_ops
import tensorflow as tf
dtype = tf.int8
lhs = tf.constant([5], dtype=dtype)
rhs = tf.constant([1], dtype=dtype)
right_shift_result = bitwise_ops.right_shift(lhs, rhs)
tf.print(right_shift_result)
left_shift_result = bitwise_ops.left_shift(right_shift_result, rhs)
tf.print(left_shift_result)
Out:
[2]
[4]
Upvotes: 1