本文整理汇总了Python中tensorflow.compat.v2.TensorShape方法的典型用法代码示例。如果您正苦于以下问题:Python v2.TensorShape方法的具体用法?Python v2.TensorShape怎么用?Python v2.TensorShape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v2
的用法示例。
在下文中一共展示了v2.TensorShape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spec_as_shape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def spec_as_shape(spec, context):
"""Convert a type_spec to a tf shape.
Args:
spec: a single specification for tuple_generator_builder
context: a NQL context
Returns:
tensor shape specification, as required by tf.data.Dataset.from_generator
"""
if spec == str:
return tf.TensorShape([])
elif isinstance(spec, int):
return tf.TensorShape([spec])
else:
return tf.TensorShape([context.get_max_id(spec)])
# GOOGLE_INTERNAL: TODO(b/124102056) Consider moving into nql.
示例2: _reshape_inner_dims
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _reshape_inner_dims(
tensor: tf.Tensor,
shape: tf.TensorShape,
new_shape: tf.TensorShape) -> tf.Tensor:
"""Reshapes tensor to: shape(tensor)[:-len(shape)] + new_shape."""
tensor_shape = tf.shape(tensor)
ndims = shape.rank
tensor.shape[-ndims:].assert_is_compatible_with(shape)
new_shape_inner_tensor = tf.cast(
[-1 if d is None else d for d in new_shape.as_list()], tf.int64)
new_shape_outer_tensor = tf.cast(
tensor_shape[:-ndims], tf.int64)
full_new_shape = tf.concat(
(new_shape_outer_tensor, new_shape_inner_tensor), axis=0)
new_tensor = tf.reshape(tensor, full_new_shape)
new_tensor.set_shape(tensor.shape[:-ndims] + new_shape)
return new_tensor
示例3: restore
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def restore(cls, saved_model_path: str) -> "LanguageModelTF2":
with open(saved_model_path, "rb") as fh:
saved_data = pickle.load(fh)
model = cls(saved_data["hyperparameters"], saved_data["vocab"])
model.build(tf.TensorShape([None, None]))
model.load_weights(saved_model_path)
return model
示例4: build
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def build(self, input_shape):
# A small hack necessary so that train.py is completely framework-agnostic:
input_shape = tf.TensorShape(input_shape)
super().build(input_shape)
示例5: testCustomGrad
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def testCustomGrad(self):
"""Test for custom_grad."""
x_shape = (tf.TensorShape([10]), tf.TensorShape([1, 10]))
y_shape = (tf.TensorShape([]))
dtype = np.float32
scale1 = 5.0
scale2 = 6.0
def fwd(a, b):
return tf_np.sum(tf_np.sqrt(tf_np.exp(a)) + b)
@extensions.custom_grad
def f(a, b):
y = fwd(a, b)
def vjp(dy):
return dy * scale1 * a, dy * scale2 * b
return y, vjp
rng = tf.random.Generator.from_seed(1234)
x, dy = tf.nest.map_structure(lambda shape: uniform(rng, shape, dtype),
[x_shape, y_shape])
expected_y = fwd(*x)
expected_dx = (dy * scale1 * x[0], dy * scale2 * x[1])
y, vjp = extensions.vjp(f, *x)
dx = vjp(dy)
self.assertAllClose(to_tf(expected_y), to_tf(y))
self.assertAllClose(to_tf(expected_dx), to_tf(dx))
示例6: testVjp
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def testVjp(self, has_aux):
x_shape = (tf.TensorShape([10]), tf.TensorShape([1, 10]))
y_shape = (tf.TensorShape([]))
dtype = np.float32
def f(a, b):
y = tf_np.sum(tf_np.sqrt(tf_np.exp(a)) + b)
if has_aux:
return y, tf_np.asarray(1)
else:
return y
rng = tf.random.Generator.from_seed(1234)
x, dy_list = tf.nest.map_structure(lambda shape: uniform(rng, shape, dtype),
[x_shape, [y_shape] * 2])
tf_x = to_tf(x)
outputs = extensions.vjp(f, *x, has_aux=has_aux)
if has_aux:
y, vjp, aux = outputs
else:
y, vjp = outputs
with tf.GradientTape(persistent=True) as tape:
tape.watch(tf_x)
outputs = f(*x)
if has_aux:
expected_y, expected_aux = outputs
self.assertAllClose(to_tf(expected_aux), to_tf(aux))
else:
expected_y = outputs
self.assertAllClose(to_tf(expected_y), to_tf(y))
for dy in dy_list:
expected_dx = tape.gradient(
to_tf(expected_y), tf_x, output_gradients=to_tf(dy))
self.assertAllClose(expected_dx, to_tf(vjp(dy)))
示例7: _replace_none_batch
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _replace_none_batch(x, batch_size=None):
if batch_size is None:
return x
if isinstance(x, tf.Tensor) and x.shape[0] is None:
x.set_shape([batch_size] + x.shape[1:])
return x
elif isinstance(x, tf.TensorShape) and x[0] is None:
return [batch_size] + x[1:]
return x
示例8: build
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def build(self, input_shape=None):
"""Build `Layer`.
Args:
input_shape: The shape of the input to `self.layer`.
Raises:
ValueError: If `Layer` does not contain a `kernel` of weights
"""
input_shape = tf.TensorShape(input_shape).as_list()
input_shape[0] = None
self.input_spec = tf.keras.layers.InputSpec(shape=input_shape)
if not self.layer.built:
self.layer.build(input_shape)
if not hasattr(self.layer, 'kernel'):
raise ValueError('`WeightNorm` must wrap a layer that contains a `kernel` for weights')
self.kernel_norm_axes = list(range(self.layer.kernel.shape.ndims))
self.kernel_norm_axes.pop(self.filter_axis)
self.v = self.layer.kernel
# to avoid a duplicate `kernel` variable after `build` is called
self.layer.kernel = None
self.g = self.add_weight(
name='g',
shape=(int(self.v.shape[self.filter_axis]),),
initializer='ones',
dtype=self.v.dtype,
trainable=True
)
self.initialized = self.add_weight(
name='initialized',
dtype=tf.bool,
trainable=False
)
self.initialized.assign(False)
super(WeightNorm, self).build()
示例9: compute_output_shape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def compute_output_shape(self, input_shape):
return tf.TensorShape(self.layer.compute_output_shape(input_shape).as_list())
示例10: _batch_shape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _batch_shape(self):
return tf.TensorShape([])
示例11: _event_shape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _event_shape(self):
return tf.TensorShape(self.image_shape)
示例12: _mvnormal_pseudo_antithetic
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _mvnormal_pseudo_antithetic(sample_shape,
mean,
covariance_matrix=None,
scale_matrix=None,
random_type=RandomType.PSEUDO_ANTITHETIC,
seed=None,
dtype=None):
"""Returns normal draws with the antithetic samples."""
sample_shape = tf.TensorShape(sample_shape).as_list()
sample_zero_dim = sample_shape[0]
# For the antithetic sampler `sample_shape` is split evenly between
# samples and their antithetic counterparts. In order to do the splitting
# we expect the first dimension of `sample_shape` to be even.
is_even_dim = tf.compat.v1.debugging.assert_equal(
sample_zero_dim % 2,
0,
message='First dimension of `sample_shape` should be even for '
'PSEUDO_ANTITHETIC random type')
with tf.control_dependencies([is_even_dim]):
antithetic_shape = [sample_zero_dim // 2] + sample_shape[1:]
if random_type == RandomType.PSEUDO_ANTITHETIC:
random_type_sample = RandomType.PSEUDO
else:
random_type_sample = RandomType.STATELESS
result = _mvnormal_pseudo(
antithetic_shape,
mean,
covariance_matrix=covariance_matrix,
scale_matrix=scale_matrix,
random_type=random_type_sample,
seed=seed,
dtype=dtype)
if mean is None:
return tf.concat([result, -result], axis=0)
else:
return tf.concat([result, 2 * mean - result], axis=0)
示例13: _prepare_index_matrix
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def _prepare_index_matrix(batch_shape, num_points, dtype):
"""Prepares index matrix for index argument of `tf.gather_nd`."""
batch_shape_reverse = batch_shape.copy()
batch_shape_reverse.reverse()
index_matrix = tf.constant(
np.flip(np.transpose(np.indices(batch_shape_reverse)), -1),
dtype=dtype)
batch_rank = len(batch_shape)
# Broadcast index matrix to the shape of
# `batch_shape + [num_points] + [batch_rank]`.
broadcasted_shape = batch_shape + [num_points] + [batch_rank]
index_matrix = tf.expand_dims(index_matrix, -2) + tf.zeros(
tf.TensorShape(broadcasted_shape), dtype=dtype)
return index_matrix
示例14: broadcast_batch_shape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def broadcast_batch_shape(x, batch_shape):
"""Broadcasts batch shape of `x`."""
return tf.broadcast_to(x, tf.TensorShape(batch_shape) + x.shape[-1])
示例15: while_with_variable_shape_growing_vector
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import TensorShape [as 别名]
def while_with_variable_shape_growing_vector(n):
v = tf.constant([0, 0])
i = 0
while i < n:
tf.autograph.experimental.set_loop_options(
shape_invariants=[(v, tf.TensorShape([None]))])
v = tf.concat((v, [i]), 0)
i += 1
return v