你可以这样做:
import tensorflow as tf
def bits_to_one_hot(bits, depth, dtype=None):
bits = tf.convert_to_tensor(bits)
masks = tf.bitwise.left_shift(tf.ones([], dtype=bits.dtype),
tf.range(depth, dtype=bits.dtype))
masked = tf.bitwise.bitwise_and(tf.expand_dims(bits, -1), masks)
dtype = dtype or bits.dtype
return tf.cast(tf.not_equal(masked, 0), dtype)
data = [0b10111010, 0b00101101]
depth = 8
input_bits = tf.placeholder(tf.int64, [None])
one_hot = bits_to_one_hot(input_bits, depth)
with tf.Session() as sess:
print(sess.run(one_hot, feed_dict={input_bits: data}))
输出:
[[0 1 0 1 1 1 0 1]
[1 0 1 1 0 1 0 0]]