Python tensorflow.python.ops.array_ops 模块,stack() 实例源码

我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用tensorflow.python.ops.array_ops.stack()

项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def repeat(x, n):
      """Repeats a 2D tensor.

      if `x` has shape (samples, dim) and `n` is `2`,
      the output will have shape `(samples, 2, dim)`.

      Arguments:
          x: Tensor or variable.
          n: Python integer, number of times to repeat.

      Returns:
          A tensor.
      """
      assert ndim(x) == 2
      x = array_ops.expand_dims(x, 1)
      pattern = array_ops.stack([1, n, 1])
      return array_ops.tile(x, pattern)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def _preprocess_deconv_output_shape(x, shape, data_format):
      """Get the output_shape for the deconvolution.

      Arguments:
          x: input tensor.
          shape: output shape.
          data_format: string, one of 'channels_last', 'channels_first'.

      Returns:
          The output shape.
      """
      if data_format == 'channels_first':
        shape = (shape[0], shape[2], shape[3], shape[1])

      if shape[0] is None:
        shape = (array_ops.shape(x)[0],) + tuple(shape[1:])
        shape = array_ops.stack(list(shape))
      return shape
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def test_5th_order_polynomial(self):
    # this should be an exact fit
    f = lambda x: x**4 + x**3 - 2 * x**2 + 4 * x + 5
    f_prime = lambda x: 4 * x**3 + 3 * x**2 - 4 * x + 4
    coeffs = odes._interp_fit(
        f(0.0), f(10.0), f(5.0), f_prime(0.0), f_prime(10.0), 10.0)
    times = np.linspace(0, 10, dtype=np.float32)
    y_fit = array_ops.stack(
        [odes._interp_evaluate(coeffs, 0.0, 10.0, t) for t in times])
    y_expected = f(times)
    with self.test_session() as sess:
      y_actual = sess.run(y_fit)
      self.assertAllClose(y_expected, y_actual)

    # attempt interpolation outside bounds
    y_invalid = odes._interp_evaluate(coeffs, 0.0, 10.0, 100.0)
    with self.test_session() as sess:
      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(y_invalid)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _shape_tensor(self):
    # Avoid messy broadcasting if possible.
    if self.shape.is_fully_defined():
      return ops.convert_to_tensor(
          self.shape.as_list(), dtype=dtypes.int32, name="shape")

    # Don't check the matrix dimensions.  That would add unnecessary Asserts to
    # the graph.  Things will fail at runtime naturally if shapes are
    # incompatible.
    matrix_shape = array_ops.stack([
        self.operators[0].range_dimension_tensor(),
        self.operators[-1].domain_dimension_tensor()
    ])

    # Dummy Tensor of zeros.  Will never be materialized.
    zeros = array_ops.zeros(shape=self.operators[0].batch_shape_tensor())
    for operator in self.operators[1:]:
      zeros += array_ops.zeros(shape=operator.batch_shape_tensor())
    batch_shape = array_ops.shape(zeros)

    return array_ops.concat((batch_shape, matrix_shape), 0)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _sample_n(self, n, seed=None):
    # Recall _assert_valid_mu ensures mu and self._cov have same batch shape.
    shape = array_ops.concat([self._cov.vector_shape(), [n]], 0)
    white_samples = random_ops.random_normal(shape=shape,
                                             mean=0.,
                                             stddev=1.,
                                             dtype=self.dtype,
                                             seed=seed)

    correlated_samples = self._cov.sqrt_matmul(white_samples)

    # Move the last dimension to the front
    perm = array_ops.concat(
        (array_ops.stack([array_ops.rank(correlated_samples) - 1]),
         math_ops.range(0, array_ops.rank(correlated_samples) - 1)), 0)

    # TODO(ebrevdo): Once we get a proper tensor contraction op,
    # perform the inner product using that instead of batch_matmul
    # and this slow transpose can go away!
    correlated_samples = array_ops.transpose(correlated_samples, perm)
    samples = correlated_samples + self.mu
    return samples
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def testUniformNans(self):
    with self.test_session():
      a = 10.0
      b = [11.0, 100.0]
      uniform = uniform_lib.Uniform(a=a, b=b)

      no_nans = constant_op.constant(1.0)
      nans = constant_op.constant(0.0) / constant_op.constant(0.0)
      self.assertTrue(math_ops.is_nan(nans).eval())
      with_nans = array_ops.stack([no_nans, nans])

      pdf = uniform.prob(with_nans)

      is_nan = math_ops.is_nan(pdf).eval()
      self.assertFalse(is_nan[0])
      self.assertTrue(is_nan[1])
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def normalize_batch_in_training(x, gamma, beta, reduction_axes, epsilon=1e-3):
      """Computes mean and std for batch then apply batch_normalization on batch.

      Arguments:
          x: Input tensor or variable.
          gamma: Tensor by which to scale the input.
          beta: Tensor with which to center the input.
          reduction_axes: iterable of integers,
              axes over which to normalize.
          epsilon: Fuzz factor.

      Returns:
          A tuple length of 3, `(normalized_tensor, mean, variance)`.
      """
      mean, var = nn.moments(
          x, reduction_axes, shift=None, name=None, keep_dims=False)
      if sorted(reduction_axes) == list(range(ndim(x)))[:-1]:
        normed = nn.batch_normalization(x, mean, var, beta, gamma, epsilon)
      else:
        # need broadcasting
        target_shape = []
        for axis in range(ndim(x)):
          if axis in reduction_axes:
            target_shape.append(1)
          else:
            target_shape.append(array_ops.shape(x)[axis])
        target_shape = array_ops.stack(target_shape)

        broadcast_mean = array_ops.reshape(mean, target_shape)
        broadcast_var = array_ops.reshape(var, target_shape)
        if gamma is None:
          broadcast_gamma = None
        else:
          broadcast_gamma = array_ops.reshape(gamma, target_shape)
        if beta is None:
          broadcast_beta = None
        else:
          broadcast_beta = array_ops.reshape(beta, target_shape)
        normed = nn.batch_normalization(x, broadcast_mean, broadcast_var,
                                        broadcast_beta, broadcast_gamma, epsilon)
      return normed, mean, var
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def batch_flatten(x):
      """Turn a nD tensor into a 2D tensor with same 0th dimension.

      In other words, it flattens each data samples of a batch.

      Arguments:
          x: A tensor or variable.

      Returns:
          A tensor.
      """
      x = array_ops.reshape(x, array_ops.stack([-1, prod(shape(x)[1:])]))
      return x
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def stack(x, axis=0):
      """Stacks a list of rank `R` tensors into a rank `R+1` tensor.

      Arguments:
          x: List of tensors.
          axis: Axis along which to perform stacking.

      Returns:
          A tensor.
      """
      return array_ops.stack(x, axis=axis)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def random_channel_shift(x, intensity, channel_axis=0):
          x = np.rollaxis(x, channel_axis, 0)
          min_x, max_x = np.min(x), np.max(x)
          channel_images = [
              np.clip(x_channel + np.random.uniform(-intensity, intensity), min_x,
                      max_x) for x_channel in x
          ]
          x = np.stack(channel_images, axis=0)
          x = np.rollaxis(x, 0, channel_axis + 1)
          return x
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def apply_transform(x,
                            transform_matrix,
                            channel_axis=0,
                            fill_mode='nearest',
                            cval=0.):
          """Apply the image transformation specified by a matrix.

          Arguments:
              x: 2D numpy array, single image.
              transform_matrix: Numpy array specifying the geometric transformation.
              channel_axis: Index of axis for channels in the input tensor.
              fill_mode: Points outside the boundaries of the input
                  are filled according to the given mode
                  (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
              cval: Value used for points outside the boundaries
                  of the input if `mode='constant'`.

          Returns:
              The transformed version of the input.
          """
          x = np.rollaxis(x, channel_axis, 0)
          final_affine_matrix = transform_matrix[:2, :2]
          final_offset = transform_matrix[:2, 2]
          channel_images = [
              ndi.interpolation.affine_transform(
                  x_channel,
                  final_affine_matrix,
                  final_offset,
                  order=0,
                  mode=fill_mode,
                  cval=cval) for x_channel in x
          ]
          x = np.stack(channel_images, axis=0)
          x = np.rollaxis(x, 0, channel_axis + 1)
          return x
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def pack(labeled_tensors, new_axis, axis_position=0, name=None):
  """Pack tensors along a new axis.

  See tf.pack.

  Args:
    labeled_tensors: The input tensors, which must have identical axes.
    new_axis: The name of the new axis, or a tuple containing the name
      and coordinate labels.
    axis_position: Optional integer position at which to insert the new axis.
    name: Optional op name.

  Returns:
    The packed tensors as a single LabeledTensor, with `new_axis` in the given
    `axis_position`.

  Raises:
    ValueError: If fewer than one input tensors is provided, or if the tensors
      don't have identical axes.
  """
  with ops.name_scope(name, 'lt_pack', labeled_tensors) as scope:
    labeled_tensors = [core.convert_to_labeled_tensor(lt)
                       for lt in labeled_tensors]

    if len(labeled_tensors) < 1:
      raise ValueError('pack expects at least 1 tensors, but received %s' %
                       labeled_tensors)

    axes_0 = labeled_tensors[0].axes
    for t in labeled_tensors:
      if t.axes != axes_0:
        raise ValueError('Non-identical axes. Expected %s but got %s' %
                         (axes_0, t.axes))

    pack_op = array_ops.stack(
        [t.tensor for t in labeled_tensors], axis=axis_position, name=scope)
    axes = list(axes_0.values())
    axes.insert(axis_position, new_axis)
    return core.LabeledTensor(pack_op, axes)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def sequence_classifier(decoding, labels, sampling_decoding=None, name=None):
  """Returns predictions and loss for sequence of predictions.

  Args:
    decoding: List of Tensors with predictions.
    labels: List of Tensors with labels.
    sampling_decoding: Optional, List of Tensor with predictions to be used
      in sampling. E.g. they shouldn't have dependncy on outputs.
      If not provided, decoding is used.
    name: Operation name.

  Returns:
    Predictions and losses tensors.
  """
  with ops.name_scope(name, "sequence_classifier", [decoding, labels]):
    predictions, xent_list = [], []
    for i, pred in enumerate(decoding):
      xent_list.append(nn.softmax_cross_entropy_with_logits(
          labels=labels[i], logits=pred,
          name="sequence_loss/xent_raw{0}".format(i)))
      if sampling_decoding:
        predictions.append(nn.softmax(sampling_decoding[i]))
      else:
        predictions.append(nn.softmax(pred))
    xent = math_ops.add_n(xent_list, name="sequence_loss/xent")
    loss = math_ops.reduce_sum(xent, name="sequence_loss")
    return array_ops.stack(predictions, axis=1), loss
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def seq2seq_inputs(x, y, input_length, output_length, sentinel=None, name=None):
  """Processes inputs for Sequence to Sequence models.

  Args:
    x: Input Tensor [batch_size, input_length, embed_dim].
    y: Output Tensor [batch_size, output_length, embed_dim].
    input_length: length of input x.
    output_length: length of output y.
    sentinel: optional first input to decoder and final output expected.
      If sentinel is not provided, zeros are used. Due to fact that y is not
      available in sampling time, shape of sentinel will be inferred from x.
    name: Operation name.

  Returns:
    Encoder input from x, and decoder inputs and outputs from y.
  """
  with ops.name_scope(name, "seq2seq_inputs", [x, y]):
    in_x = array_ops.unstack(x, axis=1)
    y = array_ops.unstack(y, axis=1)
    if not sentinel:
      # Set to zeros of shape of y[0], using x for batch size.
      sentinel_shape = array_ops.stack(
          [array_ops.shape(x)[0], y[0].get_shape()[1]])
      sentinel = array_ops.zeros(sentinel_shape)
      sentinel.set_shape(y[0].get_shape())
    in_y = [sentinel] + y
    out_y = y + [sentinel]
    return in_x, in_y, out_y
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _prepare_inputs_for_rnn(sequence_features, context_features, num_unroll):
  """Prepares features batched by the SQSS for input to a state-saving RNN.

  Args:
    sequence_features: A dict of sequence feature name to `Tensor`, with
      tensors of shape `[batch_size, num_unroll, ...]` and type float32.
    context_features: A dict of context feature name to `Tensor`, with
      tensors of shape `[batch_size, 1, ...]` and type float32.
    num_unroll: Python integer, how many time steps to unroll at a time.
      The input sequences of length `k` are then split into `k / num_unroll`
      many segments.

  Returns:
    features_by_time: A list of length `num_unroll` with `Tensor` entries of
      shape `[batch_size, len(sequence_features) + len(context_features)]` of
      type float32. Features are stored in lexicographic order by their
      corresponding feature dict keys, first in the `sequence_features` and
      then in the `context_features` dicts. Context features are copied into
      each time step.
  """

  def _tile(feature):
    return array_ops.squeeze(
        array_ops.tile(array_ops.expand_dims(feature, 1), [1, num_unroll, 1]),
        axis=2)

  sequence_features = [sequence_features[k] for k in sorted(sequence_features)]
  if not context_features:
    return array_ops.unstack(array_ops.stack(sequence_features, 2), axis=1)
  context_features = [
      _tile(context_features[k]) for k in sorted(context_features)
  ]
  return array_ops.unstack(
      array_ops.stack(sequence_features + context_features, 2), axis=1)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def to_sparse_tensor(self, input_tensor):
    """Creates a SparseTensor from the bucketized Tensor."""
    dimension = self.source_column.dimension
    batch_size = array_ops.shape(input_tensor, name="shape")[0]

    if dimension > 1:
      i1 = array_ops.reshape(
          array_ops.tile(
              array_ops.expand_dims(
                  math_ops.range(0, batch_size), 1, name="expand_dims"),
              [1, dimension],
              name="tile"), [-1],
          name="rehsape")
      i2 = array_ops.tile(
          math_ops.range(0, dimension), [batch_size], name="tile")
      # Flatten the bucket indices and unique them across dimensions
      # E.g. 2nd dimension indices will range from k to 2*k-1 with k buckets
      bucket_indices = array_ops.reshape(
          input_tensor, [-1], name="reshape") + self.length * i2
    else:
      # Simpler indices when dimension=1
      i1 = math_ops.range(0, batch_size)
      i2 = array_ops.zeros([batch_size], dtype=dtypes.int32, name="zeros")
      bucket_indices = array_ops.reshape(input_tensor, [-1], name="reshape")

    indices = math_ops.to_int64(array_ops.transpose(array_ops.stack((i1, i2))))
    shape = math_ops.to_int64(array_ops.stack([batch_size, dimension]))
    sparse_id_values = sparse_tensor_py.SparseTensor(
        indices, bucket_indices, shape)

    return sparse_id_values
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def pack(labeled_tensors, new_axis, axis_position=0, name=None):
  """Pack tensors along a new axis.

  See tf.pack.

  Args:
    labeled_tensors: The input tensors, which must have identical axes.
    new_axis: The name of the new axis, or a tuple containing the name
      and coordinate labels.
    axis_position: Optional integer position at which to insert the new axis.
    name: Optional op name.

  Returns:
    The packed tensors as a single LabeledTensor, with `new_axis` in the given
    `axis_position`.

  Raises:
    ValueError: If fewer than one input tensors is provided, or if the tensors
      don't have identical axes.
  """
  with ops.name_scope(name, 'lt_pack', labeled_tensors) as scope:
    labeled_tensors = [
        core.convert_to_labeled_tensor(lt) for lt in labeled_tensors
    ]

    if len(labeled_tensors) < 1:
      raise ValueError('pack expects at least 1 tensors, but received %s' %
                       labeled_tensors)

    axes_0 = labeled_tensors[0].axes
    for t in labeled_tensors:
      if t.axes != axes_0:
        raise ValueError('Non-identical axes. Expected %s but got %s' %
                         (axes_0, t.axes))

    pack_op = array_ops.stack(
        [t.tensor for t in labeled_tensors], axis=axis_position, name=scope)
    axes = list(axes_0.values())
    axes.insert(axis_position, new_axis)
    return core.LabeledTensor(pack_op, axes)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def test(self):
    pack_lt = ops.pack([self.original_lt, self.original_lt], 'batch')
    golden_lt = core.LabeledTensor(
        array_ops.stack([self.original_lt.tensor, self.original_lt.tensor]),
        ['batch', self.a0, self.a1, self.a2, self.a3])

    self.assertLabeledTensorsEqual(pack_lt, golden_lt)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _define_expectation_operation(self, shard_id):
    # Shape broadcasting.
    probs = array_ops.expand_dims(self._probs[shard_id], 0)
    # Membership weights are computed as:
    # w_{ik} = \frac{\alpha_k f(\mathbf{y_i}|\mathbf{\theta}_k)}
    #               {\sum_{m=1}^{K}\alpha_mf(\mathbf{y_i}|\mathbf{\theta}_m)}
    # where "i" is the i-th example, "k" is the k-th mixture, theta are
    # the model parameters and y_i the observations.
    # These are defined for each shard.
    self._w[shard_id] = array_ops.reshape(
        math_ops.exp(probs - self._prior_probs[shard_id]),
        array_ops.stack([self._num_examples, self._num_classes]))
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _define_distance_to_clusters(self, data):
    """Defines the Mahalanobis distance to the assigned Gaussian."""
    # TODO(xavigonzalvo): reuse (input - mean) * cov^-1 * (input -
    # mean) from log probability function.
    self._all_scores = []
    for shard in data:
      all_scores = []
      shard = array_ops.expand_dims(shard, 0)
      for c in xrange(self._num_classes):
        if self._covariance_type == FULL_COVARIANCE:
          cov = self._covs[c, :, :]
        elif self._covariance_type == DIAG_COVARIANCE:
          cov = array_ops.diag(self._covs[c, :])
        inverse = linalg_ops.matrix_inverse(cov + self._min_var)
        inv_cov = array_ops.tile(
            array_ops.expand_dims(inverse, 0),
            array_ops.stack([self._num_examples, 1, 1]))
        diff = array_ops.transpose(shard - self._means[c, :, :], perm=[1, 0, 2])
        m_left = math_ops.matmul(diff, inv_cov)
        all_scores.append(
            math_ops.sqrt(
                math_ops.matmul(
                    m_left, array_ops.transpose(
                        diff, perm=[0, 2, 1]))))
      self._all_scores.append(
          array_ops.reshape(
              array_ops.concat(all_scores, 1),
              array_ops.stack([self._num_examples, self._num_classes])))

    # Distance to the associated class.
    self._all_scores = array_ops.concat(self._all_scores, 0)
    assignments = array_ops.concat(self.assignments(), 0)
    rows = math_ops.to_int64(math_ops.range(0, self._num_examples))
    indices = array_ops.concat(
        [array_ops.expand_dims(rows, 1), array_ops.expand_dims(assignments, 1)],
        1)
    self._scores = array_ops.gather_nd(self._all_scores, indices)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _shape_tensor(self):
    matrix_shape = array_ops.stack(
        (self._num_rows, self._num_rows), axis=0)
    if self._batch_shape_arg is None:
      return matrix_shape

    return array_ops.concat((self._batch_shape_arg, matrix_shape), 0)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _log_prob(self, x):
    with ops.control_dependencies(self._assertions):
      x = ops.convert_to_tensor(x, name="x")
      distribution_log_probs = [d.log_prob(x) for d in self.components]
      cat_log_probs = self._cat_probs(log_probs=True)
      final_log_probs = [
          cat_lp + d_lp
          for (cat_lp, d_lp) in zip(cat_log_probs, distribution_log_probs)
      ]
      concat_log_probs = array_ops.stack(final_log_probs, 0)
      log_sum_exp = math_ops.reduce_logsumexp(concat_log_probs, [0])
      return log_sum_exp
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def _event_shape(self):
    return array_ops.stack([self._cov.vector_space_dimension()])
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def ndlstm_base_unrolled(inputs, noutput, scope=None, reverse=False):
  """Run an LSTM, either forward or backward.

  This is a 1D LSTM implementation using unrolling and the TensorFlow
  LSTM op.

  Args:
    inputs: input sequence (length, batch_size, ninput)
    noutput: depth of output
    scope: optional scope name
    reverse: run LSTM in reverse

  Returns:
    Output sequence (length, batch_size, noutput)

  """
  with variable_scope.variable_scope(scope, "SeqLstmUnrolled", [inputs]):
    length, batch_size, _ = _shape(inputs)
    lstm_cell = core_rnn_cell_impl.BasicLSTMCell(noutput, state_is_tuple=False)
    state = array_ops.zeros([batch_size, lstm_cell.state_size])
    output_u = []
    inputs_u = array_ops.unstack(inputs)
    if reverse:
      inputs_u = list(reversed(inputs_u))
    for i in xrange(length):
      if i > 0:
        variable_scope.get_variable_scope().reuse_variables()
      output, state = lstm_cell(inputs_u[i], state)
      output_u += [output]
    if reverse:
      output_u = list(reversed(output_u))
    outputs = array_ops.stack(output_u)
    return outputs
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def testConcatNoScalars(self):
    with self.test_session():
      with self.test_scope():
        scalar = constant_op.constant(7)
        dim = array_ops.placeholder(dtypes.int32)
        with self.assertRaisesRegexp(
            ValueError, r"Can't concatenate scalars \(use tf\.stack instead\)"):
          array_ops.concat([scalar, scalar, scalar], dim)
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def testBasic(self):
    with self.test_session() as sess:
      with self.test_scope():
        s0 = constant_op.constant([2, 3, 5], dtypes.int32)
        s1 = constant_op.constant([2, 7, 5], dtypes.int32)
        s2 = constant_op.constant([2, 20, 5], dtypes.int32)
        packed = array_ops.stack([s0, s1, s2])
        ans = sess.run(packed)
        self.assertAllEqual(ans, [[2, 3, 5], [2, 7, 5], [2, 20, 5]])
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def testScalars(self):
    with self.test_session() as sess:
      with self.test_scope():
        s0 = constant_op.constant(2, dtypes.int32)
        s1 = constant_op.constant(3, dtypes.int32)
        s2 = constant_op.constant(5, dtypes.int32)
        packed = array_ops.stack([s0, s1, s2])
        ans = sess.run(packed)
        self.assertAllEqual(ans, [2, 3, 5])
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def testEmpty(self):
    with self.test_session() as sess:
      with self.test_scope():
        s0 = constant_op.constant([[]], dtypes.int32)
        s1 = constant_op.constant([[]], dtypes.int32)
        s2 = constant_op.constant([[]], dtypes.int32)
        packed = array_ops.stack([s0, s1, s2])
        ans = sess.run(packed)
        self.assertAllEqual(ans, [[[]], [[]], [[]]])
项目:HyperGAN    作者:255BITS    | 项目源码 | 文件源码
def crop_to_bounding_box(image, offset_height, offset_width, target_height,
                         target_width, dynamic_shape=False):
  """Crops an image to a specified bounding box.

  This op cuts a rectangular part out of `image`. The top-left corner of the
  returned image is at `offset_height, offset_width` in `image`, and its
  lower-right corner is at
  `offset_height + target_height, offset_width + target_width`.

  Args:
    image: 3-D tensor with shape `[height, width, channels]`
    offset_height: Vertical coordinate of the top-left corner of the result in
                   the input.
    offset_width: Horizontal coordinate of the top-left corner of the result in
                  the input.
    target_height: Height of the result.
    target_width: Width of the result.
    dynamic_shape: Whether the input image has undertermined shape. If set to
      `True`, shape information will be retrieved at run time. Default to
      `False`.

  Returns:
    3-D tensor of image with shape `[target_height, target_width, channels]`

  Raises:
    ValueError: If the shape of `image` is incompatible with the `offset_*` or
    `target_*` arguments, and `dynamic_shape` is set to `False`.
  """
  image = tf.convert_to_tensor(image, name='image')
  _Check3DImage(image, require_static=(not dynamic_shape))
  height, width, _ = _ImageDimensions(image, dynamic_shape=dynamic_shape)

  if not dynamic_shape:
    if offset_width < 0:
      raise ValueError('offset_width must be >= 0.')
    if offset_height < 0:
      raise ValueError('offset_height must be >= 0.')

    if width < (target_width + offset_width):
      raise ValueError('width must be >= target + offset.')
    if height < (target_height + offset_height):
      raise ValueError('height must be >= target + offset.')

  cropped = array_ops.slice(image,
                            array_ops.stack([offset_height, offset_width, 0]),
                            array_ops.stack([target_height, target_width, -1]))

  return cropped


# In[3]:
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def ctc_label_dense_to_sparse(labels, label_lengths):
      """Converts CTC labels from dense to sparse.

      Arguments:
          labels: dense CTC labels.
          label_lengths: length of the labels.

      Returns:
          A sparse tensor representation of the lablels.
      """
      label_shape = array_ops.shape(labels)
      num_batches_tns = array_ops.stack([label_shape[0]])
      max_num_labels_tns = array_ops.stack([label_shape[1]])

      def range_less_than(_, current_input):
        return array_ops.expand_dims(
            math_ops.range(label_shape[1]), 0) < array_ops.fill(
                max_num_labels_tns, current_input)

      init = math_ops.cast(
          array_ops.fill([1, label_shape[1]], 0), dtypes_module.bool)
      dense_mask = functional_ops.scan(
          range_less_than, label_lengths, initializer=init, parallel_iterations=1)
      dense_mask = dense_mask[:, 0, :]

      label_array = array_ops.reshape(
          array_ops.tile(math_ops.range(0, label_shape[1]), num_batches_tns),
          label_shape)
      label_ind = array_ops.boolean_mask(label_array, dense_mask)

      batch_array = array_ops.transpose(
          array_ops.reshape(
              array_ops.tile(math_ops.range(0, label_shape[0]), max_num_labels_tns),
              reverse(label_shape, 0)))
      batch_ind = array_ops.boolean_mask(batch_array, dense_mask)
      indices = array_ops.transpose(
          array_ops.reshape(concatenate([batch_ind, label_ind], axis=0), [2, -1]))

      vals_sparse = array_ops.gather_nd(labels, indices)

      return sparse_tensor.SparseTensor(
          math_ops.to_int64(indices), vals_sparse, math_ops.to_int64(label_shape))
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def call(self, inputs):
            if self._reshape_required:
              reshaped_inputs = []
              input_ndims = list(map(K.ndim, inputs))
              if None not in input_ndims:
                # If ranks of all inputs are available,
                # we simply expand each of them at axis=1
                # until all of them have the same rank.
                max_ndim = max(input_ndims)
                for x in inputs:
                  x_ndim = K.ndim(x)
                  for _ in range(max_ndim - x_ndim):
                    x = K.expand_dims(x, 1)
                  reshaped_inputs.append(x)
                return self._merge_function(reshaped_inputs)
              else:
                # Transpose all inputs so that batch size is the last dimension.
                # (batch_size, dim1, dim2, ... ) -> (dim1, dim2, ... , batch_size)
                transposed = False
                for x in inputs:
                  x_ndim = K.ndim(x)
                  if x_ndim is None:
                    x_shape = K.shape(x)
                    batch_size = x_shape[0]
                    new_shape = K.concatenate([x_shape[1:], K.expand_dims(batch_size)])
                    x_transposed = K.reshape(x,
                                             K.stack([batch_size,
                                                      K.prod(x_shape[1:])]))
                    x_transposed = K.permute_dimensions(x_transposed, (1, 0))
                    x_transposed = K.reshape(x_transposed, new_shape)
                    reshaped_inputs.append(x_transposed)
                    transposed = True
                  elif x_ndim > 1:
                    dims = list(range(1, x_ndim)) + [0]
                    reshaped_inputs.append(K.permute_dimensions(x, dims))
                    transposed = True
                  else:
                    # We don't transpose inputs if they are 1D vectors or scalars.
                    reshaped_inputs.append(x)
                y = self._merge_function(reshaped_inputs)
                y_ndim = K.ndim(y)
                if transposed:
                  # If inputs have been transposed, we have to transpose the output too.
                  if y_ndim is None:
                    y_shape = K.shape(y)
                    y_ndim = K.shape(y_shape)[0]
                    batch_size = y_shape[y_ndim - 1]
                    new_shape = K.concatenate(
                        [K.expand_dims(batch_size), y_shape[:y_ndim - 1]])
                    y = K.reshape(y, (-1, batch_size))
                    y = K.permute_dimensions(y, (1, 0))
                    y = K.reshape(y, new_shape)
                  elif y_ndim > 1:
                    dims = [y_ndim - 1] + list(range(y_ndim - 1))
                    y = K.permute_dimensions(y, dims)
                return y
            else:
              return self._merge_function(inputs)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def _time_distributed_dense(x,
                                    w,
                                    b=None,
                                    dropout=None,
                                    input_dim=None,
                                    output_dim=None,
                                    timesteps=None,
                                    training=None):
          """Apply `y . w + b` for every temporal slice y of x.

          Arguments:
              x: input tensor.
              w: weight matrix.
              b: optional bias vector.
              dropout: wether to apply dropout (same dropout mask
                  for every temporal slice of the input).
              input_dim: integer; optional dimensionality of the input.
              output_dim: integer; optional dimensionality of the output.
              timesteps: integer; optional number of timesteps.
              training: training phase tensor or boolean.

          Returns:
              Output tensor.
          """
          if not input_dim:
            input_dim = K.shape(x)[2]
          if not timesteps:
            timesteps = K.shape(x)[1]
          if not output_dim:
            output_dim = K.shape(w)[1]

          if dropout is not None and 0. < dropout < 1.:
            # apply the same dropout pattern at every timestep
            ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
            dropout_matrix = K.dropout(ones, dropout)
            expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
            x = K.in_train_phase(x * expanded_dropout_matrix, x, training=training)

          # collapse time dimension and batch dimension together
          x = K.reshape(x, (-1, input_dim))
          x = K.dot(x, w)
          if b is not None:
            x = K.bias_add(x, b)
          # reshape to 3D tensor
          if K.backend() == 'tensorflow':
            x = K.reshape(x, K.stack([-1, timesteps, output_dim]))
            x.set_shape([None, None, output_dim])
          else:
            x = K.reshape(x, (-1, timesteps, output_dim))
          return x
项目:WhatTheFuck    作者:wangqingbaidu    | 项目源码 | 文件源码
def repeat(inputs, repetitions, layer, *args, **kwargs):
  """Applies the same layer with the same arguments repeatedly.

  ```python
    y = repeat(x, 3, conv2d, 64, [3, 3], scope='conv1')
    # It is equivalent to:

    x = conv2d(x, 64, [3, 3], scope='conv1/conv1_1')
    x = conv2d(x, 64, [3, 3], scope='conv1/conv1_2')
    y = conv2d(x, 64, [3, 3], scope='conv1/conv1_3')

If the scope argument is not given in kwargs, it is set to layer.__name__, or layer.func.__name__ (for functools.partial objects). If neither __name__ nor func.__name__ is available, the layers are called with scope='stack'.

Args: inputs: A Tensor suitable for layer. repetitions: Int, number of repetitions. layer: A layer with arguments (inputs, *args, **kwargs) *args: Extra args for the layer. **kwargs: Extra kwargs for the layer.

Returns: A tensor result of applying the layer, repetitions times. Raises: ValueError: If the op is unknown or wrong. """ scope = kwargs.pop('scope', None) with variable_scope.variable_scope(scope, 'Repeat', [inputs]): inputs = ops.convert_totensor(inputs) if scope is None: if hasattr(layer, 'name'): scope = layer.name elif hasattr(layer, 'func') and hasattr(layer.func, 'name'): scope = layer.func.name # In case layer is a functools.partial. else: scope = 'repeat' outputs = inputs for i in range(repetitions): kwargs['scope'] = scope + '' + str(i+1) outputs = layer(outputs, *args, **kwargs) return outputs ```

项目:WhatTheFuck    作者:wangqingbaidu    | 项目源码 | 文件源码
def stack(inputs, layer, stack_args, **kwargs):
  """Builds a stack of layers by applying layer repeatedly using stack_args.

  `stack` allows you to repeatedly apply the same operation with different
  arguments `stack_args[i]`. For each application of the layer, `stack` creates
  a new scope appended with an increasing number. For example:

  ```python
    y = stack(x, fully_connected, [32, 64, 128], scope='fc')
    # It is equivalent to:

    x = fully_connected(x, 32, scope='fc/fc_1')
    x = fully_connected(x, 64, scope='fc/fc_2')
    y = fully_connected(x, 128, scope='fc/fc_3')

If the scope argument is not given in kwargs, it is set to layer.__name__, or layer.func.__name__ (for functools.partial objects). If neither __name__ nor func.__name__ is available, the layers are called with scope='stack'.

Args: inputs: A Tensor suitable for layer. layer: A layer with arguments (inputs, *args, **kwargs) stack_args: A list/tuple of parameters for each call of layer. **kwargs: Extra kwargs for the layer.

Returns: A Tensor result of applying the stacked layers.

Raises: ValueError: If the op is unknown or wrong. """ scope = kwargs.pop('scope', None) if not isinstance(stack_args, (list, tuple)): raise ValueError('stack_args need to be a list or tuple') with variable_scope.variable_scope(scope, 'Stack', [inputs]): inputs = ops.convert_to_tensor(inputs) if scope is None: if hasattr(layer, 'name'): scope = layer.name elif hasattr(layer, 'func') and hasattr(layer.func, 'name'): scope = layer.func.name # In case layer is a functools.partial. else: scope = 'stack' outputs = inputs for i in range(len(stackargs)): kwargs['scope'] = scope + '' + str(i+1) layer_args = stack_args[i] if not isinstance(layer_args, (list, tuple)): layer_args = [layer_args] outputs = layer(outputs, *layer_args, **kwargs) return outputs ```

项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def construct_state_saving_rnn(cell,
                               inputs,
                               num_label_columns,
                               state_saver,
                               state_name,
                               scope='rnn'):
  """Build a state saving RNN and apply a fully connected layer.

  Args:
    cell: An instance of `RNNCell`.
    inputs: A length `T` list of inputs, each a `Tensor` of shape
      `[batch_size, input_size, ...]`.
    num_label_columns: The desired output dimension.
    state_saver: A state saver object with methods `state` and `save_state`.
    state_name: Python string or tuple of strings.  The name to use with the
      state_saver. If the cell returns tuples of states (i.e.,
      `cell.state_size` is a tuple) then `state_name` should be a tuple of
      strings having the same length as `cell.state_size`.  Otherwise it should
      be a single string.
    scope: `VariableScope` for the created subgraph; defaults to "rnn".

  Returns:
    activations: The output of the RNN, projected to `num_label_columns`
      dimensions, a `Tensor` of shape `[batch_size, T, num_label_columns]`.
    final_state: The final state output by the RNN
  """
  with ops.name_scope(scope):
    rnn_outputs, final_state = core_rnn.static_state_saving_rnn(
        cell=cell,
        inputs=inputs,
        state_saver=state_saver,
        state_name=state_name,
        scope=scope)
    # Convert rnn_outputs from a list of time-major order Tensors to a single
    # Tensor of batch-major order.
    rnn_outputs = array_ops.stack(rnn_outputs, axis=1)
    activations = layers.fully_connected(
        inputs=rnn_outputs,
        num_outputs=num_label_columns,
        activation_fn=None,
        trainable=True)
    # Use `identity` to rename `final_state`.
    final_state = array_ops.identity(final_state, name=RNNKeys.FINAL_STATE_KEY)
    return activations, final_state
项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def repeat(inputs, repetitions, layer, *args, **kwargs):
  """Applies the same layer with the same arguments repeatedly.

  ```python
    y = repeat(x, 3, conv2d, 64, [3, 3], scope='conv1')
    # It is equivalent to:

    x = conv2d(x, 64, [3, 3], scope='conv1/conv1_1')
    x = conv2d(x, 64, [3, 3], scope='conv1/conv1_2')
    y = conv2d(x, 64, [3, 3], scope='conv1/conv1_3')

If the scope argument is not given in kwargs, it is set to layer.__name__, or layer.func.__name__ (for functools.partial objects). If neither __name__ nor func.__name__ is available, the layers are called with scope='stack'.

Args: inputs: A Tensor suitable for layer. repetitions: Int, number of repetitions. layer: A layer with arguments (inputs, *args, **kwargs) *args: Extra args for the layer. **kwargs: Extra kwargs for the layer.

Returns: A tensor result of applying the layer, repetitions times. Raises: ValueError: If the op is unknown or wrong. """ scope = kwargs.pop('scope', None) with variable_scope.variable_scope(scope, 'Repeat', [inputs]): inputs = ops.convert_totensor(inputs) if scope is None: if hasattr(layer, 'name'): scope = layer.name elif hasattr(layer, 'func') and hasattr(layer.func, 'name'): scope = layer.func.name # In case layer is a functools.partial. else: scope = 'repeat' outputs = inputs for i in range(repetitions): kwargs['scope'] = scope + '' + str(i+1) outputs = layer(outputs, *args, **kwargs) return outputs ```

项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def stack(inputs, layer, stack_args, **kwargs):
  """Builds a stack of layers by applying layer repeatedly using stack_args.

  `stack` allows you to repeatedly apply the same operation with different
  arguments `stack_args[i]`. For each application of the layer, `stack` creates
  a new scope appended with an increasing number. For example:

  ```python
    y = stack(x, fully_connected, [32, 64, 128], scope='fc')
    # It is equivalent to:

    x = fully_connected(x, 32, scope='fc/fc_1')
    x = fully_connected(x, 64, scope='fc/fc_2')
    y = fully_connected(x, 128, scope='fc/fc_3')

If the scope argument is not given in kwargs, it is set to layer.__name__, or layer.func.__name__ (for functools.partial objects). If neither __name__ nor func.__name__ is available, the layers are called with scope='stack'.

Args: inputs: A Tensor suitable for layer. layer: A layer with arguments (inputs, *args, **kwargs) stack_args: A list/tuple of parameters for each call of layer. **kwargs: Extra kwargs for the layer.

Returns: A Tensor result of applying the stacked layers.

Raises: ValueError: If the op is unknown or wrong. """ scope = kwargs.pop('scope', None) if not isinstance(stack_args, (list, tuple)): raise ValueError('stack_args need to be a list or tuple') with variable_scope.variable_scope(scope, 'Stack', [inputs]): inputs = ops.convert_to_tensor(inputs) if scope is None: if hasattr(layer, 'name'): scope = layer.name elif hasattr(layer, 'func') and hasattr(layer.func, 'name'): scope = layer.func.name # In case layer is a functools.partial. else: scope = 'stack' outputs = inputs for i in range(len(stackargs)): kwargs['scope'] = scope + '' + str(i+1) layer_args = stack_args[i] if not isinstance(layer_args, (list, tuple)): layer_args = [layer_args] outputs = layer(outputs, *layer_args, **kwargs) return outputs ```

项目:DeepLearning_VirtualReality_BigData_Project    作者:rashmitripathi    | 项目源码 | 文件源码
def __init__(self,
               data,
               num_classes,
               initial_means=None,
               params='wmc',
               covariance_type=FULL_COVARIANCE,
               random_seed=0):
    """Constructor.

    Args:
      data: a list of Tensors with data, each row is a new example.
      num_classes: number of clusters.
      initial_means: a Tensor with a matrix of means. If None, means are
        computed by sampling randomly.
      params: Controls which parameters are updated in the training
        process. Can contain any combination of "w" for weights, "m" for
        means, and "c" for covariances.
      covariance_type: one of "full", "diag".
      random_seed: Seed for PRNG used to initialize seeds.

    Raises:
      Exception if covariance type is unknown.
    """
    self._params = params
    self._random_seed = random_seed
    self._covariance_type = covariance_type
    if self._covariance_type not in [DIAG_COVARIANCE, FULL_COVARIANCE]:
      raise Exception(  # pylint: disable=g-doc-exception
          'programmer error: Invalid covariance type: %s' %
          self._covariance_type)
    # Create sharded variables for multiple shards. The following
    # lists are indexed by shard.
    # Probability per example in a class.
    num_shards = len(data)
    self._probs = [None] * num_shards
    # Prior probability.
    self._prior_probs = [None] * num_shards
    # Membership weights w_{ik} where "i" is the i-th example and "k"
    # is the k-th mixture.
    self._w = [None] * num_shards
    # Number of examples in a class.
    self._points_in_k = [None] * num_shards
    first_shard = data[0]
    self._dimensions = array_ops.shape(first_shard)[1]
    self._num_classes = num_classes
    # Small value to guarantee that covariances are invertible.
    self._min_var = array_ops.diag(
        array_ops.ones(array_ops.stack([self._dimensions]))) * 1e-3
    self._create_variables(data, initial_means)
    # Operations of partial statistics for the computation of the means.
    self._w_mul_x = []
    # Operations of partial statistics for the computation of the covariances.
    self._w_mul_x2 = []
    self._define_graph(data)