本文整理汇总了Python中tensorflow.compat.v1.slice方法的典型用法代码示例。如果您正苦于以下问题:Python v1.slice方法的具体用法?Python v1.slice怎么用?Python v1.slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.slice方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_tiled_linear
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def _build_tiled_linear(self, inputs, input_name_and_sizes,
output_name_and_sizes, add_bias):
# pylint: disable=missing-docstring
def split_output(output):
if len(output_name_and_sizes) == 1:
return output
elif len(set([size for _, size in output_name_and_sizes])) == 1:
# This is a bit faster than several tf.slice calls.
return tf.split(output, len(output_name_and_sizes), axis=1)
else:
outputs = []
offset = 0
for _, output_size in output_name_and_sizes:
outputs.append(tf.slice(output, [0, offset], [-1, output_size]))
offset += output_size
return outputs
weights = self._ensure_weights()
if len(inputs) > 1:
inputs = tf.concat(inputs, 1)
if add_bias:
biases = self._ensure_biases()
return split_output(tf.nn.xw_plus_b(inputs, weights, biases))
else:
return split_output(tf.matmul(inputs, weights))
示例2: update_hparams_for_vq_gating
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def update_hparams_for_vq_gating(hparams):
"""VQ Gating hparams."""
hparams.add_hparam("z_size", 4)
hparams.add_hparam("noise_dev", 0.5)
# Bottleneck kinds supported: dense, vae, dvq.
hparams.add_hparam("bottleneck_kind", "dvq")
hparams.add_hparam("num_blocks", 1)
hparams.add_hparam("num_residuals", 1)
# Reshape method for DVQ: slice, project
hparams.add_hparam("beta", 0.25)
hparams.add_hparam("epsilon", 1e-5)
hparams.add_hparam("decay", 0.999)
hparams.add_hparam("ema", False) # default is false until ema is implemented
hparams.add_hparam("random_top_k", 1)
hparams.add_hparam("soft_em", False)
hparams.add_hparam("num_samples", 10)
hparams.add_hparam("gating_type", "vq")
hparams.add_hparam("use_scales", int(True))
hparams.add_hparam("residual_centroids", int(False))
示例3: combine
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def combine(self, expert_out, multiply_by_gates=True):
"""Sum together the expert output, weighted by the gates.
The slice corresponding to a particular batch element `b` is computed
as the sum over all experts `i` of the expert output, weighted by the
corresponding gate values. If `multiply_by_gates` is set to False, the
gate values are ignored.
Args:
expert_out: a list of `num_experts` `Tensor`s, each with shape
`[expert_batch_size_i, <extra_output_dims>]`.
multiply_by_gates: a boolean
Returns:
a `Tensor` with shape `[batch_size, <extra_output_dims>]`.
"""
# see comments on convert_gradient_to_tensor
stitched = common_layers.convert_gradient_to_tensor(
tf.concat(expert_out, 0))
if multiply_by_gates:
stitched *= tf.expand_dims(self._nonzero_gates, 1)
combined = tf.unsorted_segment_sum(stitched, self._batch_index,
tf.shape(self._gates)[0])
return combined
示例4: _attention
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def _attention(self, query, attn_states):
conv2d = tf.nn.conv2d
reduce_sum = tf.math.reduce_sum
softmax = tf.nn.softmax
tanh = tf.math.tanh
with tf.variable_scope("attention"):
k = tf.get_variable("attn_w",
[1, 1, self._attn_size, self._attn_vec_size])
v = tf.get_variable("attn_v", [self._attn_vec_size])
hidden = tf.reshape(attn_states,
[-1, self._attn_length, 1, self._attn_size])
hidden_features = conv2d(hidden, k, [1, 1, 1, 1], "SAME")
if self._linear3 is None:
self._linear3 = _Linear(query, self._attn_vec_size, True)
y = self._linear3(query)
y = tf.reshape(y, [-1, 1, 1, self._attn_vec_size])
s = reduce_sum(v * tanh(hidden_features + y), [2, 3])
a = softmax(s)
d = reduce_sum(
tf.reshape(a, [-1, self._attn_length, 1, 1]) * hidden, [1, 2])
new_attns = tf.reshape(d, [-1, self._attn_size])
new_attn_states = tf.slice(attn_states, [0, 1, 0], [-1, -1, -1])
return new_attns, new_attn_states
示例5: assign_to_slices
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def assign_to_slices(self, assign_fn, values, assign_to_tensor_list=None):
"""Assign to the slice variables.
Args:
assign_fn: a function from
(mtf.Variable, tf.Variable, tf.Tensor) -> tf.Operation
values: a list of tf.Tensor
assign_to_tensor_list: an optional list of tf.Variable
Returns:
a tf.operation
"""
if assign_to_tensor_list is None:
assign_to_tensor_list = self._laid_out_tensor.all_slices
# Handle both N -> 1 and N -> N cases.
num_slices = min(len(assign_to_tensor_list), len(values))
devices = [""] * num_slices
return tf.group(
mtf.parallel(devices, assign_fn,
[self._variable] * len(devices),
assign_to_tensor_list[:num_slices],
values[:num_slices]))
示例6: expanded_shape
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def expanded_shape(orig_shape, start_dim, num_dims):
"""Inserts multiple ones into a shape vector.
Inserts an all-1 vector of length num_dims at position start_dim into a shape.
Can be combined with tf.reshape to generalize tf.expand_dims.
Args:
orig_shape: the shape into which the all-1 vector is added (int32 vector)
start_dim: insertion position (int scalar)
num_dims: length of the inserted all-1 vector (int scalar)
Returns:
An int32 vector of length tf.size(orig_shape) + num_dims.
"""
with tf.name_scope('ExpandedShape'):
start_dim = tf.expand_dims(start_dim, 0) # scalar to rank-1
before = tf.slice(orig_shape, [0], start_dim)
add_shape = tf.ones(tf.reshape(num_dims, [1]), dtype=tf.int32)
after = tf.slice(orig_shape, start_dim, [-1])
new_shape = tf.concat([before, add_shape, after], 0)
return new_shape
示例7: _minibatch_subsample_fn
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def _minibatch_subsample_fn(self, inputs):
"""Randomly samples anchors for one image.
Args:
inputs: a list of 2 inputs. First one is a tensor of shape [num_anchors,
num_classes] indicating targets assigned to each anchor. Second one
is a tensor of shape [num_anchors] indicating the class weight of each
anchor.
Returns:
batch_sampled_indicator: bool tensor of shape [num_anchors] indicating
whether the anchor should be selected for loss computation.
"""
cls_targets, cls_weights = inputs
if self._add_background_class:
# Set background_class bits to 0 so that the positives_indicator
# computation would not consider background class.
background_class = tf.zeros_like(tf.slice(cls_targets, [0, 0], [-1, 1]))
regular_class = tf.slice(cls_targets, [0, 1], [-1, -1])
cls_targets = tf.concat([background_class, regular_class], 1)
positives_indicator = tf.reduce_sum(cls_targets, axis=1)
return self._random_example_sampler.subsample(
tf.cast(cls_weights, tf.bool),
batch_size=None,
labels=tf.cast(positives_indicator, tf.bool))
示例8: _cross_suppression
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def _cross_suppression(boxes, box_slice, iou_threshold, inner_idx):
"""Bounding-boxes cross-suppression loop body.
Args:
boxes: A float Tensor of shape [1, anchors, 4], representing boxes.
box_slice: A float Tensor of shape [1, _NMS_TILE_SIZE, 4], the box tile
returned from last iteration
iou_threshold: A scalar, representing IOU threshold.
inner_idx: A scalar, representing inner index.
Returns:
boxes: A float Tensor of shape [1, anchors, 4], representing boxes.
ret_slice: A float Tensor of shape [1, _NMS_TILE_SIZE, 4], the box tile
after suppression
iou_threshold: A scalar, representing IOU threshold.
inner_idx: A scalar, inner index incremented.
"""
new_slice = tf.slice(boxes, [0, inner_idx * _NMS_TILE_SIZE, 0],
[1, _NMS_TILE_SIZE, 4])
iou = batch_iou(new_slice, box_slice)
ret_slice = tf.expand_dims(
tf.cast(tf.reduce_all(iou < iou_threshold, [1]), box_slice.dtype),
2) * box_slice
return boxes, ret_slice, iou_threshold, inner_idx + 1
示例9: __call__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def __call__(self, inputs, state, scope=None):
"""Run this multi-layer cell on inputs, starting from state."""
output = None
with tf.variable_scope(scope or "skip_multi_rnn_cell"):
cur_state_pos = 0
cur_inp = inputs
new_states = []
for i, cell in enumerate(self._cells):
with tf.variable_scope("cell_%d" % i):
if self._state_is_tuple:
if not nest.is_sequence(state):
raise ValueError(
"Expected state to be a tuple of length %d, but received: %s"
% (len(self.state_size), state))
cur_state = state[i]
else:
cur_state = tf.slice(
state, [0, cur_state_pos], [-1, cell.state_size])
cur_state_pos += cell.state_size
cur_inp, new_state = cell(cur_inp, cur_state)
new_states.append(new_state)
if output is None:
output = cur_inp
else:
output += cur_inp
new_states = (tuple(new_states) if self._state_is_tuple else
tf.concat(new_states, 1))
return output, new_states
示例10: __call__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def __call__(self, inputs, state, scope=None):
"""Run this multi-layer cell on inputs, starting from state."""
with tf.variable_scope(scope or "res_multi_rnn_cell"):
cur_state_pos = 0
cur_inp = inputs
new_states = []
for i, cell in enumerate(self._cells):
with tf.variable_scope("cell_%d" % i):
if self._state_is_tuple:
if not nest.is_sequence(state):
raise ValueError(
"Expected state to be a tuple of length %d, but received: %s"
% (len(self.state_size), state))
cur_state = state[i]
else:
cur_state = tf.slice(
state, [0, cur_state_pos], [-1, cell.state_size])
cur_state_pos += cell.state_size
cur_inp2, new_state = cell(cur_inp, cur_state)
if i == 0:
cur_inp = cur_inp2
else:
cur_inp = cur_inp + cur_inp2
new_states.append(new_state)
new_states = (tuple(new_states) if self._state_is_tuple else
tf.concat(new_states, 1))
return cur_inp, new_states
示例11: _curvature_range
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def _curvature_range(self):
"""Curvature range.
Returns:
h_max_t, h_min_t ops
"""
self._curv_win = tf.get_variable("curv_win",
dtype=tf.float32,
trainable=False,
shape=[self.curvature_window_width,],
initializer=tf.zeros_initializer)
# We use log smoothing for curvature range
self._curv_win = tf.scatter_update(self._curv_win,
self._step % self.curvature_window_width,
tf.log(self._grad_norm_squared))
# Note here the iterations start from iteration 0
valid_window = tf.slice(self._curv_win,
tf.constant([0,]),
tf.expand_dims(
tf.minimum(
tf.constant(self.curvature_window_width),
self._step + 1), dim=0))
self._h_min_t = tf.reduce_min(valid_window)
self._h_max_t = tf.reduce_max(valid_window)
curv_range_ops = []
with tf.control_dependencies([self._h_min_t, self._h_max_t]):
avg_op = self._moving_averager.apply([self._h_min_t, self._h_max_t])
with tf.control_dependencies([avg_op]):
self._h_min = tf.exp(
tf.identity(self._moving_averager.average(self._h_min_t)))
self._h_max = tf.exp(
tf.identity(self._moving_averager.average(self._h_max_t)))
if self._sparsity_debias:
self._h_min *= self._sparsity_avg
self._h_max *= self._sparsity_avg
curv_range_ops.append(avg_op)
return curv_range_ops # h_max_t, h_min_t
示例12: call_fake_controller
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def call_fake_controller(push_values, pop_values, write_values, output_values):
"""Mock a RNN controller from a set of expected outputs.
Args:
push_values: Expected controller push values.
pop_values: Expected controller pop values.
write_values: Expected controller write values.
output_values: Expected controller output values.
Returns:
A callable which behaves like the call method of an NeuralStackCell.
"""
def call(cell, inputs, prev_read_values, controller_state, batch_size):
del inputs
del prev_read_values
del batch_size
next_step = tf.constant(0)
if hasattr(cell, "current_step"):
next_step = tf.assign_add(cell.current_step, tf.constant(1))
return neural_stack.NeuralStackControllerInterface(
push_strengths=tf.slice(tf.constant(push_values),
[next_step, 0, 0, 0],
[1, -1, -1, -1]),
pop_strengths=tf.slice(tf.constant(pop_values),
[next_step, 0, 0, 0],
[1, -1, -1, -1]),
write_values=tf.slice(tf.constant(write_values),
[next_step, 0, 0],
[1, -1, -1]),
outputs=tf.slice(tf.constant(output_values),
[next_step, 0, 0],
[1, -1, -1]),
state=controller_state
)
return call
示例13: tpu_conv1d
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def tpu_conv1d(inputs, filters, kernel_size, padding="SAME", name="tpu_conv1d"):
"""Version of conv1d that works on TPU (as of 11/2017).
Args:
inputs: a Tensor with shape [batch, length, input_depth].
filters: an integer.
kernel_size: an integer.
padding: a string - "SAME" or "LEFT".
name: a string.
Returns:
a Tensor with shape [batch, length, filters].
"""
if kernel_size == 1:
return dense(inputs, filters, name=name, use_bias=True)
if padding == "SAME":
assert kernel_size % 2 == 1
first_offset = -((kernel_size - 1) // 2)
else:
assert padding == "LEFT"
first_offset = -(kernel_size - 1)
last_offset = first_offset + kernel_size - 1
results = []
padded = tf.pad(inputs, [[0, 0], [-first_offset, last_offset], [0, 0]])
for i in range(kernel_size):
shifted = tf.slice(padded, [0, i, 0], tf.shape(inputs)) if i else inputs
shifted.set_shape(inputs.get_shape())
results.append(
dense(shifted, filters, use_bias=(i == 0), name=name + "_%d" % i))
ret = tf.add_n(results)
ret *= kernel_size**-0.5
return ret
示例14: dna_transformation
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def dna_transformation(prev_image, dna_input, dna_kernel_size, relu_shift):
"""Apply dynamic neural advection to previous image.
Args:
prev_image: previous image to be transformed.
dna_input: hidden lyaer to be used for computing DNA transformation.
dna_kernel_size: dna kernel size.
relu_shift: shift for ReLU function.
Returns:
List of images transformed by the predicted CDNA kernels.
"""
# Construct translated images.
prev_image_pad = tf.pad(prev_image, [[0, 0], [2, 2], [2, 2], [0, 0]])
image_height = int(prev_image.get_shape()[1])
image_width = int(prev_image.get_shape()[2])
inputs = []
for xkern in range(dna_kernel_size):
for ykern in range(dna_kernel_size):
inputs.append(
tf.expand_dims(
tf.slice(prev_image_pad, [0, xkern, ykern, 0],
[-1, image_height, image_width, -1]), [3]))
inputs = tf.concat(axis=3, values=inputs)
# Normalize channels to 1.
kernel = tf.nn.relu(dna_input - relu_shift) + relu_shift
kernel = tf.expand_dims(
kernel / tf.reduce_sum(kernel, [3], keep_dims=True), [4])
return tf.reduce_sum(kernel * inputs, [3], keep_dims=False)
示例15: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import slice [as 别名]
def __init__(self, get_regularizer_to_slice, begin, size):
"""Creates an instance.
Args:
get_regularizer_to_slice: A callable, such that get_regularizer_to_slice()
returns an OpRegularizer that has to be sliced.
begin: An integer, where to begin the slice.
size: An integer, the length of the slice (so the slice ends at
begin + size).
"""
self._get_regularizer_to_slice = get_regularizer_to_slice
self._begin = begin
self._size = size
self._alive_vector = None
self._regularization_vector = None