本文整理汇总了Python中tensorflow.compat.v1.placeholder_with_default方法的典型用法代码示例。如果您正苦于以下问题:Python v1.placeholder_with_default方法的具体用法?Python v1.placeholder_with_default怎么用?Python v1.placeholder_with_default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.placeholder_with_default方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testLSTMSeq2SeqAttention
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def testLSTMSeq2SeqAttention(self):
vocab_size = 9
x = np.random.randint(1, high=vocab_size, size=(3, 5, 1, 1))
y = np.random.randint(1, high=vocab_size, size=(3, 6, 1, 1))
hparams = lstm.lstm_attention()
p_hparams = problem_hparams.test_problem_hparams(vocab_size,
vocab_size,
hparams)
x = tf.constant(x, dtype=tf.int32)
x = tf.placeholder_with_default(x, shape=[None, None, 1, 1])
with self.test_session() as session:
features = {
"inputs": x,
"targets": tf.constant(y, dtype=tf.int32),
}
model = lstm.LSTMSeq2seqAttention(
hparams, tf.estimator.ModeKeys.TRAIN, p_hparams)
logits, _ = model(features)
session.run(tf.global_variables_initializer())
res = session.run(logits)
self.assertEqual(res.shape, (3, 6, 1, 1, vocab_size))
示例2: get_placeholders
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def get_placeholders(self):
hparams = self.hparams
return dict(
pianorolls=tf.placeholder(
tf.bool,
[None, None, hparams.num_pitches, hparams.num_instruments],
"pianorolls"),
# The default value is only used for checking if completion masker
# should be evoked. It can't be used directly as the batch size
# and length of pianorolls are unknown during static time.
outer_masks=tf.placeholder_with_default(
np.zeros(
(1, 1, hparams.num_pitches, hparams.num_instruments),
dtype=np.float32),
[None, None, hparams.num_pitches, hparams.num_instruments],
"outer_masks"),
sample_steps=tf.placeholder_with_default(0, (), "sample_steps"),
total_gibbs_steps=tf.placeholder_with_default(
0, (), "total_gibbs_steps"),
current_step=tf.placeholder_with_default(0, (), "current_step"),
temperature=tf.placeholder_with_default(0.99, (), "temperature"))
示例3: testStatesAfterLoop
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def testStatesAfterLoop(self):
batch_size = 1
beam_size = 1
vocab_size = 2
decode_length = 3
initial_ids = tf.constant([0] * batch_size) # GO
probabilities = tf.constant([[[0.7, 0.3]], [[0.4, 0.6]], [[0.5, 0.5]]])
def symbols_to_logits(ids, _, states):
pos = tf.shape(ids)[1] - 1
logits = tf.to_float(tf.log(probabilities[pos, :]))
states["state"] += 1
return logits, states
states = {
"state": tf.zeros((batch_size, 1)),
}
states["state"] = tf.placeholder_with_default(
states["state"], shape=(None, 1))
_, _, final_states = beam_search.beam_search(
symbols_to_logits,
initial_ids,
beam_size,
decode_length,
vocab_size,
0.0,
eos_id=1,
states=states)
with self.test_session() as sess:
final_states = sess.run(final_states)
self.assertAllEqual([[[2]]], final_states["state"])
示例4: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def __init__(self, capacity, shape, dtype=tf.int32, name=None):
"""Initializes the TensorBuffer.
Args:
capacity: Initial capacity. Buffer will double in capacity each time it is
filled to capacity.
shape: The shape (as tuple or list) of the tensors to accumulate.
dtype: The type of the tensors.
name: A string name for the variable_scope used.
Raises:
ValueError: If the shape is empty (specifies scalar shape).
"""
shape = list(shape)
self._rank = len(shape)
self._name = name
self._dtype = dtype
if not self._rank:
raise ValueError('Shape cannot be scalar.')
shape = [capacity] + shape
with tf.variable_scope(self._name):
# We need to use a placeholder as the initial value to allow resizing.
self._buffer = tf.Variable(
initial_value=tf.placeholder_with_default(
tf.zeros(shape, dtype), shape=None),
trainable=False,
name='buffer',
use_resource=True)
self._current_size = tf.Variable(
initial_value=0, dtype=tf.int32, trainable=False, name='current_size')
self._capacity = tf.Variable(
initial_value=capacity,
dtype=tf.int32,
trainable=False,
name='capacity')
示例5: _init_placeholders
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def _init_placeholders(self):
self.input_ids_ph = tf.placeholder(shape=(None, None), dtype=tf.int32, name='ids_ph')
self.input_masks_ph = tf.placeholder(shape=(None, None), dtype=tf.int32, name='masks_ph')
self.token_types_ph = tf.placeholder(shape=(None, None), dtype=tf.int32, name='token_types_ph')
self.is_train_ph = tf.placeholder_with_default(False, shape=[], name='is_train_ph')
示例6: execute_tpu_tf1
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def execute_tpu_tf1(self, compute_fn, inputs, graph=None):
"""Executes compute_fn on TPU with Tensorflow 1.X.
Args:
compute_fn: a function containing Tensorflow computation that takes a list
of input numpy tensors, performs computation and returns output numpy
tensors.
inputs: a list of numpy arrays to feed input to the `compute_fn`.
graph: (optional) If not None, provided `graph` is used for computation
instead of a brand new tf.Graph().
Returns:
A list of numpy arrays or a single numpy array.
"""
with self.session(graph=(graph or tf.Graph())) as sess:
placeholders = [tf.placeholder_with_default(v, v.shape) for v in inputs]
def wrap_graph_fn(*args, **kwargs):
results = compute_fn(*args, **kwargs)
if (not (isinstance(results, dict) or isinstance(results, tf.Tensor))
and hasattr(results, '__iter__')):
results = list(results)
return results
tpu_computation = contrib_tpu.rewrite(wrap_graph_fn, placeholders)
sess.run(contrib_tpu.initialize_system())
sess.run([tf.global_variables_initializer(), tf.tables_initializer(),
tf.local_variables_initializer()])
materialized_results = sess.run(tpu_computation,
feed_dict=dict(zip(placeholders, inputs)))
sess.run(contrib_tpu.shutdown_system())
return self.maybe_extract_single_output(materialized_results)
示例7: execute_cpu_tf1
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def execute_cpu_tf1(self, compute_fn, inputs, graph=None):
"""Executes compute_fn on CPU with Tensorflow 1.X.
Args:
compute_fn: a function containing Tensorflow computation that takes a list
of input numpy tensors, performs computation and returns output numpy
tensors.
inputs: a list of numpy arrays to feed input to the `compute_fn`.
graph: (optional) If not None, provided `graph` is used for computation
instead of a brand new tf.Graph().
Returns:
A list of numpy arrays or a single numpy array.
"""
if self.is_tf2():
raise ValueError('Required version Tenforflow 1.X is not available.')
with self.session(graph=(graph or tf.Graph())) as sess:
placeholders = [tf.placeholder_with_default(v, v.shape) for v in inputs]
results = compute_fn(*placeholders)
if (not (isinstance(results, dict) or isinstance(results, tf.Tensor)) and
hasattr(results, '__iter__')):
results = list(results)
sess.run([tf.global_variables_initializer(), tf.tables_initializer(),
tf.local_variables_initializer()])
materialized_results = sess.run(results, feed_dict=dict(zip(placeholders,
inputs)))
return self.maybe_extract_single_output(materialized_results)
示例8: Input
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def Input(self, shape):
"""Builds an Input layer.
Overrides the Keras application Input layer with one that uses a
tf.placeholder_with_default instead of a tf.placeholder. This is necessary
to ensure the application works when run on a TPU.
Args:
shape: The shape for the input layer to use. (Does not include a dimension
for the batch size).
Returns:
An input layer for the specified shape that internally uses a
placeholder_with_default.
"""
default_size = 224
default_batch_size = 1
shape = list(shape)
default_shape = [default_size if dim is None else dim for dim in shape]
input_tensor = tf.constant(0.0, shape=[default_batch_size] + default_shape)
placeholder_with_default = tf.placeholder_with_default(
input=input_tensor, shape=[None] + shape)
return model_utils.input_layer(shape, placeholder_with_default)
# pylint: disable=unused-argument
示例9: Input
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def Input(self, shape):
"""Builds an Input layer.
Overrides the Keras application Input layer with one that uses a
tf.placeholder_with_default instead of a tf.placeholder. This is necessary
to ensure the application works when run on a TPU.
Args:
shape: A tuple of integers representing the shape of the input, which
includes both spatial share and channels, but not the batch size.
Elements of this tuple can be None; 'None' elements represent dimensions
where the shape is not known.
Returns:
An input layer for the specified shape that internally uses a
placeholder_with_default.
"""
default_size = 224
default_batch_size = 1
shape = list(shape)
default_shape = [default_size if dim is None else dim for dim in shape]
input_tensor = tf.constant(0.0, shape=[default_batch_size] + default_shape)
placeholder_with_default = tf.placeholder_with_default(
input=input_tensor, shape=[None] + shape)
return model_utils.input_layer(shape, placeholder_with_default)
示例10: testStates
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def testStates(self):
batch_size = 1
beam_size = 1
vocab_size = 2
decode_length = 3
initial_ids = tf.constant([0] * batch_size) # GO
probabilities = tf.constant([[[0.7, 0.3]], [[0.4, 0.6]], [[0.5, 0.5]]])
expected_states = tf.constant([[[0.]], [[1.]]])
def symbols_to_logits(ids, _, states):
pos = tf.shape(ids)[1] - 1
# We have to assert the values of state inline here since we can't fetch
# them out of the loop!
with tf.control_dependencies(
[tf.assert_equal(states["state"], expected_states[pos])]):
logits = tf.to_float(tf.log(probabilities[pos, :]))
states["state"] += 1
return logits, states
states = {
"state": tf.zeros((batch_size, 1)),
}
states["state"] = tf.placeholder_with_default(
states["state"], shape=(None, 1))
final_ids, _, _ = beam_search.beam_search(
symbols_to_logits,
initial_ids,
beam_size,
decode_length,
vocab_size,
0.0,
eos_id=1,
states=states)
with self.test_session() as sess:
# Catch and fail so that the testing framework doesn't think it's an error
try:
sess.run(final_ids)
except tf.errors.InvalidArgumentError as e:
raise AssertionError(e.message)
示例11: testStateBeamTwo
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def testStateBeamTwo(self):
batch_size = 1
beam_size = 2
vocab_size = 3
decode_length = 3
initial_ids = tf.constant([0] * batch_size) # GO
probabilities = tf.constant([[[0.1, 0.1, 0.8], [0.1, 0.1, 0.8]],
[[0.4, 0.5, 0.1], [0.2, 0.4, 0.4]],
[[0.05, 0.9, 0.05], [0.4, 0.4, 0.2]]])
# The top beam is always selected so we should see the top beam's state
# at each position, which is the one thats getting 3 added to it each step.
expected_states = tf.constant([[[0.], [0.]], [[3.], [3.]], [[6.], [6.]]])
def symbols_to_logits(ids, _, states):
pos = tf.shape(ids)[1] - 1
# We have to assert the values of state inline here since we can't fetch
# them out of the loop!
with tf.control_dependencies(
[tf.assert_equal(states["state"], expected_states[pos])]):
logits = tf.to_float(tf.log(probabilities[pos, :]))
states["state"] += tf.constant([[3.], [7.]])
return logits, states
states = {
"state": tf.zeros((batch_size, 1)),
}
states["state"] = tf.placeholder_with_default(
states["state"], shape=(None, 1))
final_ids, _, _ = beam_search.beam_search(
symbols_to_logits,
initial_ids,
beam_size,
decode_length,
vocab_size,
0.0,
eos_id=1,
states=states)
with self.test_session() as sess:
# Catch and fail so that the testing framework doesn't think it's an error
try:
sess.run(final_ids)
except tf.errors.InvalidArgumentError as e:
raise AssertionError(e.message)
示例12: testTPUBeam
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def testTPUBeam(self):
batch_size = 1
beam_size = 2
vocab_size = 3
decode_length = 3
initial_ids = tf.constant([0] * batch_size) # GO
probabilities = tf.constant([[[0.1, 0.1, 0.8], [0.1, 0.1, 0.8]],
[[0.4, 0.5, 0.1], [0.2, 0.4, 0.4]],
[[0.05, 0.9, 0.05], [0.4, 0.4, 0.2]]])
# The top beam is always selected so we should see the top beam's state
# at each position, which is the one thats getting 3 added to it each step.
expected_states = tf.constant([[[0.], [0.]], [[3.], [3.]], [[6.], [6.]]])
def symbols_to_logits(_, i, states):
# We have to assert the values of state inline here since we can't fetch
# them out of the loop!
with tf.control_dependencies(
[tf.assert_equal(states["state"], expected_states[i])]):
logits = tf.to_float(tf.log(probabilities[i, :]))
states["state"] += tf.constant([[3.], [7.]])
return logits, states
states = {
"state": tf.zeros((batch_size, 1)),
}
states["state"] = tf.placeholder_with_default(
states["state"], shape=(None, 1))
final_ids, _, _ = beam_search.beam_search(
symbols_to_logits,
initial_ids,
beam_size,
decode_length,
vocab_size,
3.5,
eos_id=1,
states=states,
use_tpu=True)
with self.test_session() as sess:
# Catch and fail so that the testing framework doesn't think it's an error
try:
sess.run(final_ids)
except tf.errors.InvalidArgumentError as e:
raise AssertionError(e.message)
self.assertAllEqual([[[0, 2, 0, 1], [0, 2, 1, 0]]], final_ids)
示例13: mobilenet_v1
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import placeholder_with_default [as 别名]
def mobilenet_v1(batchnorm_training,
default_batchnorm_momentum=0.9997,
conv_hyperparams=None,
use_explicit_padding=False,
alpha=1.0,
min_depth=None,
conv_defs=None,
**kwargs):
"""Instantiates the MobileNetV1 architecture, modified for object detection.
This wraps the MobileNetV1 tensorflow Keras application, but uses the
Keras application's kwargs-based monkey-patching API to override the Keras
architecture with the following changes:
- Changes the default batchnorm momentum to 0.9997
- Applies the Object Detection hyperparameter configuration
- Supports FreezableBatchNorms
- Adds support for a min number of filters for each layer
- Makes the `alpha` parameter affect the final convolution block even if it
is less than 1.0
- Adds support for explicit padding of convolutions
- Makes the Input layer use a tf.placeholder_with_default instead of a
tf.placeholder, to work on TPUs.
Args:
batchnorm_training: Bool. Assigned to Batch norm layer `training` param
when constructing `freezable_batch_norm.FreezableBatchNorm` layers.
default_batchnorm_momentum: Float. When 'conv_hyperparams' is None,
batch norm layers will be constructed using this value as the momentum.
conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object
containing hyperparameters for convolution ops. Optionally set to `None`
to use default mobilenet_v1 layer builders.
use_explicit_padding: If True, use 'valid' padding for convolutions,
but explicitly pre-pads inputs so that the output dimensions are the
same as if 'same' padding were used. Off by default.
alpha: The width multiplier referenced in the MobileNetV1 paper. It
modifies the number of filters in each convolutional layer.
min_depth: Minimum number of filters in the convolutional layers.
conv_defs: Network layout to specify the mobilenet_v1 body. Default is
`None` to use the default mobilenet_v1 network layout.
**kwargs: Keyword arguments forwarded directly to the
`tf.keras.applications.Mobilenet` method that constructs the Keras
model.
Returns:
A Keras model instance.
"""
layers_override = _LayersOverride(
batchnorm_training,
default_batchnorm_momentum=default_batchnorm_momentum,
conv_hyperparams=conv_hyperparams,
use_explicit_padding=use_explicit_padding,
min_depth=min_depth,
alpha=alpha,
conv_defs=conv_defs)
return tf.keras.applications.MobileNet(
alpha=alpha, layers=layers_override, **kwargs)
# pylint: enable=invalid-name