本文整理汇总了Python中tensorflow.bitcast方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.bitcast方法的具体用法?Python tensorflow.bitcast怎么用?Python tensorflow.bitcast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.bitcast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _quantize
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def _quantize(x, params, randomize=True):
"""Quantize x according to params, optionally randomizing the rounding."""
if not params.quantize:
return x
if not randomize:
return tf.bitcast(
tf.cast(x / params.quantization_scale, tf.int16), tf.float16)
abs_x = tf.abs(x)
sign_x = tf.sign(x)
y = abs_x / params.quantization_scale
y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x)))
y = tf.minimum(y, tf.int16.max) * sign_x
q = tf.bitcast(tf.cast(y, tf.int16), tf.float16)
return q
示例2: decode_from_ternary_gradients
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def decode_from_ternary_gradients(grads_and_vars, scalers, shapes):
"""Decode each gradient tensor."""
with tf.name_scope('ternary_decoder'):
gradients, variables = zip(*grads_and_vars)
floating_gradients = []
for gradient, variable, scaler, shape in zip(gradients, variables, scalers, shapes):
if gradient is None:
floating_gradients.append(None)
# gradient is encoded, so we use variable to check its size
# We also assume dtype of variable and gradient is the same
floating_gradient = tf.cond(tf.size(variable) < FLAGS.size_to_binarize,
lambda: tf.bitcast(gradient, variable.dtype),
lambda: ternary_decoder(gradient, scaler, shape))
floating_gradients.append(floating_gradient)
return list(zip(floating_gradients, variables))
示例3: parse_a_line_b
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def parse_a_line_b(value, base_num, signal_num):
vec = tf.decode_raw(value, tf.int8)
bases = tf.cast(tf.reshape(tf.strided_slice(vec, [0], [base_num]), [base_num]), dtype=tf.int32)
means = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num], [base_num + base_num * 4]), [base_num, 4]),
type=tf.float32)
stds = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 5], [base_num * 5 + base_num * 4]), [base_num, 4]),
type=tf.float32)
sanum = tf.cast(tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 9], [base_num * 9 + base_num * 2]), [base_num, 2]),
type=tf.int16), dtype=tf.int32)
signals = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 11], [base_num * 11 + 4 * signal_num]),
[signal_num, 4]), type=tf.float32)
labels = tf.cast(
tf.reshape(tf.strided_slice(vec, [base_num * 11 + signal_num * 4], [base_num * 11 + signal_num * 4 + 1]),
[1]),
dtype=tf.int32)
return bases, means, stds, sanum, signals, labels
示例4: lookup
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def lookup(self):
"""Returns cached_tree_ids, cached_node_ids, cached_logits."""
cached_tree_ids, cached_node_ids, cached_logits = tf.split(
lookup_ops.lookup_table_find_v2(
self._table_ref,
self._example_ids,
default_value=[0.0, _DUMMY_NODE_ID, 0.0]),
[1, 1, self._logits_dimension],
axis=1)
cached_tree_ids = tf.compat.v1.squeeze(
tf.bitcast(cached_tree_ids, tf.dtypes.int32))
cached_node_ids = tf.compat.v1.squeeze(
tf.bitcast(cached_node_ids, tf.dtypes.int32))
if self._example_ids.shape.ndims is not None:
cached_logits.set_shape(
[self._example_ids.shape[0], self._logits_dimension])
return (cached_tree_ids, cached_node_ids, cached_logits)
示例5: _dequantize
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def _dequantize(q, params):
"""Dequantize q according to params."""
if not params.quantize:
return q
return tf.to_float(tf.bitcast(q, tf.int16)) * params.quantization_scale
示例6: bottom
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def bottom(self, x):
"""Transform input from data space to model space.
Args:
x: A Tensor with shape [batch, ...]
Returns:
body_input: A Tensor with shape [batch, ?, ?, body_input_depth].
"""
inputs = x
with tf.variable_scope(self.name):
# TODO(aidangomez): Will need to sort out a better audio pipeline
def xnet_resblock(x, filters, res_relu, name):
"""Xception-like block."""
with tf.variable_scope(name):
# We only stride along the length dimension to preserve the spectral
# bins (which are tiny in dimensionality relative to length)
y = common_layers.separable_conv_block(
x,
filters, [((1, 1), (3, 3)), ((1, 1), (3, 3))],
first_relu=True,
padding="SAME",
force2d=True,
name="sep_conv_block")
y = common_layers.pool(y, (3, 3), "MAX", "SAME", strides=(2, 1))
return y + common_layers.conv_block(
x,
filters, [((1, 1), (1, 1))],
padding="SAME",
strides=(2, 1),
first_relu=res_relu,
force2d=True,
name="res_conv0")
# Bitcast back from int32
x = tf.bitcast(inputs, tf.float32)
x.set_shape([None, None, None, 1])
for i in range(self._model_hparams.audio_compression):
x = xnet_resblock(x, 2**(i + 1), True, "compress_block_%d" % i)
return xnet_resblock(x, self._body_input_depth, False,
"compress_block_final")
示例7: map_flat_bits
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def map_flat_bits(f, values):
"""Apply the function f to bit-concatenated values, then convert back to original shapes and dtypes."""
values = [tf.convert_to_tensor(v) for v in values]
def maybe_bitcast(v, dtype):
cast = tf.cast if tf.bool in (v.dtype, dtype) else tf.bitcast
return cast(v, dtype)
bits = [maybe_bitcast(v, tf.uint8) for v in values]
flat = tf.concat([tf.reshape(b, [-1]) for b in bits], axis=0)
flat = f(flat)
parts = tf.split(flat, [tf.size(b) for b in bits])
return [maybe_bitcast(tf.reshape(p, tf.shape(b)), v.dtype)
for p, v, b in zip(parts, values, bits)]
示例8: _create_topk_unique
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def _create_topk_unique(inputs, k):
"""Creates the top k values in sorted order with indices.
Args:
inputs: A tensor with rank of 2. [batch_size, original_size].
k: An integer, number of top elements to select.
Returns:
topk_r2: A tensor, the k largest elements. [batch_size, k].
topk_indices_r2: A tensor, indices of the top k values. [batch_size, k].
"""
height = inputs.shape[0]
width = inputs.shape[1]
neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32)
ones = tf.ones([height, width], dtype=tf.float32)
neg_inf_r2 = ones * neg_inf_r0
inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs)
# Select the current largest value k times and keep them in topk_r2. The
# selected largest values are marked as the smallest value to avoid being
# selected again.
tmp = inputs
topk_r2 = tf.zeros([height, k], dtype=tf.float32)
for i in range(k):
kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True)
k_mask = tf.tile(tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0),
[height, 1])
topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]), topk_r2)
ge_r2 = tf.greater_equal(inputs, tf.tile(kth_order_statistic, [1, width]))
tmp = tf.where(ge_r2, neg_inf_r2, inputs)
log2_ceiling = int(math.ceil(math.log(float(int(width)), 2)))
next_power_of_two = 1 << log2_ceiling
count_mask = next_power_of_two - 1
mask_r0 = tf.constant(count_mask)
mask_r2 = tf.fill([height, k], mask_r0)
topk_r2_s32 = tf.bitcast(topk_r2, tf.int32)
topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2)
return topk_r2, topk_indices_r2
示例9: create_topk_unique
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def create_topk_unique(inputs, k):
height = inputs.shape[0]
width = inputs.shape[1]
neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32)
ones = tf.ones([height, width], dtype=tf.float32)
neg_inf_r2 = ones * neg_inf_r0
inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs)
tmp = inputs
topk_r2 = tf.zeros([height, k], dtype=tf.float32)
for i in range(k):
kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True)
k_mask = tf.tile(tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0),
[height, 1])
topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]), topk_r2)
ge_r2 = tf.greater_equal(inputs, tf.tile(kth_order_statistic, [1, width]))
tmp = tf.where(ge_r2, neg_inf_r2, inputs)
log2_ceiling = int(math.ceil(math.log(float(int(width)), 2)))
next_power_of_two = 1 << log2_ceiling
count_mask = next_power_of_two - 1
mask_r0 = tf.constant(count_mask)
mask_r2 = tf.fill([height, k], mask_r0)
topk_r2_s32 = tf.bitcast(topk_r2, tf.int32)
topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2)
return topk_r2, topk_indices_r2
示例10: _testBitcast
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def _testBitcast(self, x, datatype, shape):
with self.test_session():
tf_ans = tf.bitcast(x, datatype)
out = tf_ans.eval()
buff_after = memoryview(out).tobytes()
buff_before = memoryview(x).tobytes()
self.assertEqual(buff_before, buff_after)
self.assertEqual(tf_ans.get_shape(), shape)
self.assertEqual(tf_ans.dtype, datatype)
示例11: testErrors
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def testErrors(self):
x = np.zeros([1, 1], np.int8)
datatype = tf.int32
with self.assertRaisesRegexp(ValueError, "Cannot bitcast due to shape"):
tf.bitcast(x, datatype, None)
示例12: testUnknown
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def testUnknown(self):
x = tf.placeholder(tf.float32)
datatype = tf.int8
tf.bitcast(x, datatype, None)
示例13: encode_to_ternary_gradients
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def encode_to_ternary_gradients(grads_and_vars, get_shape=False):
"""Encode each gradient tensor."""
with tf.name_scope('ternary_encoder'):
gradients, variables = zip(*grads_and_vars)
ternary_gradients = []
gradient_shapes = []
for gradient in gradients:
if gradient is None:
ternary_gradients.append(None)
if get_shape:
gradient_shapes.append(None)
continue
if get_shape:
if isinstance(gradient, tf.IndexedSlices):
gradient_shape = gradient.dense_shape
else:
gradient_shape = gradient.get_shape()
gradient_shapes.append(gradient_shape)
ternary_gradient = tf.cond(tf.size(gradient) < FLAGS.size_to_binarize,
lambda: tf.bitcast(gradient, type=tf.uint8),
lambda: ternary_encoder(gradient))
ternary_gradients.append(ternary_gradient)
if get_shape:
return list(zip(ternary_gradients, variables)), gradient_shapes
else:
return list(zip(ternary_gradients, variables))
示例14: create_make_unique
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def create_make_unique(inputs):
"""Replaces the lower bits of each element with iota."""
if inputs.shape.ndims != 2:
raise ValueError("Input of top_k_with_unique must be rank-2 "
"but got: %s" % inputs.shape)
height = inputs.shape[0]
width = inputs.shape[1]
zeros = tf.zeros([height, width], dtype=tf.int32)
log2_ceiling = int(math.ceil(math.log(int(width), 2)))
next_power_of_two = 1 << log2_ceiling
count_mask = ~(next_power_of_two - 1)
count_mask_r0 = tf.constant(count_mask)
count_mask_r2 = tf.fill([height, width], count_mask_r0)
smallest_normal = 1 << 23
smallest_normal_r0 = tf.constant(smallest_normal, dtype=tf.int32)
smallest_normal_r2 = tf.fill([height, width], smallest_normal_r0)
low_bit_mask = ~(1 << 31)
low_bit_mask_r0 = tf.constant(low_bit_mask, dtype=tf.int32)
low_bit_mask_r2 = tf.fill([height, width], low_bit_mask_r0)
iota = tf.tile(
tf.expand_dims(tf.range(width, dtype=tf.int32), 0), [height, 1])
input_r2 = tf.bitcast(inputs, tf.int32)
abs_r2 = tf.bitwise.bitwise_and(input_r2, low_bit_mask_r2)
if_zero_r2 = tf.equal(abs_r2, zeros)
smallest_normal_preserving_sign_r2 = tf.bitwise.bitwise_or(
input_r2, smallest_normal_r2)
input_no_zeros_r2 = tf.where(if_zero_r2, smallest_normal_preserving_sign_r2,
input_r2)
and_r2 = tf.bitwise.bitwise_and(input_no_zeros_r2, count_mask_r2)
or_r2 = tf.bitwise.bitwise_or(and_r2, iota)
return tf.bitcast(or_r2, tf.float32)
示例15: create_topk_unique
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import bitcast [as 别名]
def create_topk_unique(inputs, k):
"""Creates the top k values in sorted order with indices."""
height = inputs.shape[0]
width = inputs.shape[1]
neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32)
ones = tf.ones([height, width], dtype=tf.float32)
neg_inf_r2 = ones * neg_inf_r0
inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs)
tmp = inputs
topk_r2 = tf.zeros([height, k], dtype=tf.float32)
for i in range(k):
kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True)
k_mask = tf.tile(
tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0), [height, 1])
topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]), topk_r2)
ge_r2 = tf.greater_equal(inputs, tf.tile(kth_order_statistic, [1, width]))
tmp = tf.where(ge_r2, neg_inf_r2, inputs)
log2_ceiling = int(math.ceil(math.log(float(int(width)), 2)))
next_power_of_two = 1 << log2_ceiling
count_mask = next_power_of_two - 1
mask_r0 = tf.constant(count_mask)
mask_r2 = tf.fill([height, k], mask_r0)
topk_r2_s32 = tf.bitcast(topk_r2, tf.int32)
topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2)
return topk_r2, topk_indices_r2