本文整理汇总了Python中tensorflow.compat.v1.float32方法的典型用法代码示例。如果您正苦于以下问题:Python v1.float32方法的具体用法?Python v1.float32怎么用?Python v1.float32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.float32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testAppendGradientsWithLossScaleWithoutNan
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def testAppendGradientsWithLossScaleWithoutNan(self):
v = tf.Variable(0)
training_ops = []
get_apply_gradients_ops_func = lambda: [tf.assign(v, v + 1)]
loss_scale_params = variable_mgr_util.AutoLossScaleParams(
enable_auto_loss_scale=True,
loss_scale=tf.Variable(4, dtype=tf.float32),
loss_scale_normal_steps=tf.Variable(10),
inc_loss_scale_every_n=10,
is_chief=True)
variable_mgr_util.append_gradients_with_loss_scale(
training_ops,
get_apply_gradients_ops_func,
loss_scale_params,
grad_has_inf_nan=tf.constant(False))
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(training_ops)
self.assertEqual(sess.run(v), 1)
self.assertEqual(sess.run(loss_scale_params.loss_scale), 8)
self.assertEqual(sess.run(loss_scale_params.loss_scale_normal_steps), 0)
示例2: testAppendGradientsWithLossScaleWithtNan
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def testAppendGradientsWithLossScaleWithtNan(self):
v = tf.Variable(0)
training_ops = []
get_apply_gradients_ops_func = lambda: [tf.assign(v, v + 1)]
loss_scale_params = variable_mgr_util.AutoLossScaleParams(
enable_auto_loss_scale=True,
loss_scale=tf.Variable(4, dtype=tf.float32),
loss_scale_normal_steps=tf.Variable(10),
inc_loss_scale_every_n=10,
is_chief=True)
variable_mgr_util.append_gradients_with_loss_scale(
training_ops,
get_apply_gradients_ops_func,
loss_scale_params,
grad_has_inf_nan=tf.constant(True))
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(training_ops)
self.assertEqual(sess.run(v), 0) # Skip updating for v.
# halve loss_scale and reset local_scale_normal_steps.
self.assertEqual(sess.run(loss_scale_params.loss_scale), 2)
self.assertEqual(sess.run(loss_scale_params.loss_scale_normal_steps), 0)
示例3: _fp16_variable_creator
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _fp16_variable_creator(next_creator, **kwargs):
"""Variable creator to create variables in fp32 and cast them to fp16."""
dtype = kwargs.get('dtype', None)
initial_value = kwargs.get('initial_value', None)
if dtype is None:
if initial_value is not None and not callable(initial_value):
dtype = initial_value.dtype
if dtype == tf.float16:
if callable(initial_value):
new_initial_value = lambda: tf.cast(initial_value(), tf.float32)
else:
new_initial_value = tf.cast(initial_value, tf.float32)
kwargs['dtype'] = tf.float32
kwargs['initial_value'] = new_initial_value
var = next_creator(**kwargs)
return tf.cast(var, dtype=tf.float16)
else:
return next_creator(**kwargs)
示例4: build_network
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def build_network(self, images, phase_train=True, nclass=1001,
data_type=tf.float32):
# pylint: disable=g-import-not-at-top
try:
from official.resnet.r1.imagenet_main import ImagenetModel
except ImportError:
tf.logging.fatal('Please include tensorflow/models to the PYTHONPATH.')
raise
images = tf.cast(images, data_type)
model_class = ImagenetModel(resnet_size=self.resnet_size,
resnet_version=self.version,
# The official model dtype seems to be ignored,
# as the dtype it uses is the dtype of the input
# images. Doesn't hurt to set it though.
dtype=data_type)
logits = model_class(images, phase_train)
logits = tf.cast(logits, tf.float32)
return model_lib.BuildNetworkResult(logits=logits, extra_info=None)
示例5: decode_jpeg
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def decode_jpeg(image_buffer, scope=None): # , dtype=tf.float32):
"""Decode a JPEG string into one 3-D float image Tensor.
Args:
image_buffer: scalar string Tensor.
scope: Optional scope for op_scope.
Returns:
3-D float Tensor with values ranging from [0, 1).
"""
# with tf.op_scope([image_buffer], scope, 'decode_jpeg'):
# with tf.name_scope(scope, 'decode_jpeg', [image_buffer]):
with tf.name_scope(scope or 'decode_jpeg'):
# Decode the string as an RGB JPEG.
# Note that the resulting image contains an unknown height and width
# that is set dynamically by decode_jpeg. In other words, the height
# and width of image is unknown at compile-time.
image = tf.image.decode_jpeg(image_buffer, channels=3,
fancy_upscaling=False,
dct_method='INTEGER_FAST')
# image = tf.Print(image, [tf.shape(image)], 'Image shape: ')
return image
示例6: _run_benchmark_cnn_with_black_and_white_images
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _run_benchmark_cnn_with_black_and_white_images(self, params):
"""Runs BenchmarkCNN with black and white images.
A BenchmarkCNN is created and run with black and white images as input. Half
the images are black (i.e., filled with 0s) and half are white (i.e., filled
with 255s).
Args:
params: Params for BenchmarkCNN.
Returns:
A list of lines from the output of BenchmarkCNN.
"""
# TODO(reedwm): Instead of generating images here, use black and white
# tfrecords by calling test_util.create_black_and_white_images().
effective_batch_size = params.batch_size * params.num_gpus
half_batch_size = effective_batch_size // 2
images = np.zeros((effective_batch_size, 227, 227, 3), dtype=np.float32)
images[half_batch_size:, :, :, :] = 255
labels = np.array([0] * half_batch_size + [1] * half_batch_size,
dtype=np.int32)
return self._run_benchmark_cnn_with_fake_images(params, images, labels)
示例7: testLowAccuracy
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def testLowAccuracy(self):
params = test_util.get_params('testLowAccuracy')._replace(
print_training_accuracy=True, batch_size=5, num_batches=10)
# We force low accuracy by having each batch containing 10 identical images,
# each with a different label. This guarantees a top-1 accuracy of exactly
# 0.1 and a top-5 accuracy of exactly 0.5.
images = np.zeros((10, 227, 227, 3), dtype=np.float32)
labels = np.arange(10, dtype=np.int32)
logs = self._run_benchmark_cnn_with_fake_images(params, images, labels)
training_outputs = test_util.get_training_outputs_from_logs(
logs, params.print_training_accuracy)
last_output = training_outputs[-1]
# TODO(reedwm): These should be assertEqual but for some reason,
# occasionally the accuracies are lower (Running this test 500 times, these
# asserts failed twice). Investigate this problem.
self.assertLessEqual(last_output.top_1_accuracy, 0.1)
self.assertLessEqual(last_output.top_5_accuracy, 0.5)
示例8: _test_preprocessing_traing
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _test_preprocessing_traing(self, image_buf, image_color,
output_height, output_width, bbox,
batch_position, resize_method, distortions,
summary_verbosity, fuse_decode_and_crop):
new_image = preprocessing.train_image(
image_buf,
output_height,
output_width,
bbox,
batch_position,
resize_method,
distortions,
summary_verbosity=summary_verbosity,
fuse_decode_and_crop=fuse_decode_and_crop)
self.assertEqual(new_image.shape, [output_height, output_width, 3])
with self.test_session(use_gpu=True) as sess:
new_image_value = sess.run(new_image)
self.assertAllClose(
new_image_value,
np.full(
[output_height, output_width, 3],
image_color,
dtype=np.float32),
atol=50.,
rtol=0.)
示例9: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def __init__(self,
input_op,
input_nchan,
phase_train,
use_tf_layers,
data_format='NCHW',
dtype=tf.float32,
variable_dtype=tf.float32):
self.top_layer = input_op
self.top_size = input_nchan
self.phase_train = phase_train
self.use_tf_layers = use_tf_layers
self.data_format = data_format
self.dtype = dtype
self.variable_dtype = variable_dtype
self.counts = defaultdict(lambda: 0)
self.use_batch_norm = False
self.batch_norm_config = {} # 'decay': 0.997, 'scale': True}
self.channel_pos = ('channels_last'
if data_format == 'NHWC' else 'channels_first')
self.aux_top_layer = None
self.aux_top_size = 0
示例10: _maybe_initialize_fp16
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _maybe_initialize_fp16(self):
"""Initialize fp16 settings."""
if self.params.use_fp16 and not self._doing_eval:
init_loss_scale_val = float(self.params.fp16_loss_scale or
self.model.get_fp16_loss_scale())
self.loss_scale = None
self.loss_scale_normal_steps = None
if self.enable_auto_loss_scale or init_loss_scale_val != 1:
self.loss_scale = tf.get_variable(
name='loss_scale',
initializer=init_loss_scale_val,
dtype=tf.float32,
trainable=False)
if self.enable_auto_loss_scale:
self.loss_scale_normal_steps = tf.get_variable(
name='loss_scale_normal_steps', initializer=0, trainable=False)
示例11: _ensure_keep_mask
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _ensure_keep_mask(self, x):
if self._keep_mask is None or not self._share_mask:
shape = tf.shape(x)
k = shape[1]
# To make this class a drop-in replacement for bernoulli dropout we
# paramaterize it with keep_prob. Set alpha of the dirichlet so that the
# variance is equal to the variance of the bernoulli with p=keep_prob
# divided by keep_prob.
# Now the variance of the dirichlet with k equal alphas is
# (k-1)/(k^2(k*alpha+1). Solve that for alpha.
kf = tf.cast(k, tf.float32)
alpha = self._keep_prob * (kf - 1.0) / ((1-self._keep_prob)*kf) - 1.0/kf
dist = tfp.distributions.Dirichlet(tf.ones(shape=k) * alpha)
assert (dist.reparameterization_type ==
tfp.distributions.FULLY_REPARAMETERIZED)
# The E[dir(alpha)] = 1/k for all elements, but we want the expectation to
# be keep_prob, hence the multiplication.
self._keep_mask = kf * dist.sample(shape[0])
self._keep_mask.set_shape(x.get_shape())
return self._keep_mask
示例12: compute_logits
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def compute_logits(self, token_ids: tf.Tensor) -> tf.Tensor:
"""
Implements a language model, where each output is conditional on the current
input and inputs processed so far.
Args:
token_ids: int32 tensor of shape [B, T], storing integer IDs of tokens.
Returns:
tf.float32 tensor of shape [B, T, V], storing the distribution over output symbols
for each timestep for each batch element.
"""
# TODO 5# 1) Embed tokens
# TODO 5# 2) Run RNN on embedded tokens
# TODO 5# 3) Project RNN outputs onto the vocabulary to obtain logits.
return rnn_output_logits
示例13: calculate_generalized_advantage_estimator
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def calculate_generalized_advantage_estimator(
reward, value, done, gae_gamma, gae_lambda):
# pylint: disable=g-doc-args
"""Generalized advantage estimator.
Returns:
GAE estimator. It will be one element shorter than the input; this is
because to compute GAE for [0, ..., N-1] one needs V for [1, ..., N].
"""
# pylint: enable=g-doc-args
next_value = value[1:, :]
next_not_done = 1 - tf.cast(done[1:, :], tf.float32)
delta = (reward[:-1, :] + gae_gamma * next_value * next_not_done
- value[:-1, :])
return_ = tf.reverse(tf.scan(
lambda agg, cur: cur[0] + cur[1] * gae_gamma * gae_lambda * agg,
[tf.reverse(delta, [0]), tf.reverse(next_not_done, [0])],
tf.zeros_like(delta[0, :]),
parallel_iterations=1), [0])
return tf.check_numerics(return_, "return")
示例14: _rollout_metadata
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def _rollout_metadata(batch_env, distributional_size=1):
"""Metadata for rollouts."""
batch_env_shape = batch_env.observ.get_shape().as_list()
batch_size = [batch_env_shape[0]]
value_size = batch_size
if distributional_size > 1:
value_size = batch_size + [distributional_size]
shapes_types_names = [
# TODO(piotrmilos): possibly retrieve the observation type for batch_env
(batch_size + batch_env_shape[1:], batch_env.observ_dtype, "observation"),
(batch_size, tf.float32, "reward"),
(batch_size, tf.bool, "done"),
(batch_size + list(batch_env.action_shape), batch_env.action_dtype,
"action"),
(batch_size, tf.float32, "pdf"),
(value_size, tf.float32, "value_function"),
]
return shapes_types_names
示例15: rouge_2_fscore
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import float32 [as 别名]
def rouge_2_fscore(predictions, labels, **unused_kwargs):
"""ROUGE-2 F1 score computation between labels and predictions.
This is an approximate ROUGE scoring method since we do not glue word pieces
or decode the ids and tokenize the output.
Args:
predictions: tensor, model predictions
labels: tensor, gold output.
Returns:
rouge2_fscore: approx rouge-2 f1 score.
"""
outputs = tf.to_int32(tf.argmax(predictions, axis=-1))
# Convert the outputs and labels to a [batch_size, input_length] tensor.
outputs = tf.squeeze(outputs, axis=[-1, -2])
labels = tf.squeeze(labels, axis=[-1, -2])
rouge_2_f_score = tf.py_func(rouge_n, (outputs, labels), tf.float32)
return rouge_2_f_score, tf.constant(1.0)