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

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

项目:Question-Answering    作者:MurtyShikhar    | 项目源码 | 文件源码
def hardmax(logits, name=None):
  """Returns batched one-hot vectors.

  The depth index containing the `1` is that of the maximum logit value.

  Args:
    logits: A batch tensor of logit values.
    name: Name to use when creating ops.
  Returns:
    A batched one-hot tensor.
  """
  with ops.name_scope(name, "Hardmax", [logits]):
    logits = ops.convert_to_tensor(logits, name="logits")
    if logits.get_shape()[-1].value is not None:
      depth = logits.get_shape()[-1].value
    else:
      depth = array_ops.shape(logits)[-1]
    return array_ops.one_hot(
        math_ops.argmax(logits, -1), depth, dtype=logits.dtype)
项目:opinatt    作者:epochx    | 项目源码 | 文件源码
def get_classification_loss(logits, targets, softmax_loss_function=None):
  bucket_outputs = logits
  if softmax_loss_function is None:
    assert len(bucket_outputs) == len(targets) == 1
    # We need to make target an int64-tensor and set its shape.
    bucket_target = array_ops.reshape(math_ops.to_int64(targets[0]), [-1])
    crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(logits=bucket_outputs[0],
                                                               labels=bucket_target)
  else:
    assert len(bucket_outputs) == len(targets) == 1
    crossent = softmax_loss_function(bucket_outputs[0], targets[0])

  batch_size = array_ops.shape(targets[0])[0]
  loss = tf.reduce_sum(crossent) / math_ops.cast(batch_size, dtypes.float32)

  return loss
项目:DeepNovo    作者:nh2tran    | 项目源码 | 文件源码
def _build_embedding_AAid(self, input_AAid):
    """TODO(nh2tran): docstring.

       Inputs:
         input_AAid: list of 2 tensors [batch_size].

       Outputs:
         embedding_AAid: list of 2 tensors [batch_size, embedding_size].
    """

    print("".join(["="] * 80)) # section-separating line
    print("ModelNetwork: _build_embedding_AAid()")

    scope = "embedding_rnn_seq2seq/embedding_rnn_decoder" # TODO(nh2tran): to change to "embedding_AAid"
    with tf.variable_scope(scope):

      with ops.device("/cpu:0"):
        embedding = tf.get_variable(
            name="embedding",
            shape=[self.vocab_size, self.embedding_size])

      embedding_AAid = [embedding_ops.embedding_lookup(embedding, x)
                        for x in input_AAid]

    return embedding_AAid
项目:deep-learning    作者:lbkchen    | 项目源码 | 文件源码
def inference_graph(self, input_data, data_spec):
    """Constructs a TF graph for evaluating a random tree.

    Args:
      input_data: A tensor or SparseTensor or placeholder for input data.
      data_spec: A list of tf.dtype values specifying the original types of
        each column.

    Returns:
      The last op in the random tree inference graph.
    """
    sparse_indices = []
    sparse_values = []
    sparse_shape = []
    if isinstance(input_data, ops.SparseTensor):
      sparse_indices = input_data.indices
      sparse_values = input_data.values
      sparse_shape = input_data.shape
      input_data = []
    return self.inference_ops.tree_predictions(
        input_data, sparse_indices, sparse_values, sparse_shape, data_spec,
        self.variables.tree,
        self.variables.tree_thresholds,
        self.variables.node_sums,
        valid_leaf_threshold=self.params.valid_leaf_threshold)
项目:deep-learning    作者:lbkchen    | 项目源码 | 文件源码
def average_impurity(self):
    """Constructs a TF graph for evaluating the average leaf impurity of a tree.

    If in regression mode, this is the leaf variance. If in classification mode,
    this is the gini impurity.

    Returns:
      The last op in the graph.
    """
    children = array_ops.squeeze(array_ops.slice(
        self.variables.tree, [0, 0], [-1, 1]), squeeze_dims=[1])
    is_leaf = math_ops.equal(constants.LEAF_NODE, children)
    leaves = math_ops.to_int32(array_ops.squeeze(array_ops.where(is_leaf),
                                                 squeeze_dims=[1]))
    counts = array_ops.gather(self.variables.node_sums, leaves)
    gini = self._weighted_gini(counts)
    # Guard against step 1, when there often are no leaves yet.
    def impurity():
      return gini
    # Since average impurity can be used for loss, when there's no data just
    # return a big number so that loss always decreases.
    def big():
      return array_ops.ones_like(gini, dtype=dtypes.float32) * 10000000.
    return control_flow_ops.cond(math_ops.greater(
        array_ops.shape(leaves)[0], 0), impurity, big)
项目:deep-text-corrector    作者:atpaino    | 项目源码 | 文件源码
def basic_rnn_seq2seq(
        encoder_inputs, decoder_inputs, cell, dtype=dtypes.float32, scope=None):
    """Basic RNN sequence-to-sequence model.

    This model first runs an RNN to encode encoder_inputs into a state vector,
    then runs decoder, initialized with the last encoder state, on decoder_inputs.
    Encoder and decoder use the same RNN cell type, but don't share parameters.

    Args:
      encoder_inputs: A list of 2D Tensors [batch_size x input_size].
      decoder_inputs: A list of 2D Tensors [batch_size x input_size].
      cell: rnn_cell.RNNCell defining the cell function and size.
      dtype: The dtype of the initial state of the RNN cell (default: tf.float32).
      scope: VariableScope for the created subgraph; default: "basic_rnn_seq2seq".

    Returns:
      A tuple of the form (outputs, state), where:
        outputs: A list of the same length as decoder_inputs of 2D Tensors with
          shape [batch_size x output_size] containing the generated outputs.
        state: The state of each decoder cell in the final time-step.
          It is a 2D Tensor of shape [batch_size x cell.state_size].
    """
    with variable_scope.variable_scope(scope or "basic_rnn_seq2seq"):
        _, enc_state = rnn.rnn(cell, encoder_inputs, dtype=dtype)
        return rnn_decoder(decoder_inputs, enc_state, cell)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def zeros_like(x, dtype=None, name=None):
      """Instantiates an all-zeros variable of the same shape as another tensor.

      Arguments:
          x: Keras variable or Keras tensor.
          dtype: String, dtype of returned Keras variable.
               None uses the dtype of x.
          name: String, name for the variable to create.

      Returns:
          A Keras variable with the shape of x filled with zeros.

      Example:
      ```python
          >>> from keras import backend as K
          >>> kvar = K.variable(np.random.random((2,3)))
          >>> kvar_zeros = K.zeros_like(kvar)
          >>> K.eval(kvar_zeros)
          array([[ 0.,  0.,  0.],
                 [ 0.,  0.,  0.]], dtype=float32)
"""
  return array_ops.zeros_like(x, dtype=dtype, name=name)

```

项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def ones_like(x, dtype=None, name=None):
      """Instantiates an all-ones variable of the same shape as another tensor.

      Arguments:
          x: Keras variable or tensor.
          dtype: String, dtype of returned Keras variable.
               None uses the dtype of x.
          name: String, name for the variable to create.

      Returns:
          A Keras variable with the shape of x filled with ones.

      Example:
      ```python
          >>> from keras import backend as K
          >>> kvar = K.variable(np.random.random((2,3)))
          >>> kvar_ones = K.ones_like(kvar)
          >>> K.eval(kvar_ones)
          array([[ 1.,  1.,  1.],
                 [ 1.,  1.,  1.]], dtype=float32)
"""
  return array_ops.ones_like(x, dtype=dtype, name=name)

```

项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def count_params(x):
      """Returns the number of scalars in a Keras variable.

      Arguments:
          x: Keras variable.

      Returns:
          Integer, the number of scalars in `x`.

      Example:
      ```python
          >>> kvar = K.zeros((2,3))
          >>> K.count_params(kvar)
          6
          >>> K.eval(kvar)
          array([[ 0.,  0.,  0.],
                 [ 0.,  0.,  0.]], dtype=float32)
"""
  shape = x.get_shape()
  return np.prod([shape[i]._value for i in range(len(shape))])

```

项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def moving_average_update(x, value, momentum):
      """Compute the moving average of a variable.

      Arguments:
          x: A Variable.
          value: A tensor with the same shape as `variable`.
          momentum: The moving average momentum.

      Returns:
          An Operation to update the variable.
      """
      return moving_averages.assign_moving_average(
          x, value, momentum, zero_debias=False)


    # LINEAR ALGEBRA
项目: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 one_hot(indices, num_classes):
      """Computes the one-hot representation of an integer tensor.

      Arguments:
          indices: nD integer tensor of shape
              `(batch_size, dim1, dim2, ... dim(n-1))`
          num_classes: Integer, number of classes to consider.

      Returns:
          (n + 1)D one hot representation of the input
          with shape `(batch_size, dim1, dim2, ... dim(n-1), num_classes)`

      Returns:
          The one-hot tensor.
      """
      return array_ops.one_hot(indices, depth=num_classes, axis=-1)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def set_value(x, value):
      """Sets the value of a variable, from a Numpy array.

      Arguments:
          x: Tensor to set to a new value.
          value: Value to set the tensor to, as a Numpy array
              (of the same shape).
      """
      value = np.asarray(value)
      tf_dtype = _convert_string_dtype(x.dtype.name.split('_')[0])
      if hasattr(x, '_assign_placeholder'):
        assign_placeholder = x._assign_placeholder
        assign_op = x._assign_op
      else:
        assign_placeholder = array_ops.placeholder(tf_dtype, shape=value.shape)
        assign_op = x.assign(assign_placeholder)
        x._assign_placeholder = assign_placeholder
        x._assign_op = assign_op
      get_session().run(assign_op, feed_dict={assign_placeholder: value})
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def batch_set_value(tuples):
      """Sets the values of many tensor variables at once.

      Arguments:
          tuples: a list of tuples `(tensor, value)`.
              `value` should be a Numpy array.
      """
      if tuples:
        assign_ops = []
        feed_dict = {}
        for x, value in tuples:
          value = np.asarray(value)
          tf_dtype = _convert_string_dtype(x.dtype.name.split('_')[0])
          if hasattr(x, '_assign_placeholder'):
            assign_placeholder = x._assign_placeholder
            assign_op = x._assign_op
          else:
            assign_placeholder = array_ops.placeholder(tf_dtype, shape=value.shape)
            assign_op = x.assign(assign_placeholder)
            x._assign_placeholder = assign_placeholder
            x._assign_op = assign_op
          assign_ops.append(assign_op)
          feed_dict[assign_placeholder] = value
        get_session().run(assign_ops, feed_dict=feed_dict)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def in_test_phase(x, alt, training=None):
      """Selects `x` in test phase, and `alt` otherwise.

      Note that `alt` should have the *same shape* as `x`.

      Arguments:
          x: What to return in test phase
              (tensor or callable that returns a tensor).
          alt: What to return otherwise
              (tensor or callable that returns a tensor).
          training: Optional scalar tensor
              (or Python boolean, or Python integer)
              specifying the learning phase.

      Returns:
          Either `x` or `alt` based on `K.learning_phase`.
      """
      return in_train_phase(alt, x, training=training)


    # NN OPERATIONS
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def dropout(x, level, noise_shape=None, seed=None):
      """Sets entries in `x` to zero at random, while scaling the entire tensor.

      Arguments:
          x: tensor
          level: fraction of the entries in the tensor
              that will be set to 0.
          noise_shape: shape for randomly generated keep/drop flags,
              must be broadcastable to the shape of `x`
          seed: random seed to ensure determinism.

      Returns:
          A tensor.
      """
      retain_prob = 1. - level
      if seed is None:
        seed = np.random.randint(10e6)
      # the dummy 1. works around a TF bug
      # (float32_ref vs. float32 incomptability)
      return nn.dropout(x * 1., retain_prob, noise_shape, seed=seed)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def in_top_k(predictions, targets, k):
      """Returns whether the `targets` are in the top `k` `predictions`.

      Arguments:
          predictions: A tensor of shape `(batch_size, classes)` and type `float32`.
          targets: A 1D tensor of length `batch_size` and type `int32` or `int64`.
          k: An `int`, number of top elements to consider.

      Returns:
          A 1D tensor of length `batch_size` and type `bool`.
          `output[i]` is `True` if `predictions[i, targets[i]]` is within top-`k`
          values of `predictions[i]`.
      """
      return nn.in_top_k(predictions, targets, k)


    # CONVOLUTIONS
项目: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
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def _preprocess_conv2d_input(x, data_format):
      """Transpose and cast the input before the conv2d.

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

      Returns:
          A tensor.
      """
      if dtype(x) == 'float64':
        x = math_ops.cast(x, 'float32')
      if data_format == 'channels_first':
        # TF uses the last dimension as channel dimension,
        # instead of the 2nd one.
        # TH input shape: (samples, input_depth, rows, cols)
        # TF input shape: (samples, rows, cols, input_depth)
        x = array_ops.transpose(x, (0, 2, 3, 1))
      return x
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def random_uniform(shape, minval=0.0, maxval=1.0, dtype=None, seed=None):
      """Returns a tensor with uniform distribution of values.

      Arguments:
          shape: A tuple of integers, the shape of tensor to create.
          minval: A float, lower boundary of the uniform distribution
              to draw samples.
          maxval: A float, upper boundary of the uniform distribution
              to draw samples.
          dtype: String, dtype of returned tensor.
          seed: Integer, random seed.

      Returns:
          A tensor.
      """
      if dtype is None:
        dtype = floatx()
      if seed is None:
        seed = np.random.randint(10e6)
      return random_ops.random_uniform(
          shape, minval=minval, maxval=maxval, dtype=dtype, seed=seed)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def random_binomial(shape, p=0.0, dtype=None, seed=None):
      """Returns a tensor with random binomial distribution of values.

      Arguments:
          shape: A tuple of integers, the shape of tensor to create.
          p: A float, `0. <= p <= 1`, probability of binomial distribution.
          dtype: String, dtype of returned tensor.
          seed: Integer, random seed.

      Returns:
          A tensor.
      """
      if dtype is None:
        dtype = floatx()
      if seed is None:
        seed = np.random.randint(10e6)
      return array_ops.where(
          random_ops.random_uniform(shape, dtype=dtype, seed=seed) <= p,
          array_ops.ones(shape, dtype=dtype), array_ops.zeros(shape, dtype=dtype))
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def foldr(fn, elems, initializer=None, name=None):
      """Reduce elems using fn to combine them from right to left.

      Arguments:
          fn: Callable that will be called upon each element in elems and an
              accumulator, for instance `lambda acc, x: acc + x`
          elems: tensor
          initializer: The first value used (`elems[-1]` in case of None)
          name: A string name for the foldr node in the graph

      Returns:
          Same type and shape as initializer
      """
      return functional_ops.foldr(fn, elems, initializer=initializer, name=name)


    # Load Keras default configuration from config file if present.
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def predict_classes(self, x, batch_size=32, verbose=1):
        """Generate class predictions for the input samples.

        The input samples are processed batch by batch.

        Arguments:
            x: input data, as a Numpy array or list of Numpy arrays
                (if the model has multiple inputs).
            batch_size: integer.
            verbose: verbosity mode, 0 or 1.

        Returns:
            A numpy array of class predictions.
        """
        proba = self.predict(x, batch_size=batch_size, verbose=verbose)
        if proba.shape[-1] > 1:
          return proba.argmax(axis=-1)
        else:
          return (proba > 0.5).astype('int32')
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def get_updates(self, params, constraints, loss):
        grads = self.get_gradients(loss, params)
        shapes = [K.int_shape(p) for p in params]
        accumulators = [K.zeros(shape) for shape in shapes]
        self.weights = accumulators
        self.updates = []

        lr = self.lr
        if self.initial_decay > 0:
          lr *= (1. / (1. + self.decay * self.iterations))
          self.updates.append(K.update_add(self.iterations, 1))

        for p, g, a in zip(params, grads, accumulators):
          # update accumulator
          new_a = self.rho * a + (1. - self.rho) * K.square(g)
          self.updates.append(K.update(a, new_a))
          new_p = p - lr * g / (K.sqrt(new_a) + self.epsilon)

          # apply constraints
          if p in constraints:
            c = constraints[p]
            new_p = c(new_p)
          self.updates.append(K.update(p, new_p))
        return self.updates
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def get_updates(self, params, constraints, loss):
        grads = self.get_gradients(loss, params)
        shapes = [K.int_shape(p) for p in params]
        accumulators = [K.zeros(shape) for shape in shapes]
        self.weights = accumulators
        self.updates = []

        lr = self.lr
        if self.initial_decay > 0:
          lr *= (1. / (1. + self.decay * self.iterations))
          self.updates.append(K.update_add(self.iterations, 1))

        for p, g, a in zip(params, grads, accumulators):
          new_a = a + K.square(g)  # update accumulator
          self.updates.append(K.update(a, new_a))
          new_p = p - lr * g / (K.sqrt(new_a) + self.epsilon)
          # apply constraints
          if p in constraints:
            c = constraints[p]
            new_p = c(new_p)
          self.updates.append(K.update(p, new_p))
        return self.updates
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def get_test_data(train_samples,
                      test_samples,
                      input_shape,
                      num_classes):
      """Generates test data to train a model on.

      Arguments:
        train_samples: Integer, how many training samples to generate.
        test_samples: Integer, how many test samples to generate.
        input_shape: Tuple of integers, shape of the inputs.
        num_classes: Integer, number of classes for the data and targets.
          Only relevant if `classification=True`.

      Returns:
        A tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
      """
      num_sample = train_samples + test_samples
      templates = 2 * num_classes * np.random.random((num_classes,) + input_shape)
      y = np.random.randint(0, num_classes, size=(num_sample,))
      x = np.zeros((num_sample,) + input_shape)
      for i in range(num_sample):
        x[i] = templates[y[i]] + np.random.normal(loc=0, scale=1., size=input_shape)
      return ((x[:train_samples], y[:train_samples]),
              (x[train_samples:], y[train_samples:]))
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def predict(self, x, **kwargs):
            """Returns the class predictions for the given test data.

            Arguments:
                x: array-like, shape `(n_samples, n_features)`
                    Test samples where n_samples in the number of samples
                    and n_features is the number of features.
                **kwargs: dictionary arguments
                    Legal arguments are the arguments
                    of `Sequential.predict_classes`.

            Returns:
                preds: array-like, shape `(n_samples,)`
                    Class predictions.
            """
            kwargs = self.filter_sk_params(Sequential.predict_classes, kwargs)
            classes = self.model.predict_classes(x, **kwargs)
            return self.classes_[classes]
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def predict(self, x, **kwargs):
            """Returns predictions for the given test data.

            Arguments:
                x: array-like, shape `(n_samples, n_features)`
                    Test samples where n_samples in the number of samples
                    and n_features is the number of features.
                **kwargs: dictionary arguments
                    Legal arguments are the arguments of `Sequential.predict`.

            Returns:
                preds: array-like, shape `(n_samples,)`
                    Predictions.
            """
            kwargs = self.filter_sk_params(Sequential.predict, kwargs)
            return np.squeeze(self.model.predict(x, **kwargs))
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def score(self, x, y, **kwargs):
            """Returns the mean loss on the given test data and labels.

            Arguments:
                x: array-like, shape `(n_samples, n_features)`
                    Test samples where n_samples in the number of samples
                    and n_features is the number of features.
                y: array-like, shape `(n_samples,)`
                    True labels for X.
                **kwargs: dictionary arguments
                    Legal arguments are the arguments of `Sequential.evaluate`.

            Returns:
                score: float
                    Mean accuracy of predictions on X wrt. y.
            """
            kwargs = self.filter_sk_params(Sequential.evaluate, kwargs)
            loss = self.model.evaluate(x, y, **kwargs)
            if isinstance(loss, list):
              return loss[0]
            return loss


# utils
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def convert_kernel(kernel):
          """Converts a Numpy kernel matrix from Theano format to TensorFlow format.

          Also works reciprocally, since the transformation is its own inverse.

          Arguments:
              kernel: Numpy array (3D, 4D or 5D).

          Returns:
              The converted kernel.

          Raises:
              ValueError: in case of invalid kernel shape or invalid data_format.
          """
          kernel = np.asarray(kernel)
          if not 3 <= kernel.ndim <= 5:
            raise ValueError('Invalid kernel shape:', kernel.shape)
          slices = [slice(None, None, -1) for _ in range(kernel.ndim)]
          no_flip = (slice(None, None), slice(None, None))
          slices[-2:] = no_flip
          return np.copy(kernel[slices])
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def __init__(self, datapath, dataset, start=0, end=None, normalizer=None):
            if h5py is None:
              raise ImportError('The use of HDF5Matrix requires '
                                'HDF5 and h5py installed.')

            if datapath not in list(self.refs.keys()):
              f = h5py.File(datapath)
              self.refs[datapath] = f
            else:
              f = self.refs[datapath]
            self.data = f[dataset]
            self.start = start
            if end is None:
              self.end = self.data.shape[0]
            else:
              self.end = end
            self.normalizer = normalizer
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def to_categorical(y, num_classes=None):
          """Converts a class vector (integers) to binary class matrix.

          E.g. for use with categorical_crossentropy.

          Arguments:
              y: class vector to be converted into a matrix
                  (integers from 0 to num_classes).
              num_classes: total number of classes.

          Returns:
              A binary matrix representation of the input.
          """
          y = np.array(y, dtype='int').ravel()
          if not num_classes:
            num_classes = np.max(y) + 1
          n = y.shape[0]
          categorical = np.zeros((n, num_classes))
          categorical[np.arange(n), y] = 1
          return categorical
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def build(self, input_shape):
            input_shape = tensor_shape.TensorShape(input_shape).as_list()
            param_shape = input_shape[1:]
            self.param_broadcast = [False] * len(param_shape)
            if self.shared_axes is not None:
              for i in self.shared_axes:
                param_shape[i - 1] = 1
                self.param_broadcast[i - 1] = True
            self.alpha = self.add_weight(
                shape=param_shape,
                name='alpha',
                initializer=self.alpha_initializer,
                regularizer=self.alpha_regularizer,
                constraint=self.alpha_constraint)
            # Set input spec
            axes = {}
            if self.shared_axes:
              for i in range(1, len(input_shape)):
                if i not in self.shared_axes:
                  axes[i] = input_shape[i]
            self.input_spec = InputSpec(ndim=len(input_shape), axes=axes)
            self.built = True
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def compute_output_shape(self, input_shape):
            if input_shape[0] is None:
              output_shape = None
            else:
              output_shape = input_shape[0][1:]
            for i in range(1, len(input_shape)):
              if input_shape[i] is None:
                shape = None
              else:
                shape = input_shape[i][1:]
              output_shape = self._compute_elemwise_op_output_shape(output_shape, shape)
            batch_sizes = [s[0] for s in input_shape if s is not None]
            batch_sizes = set(batch_sizes)
            batch_sizes -= set([None])
            if len(batch_sizes) == 1:
              output_shape = (list(batch_sizes)[0],) + output_shape
            else:
              output_shape = (None,) + output_shape
            return output_shape
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def build(self, input_shape):
            # Used purely for shape validation.
            if not isinstance(input_shape, list) or len(input_shape) != 2:
              raise ValueError('A `Dot` layer should be called '
                               'on a list of 2 inputs.')
            shape1 = tensor_shape.TensorShape(input_shape[0]).as_list()
            shape2 = tensor_shape.TensorShape(input_shape[1]).as_list()
            if shape1 is None or shape2 is None:
              return
            if isinstance(self.axes, int):
              if self.axes < 0:
                axes = [self.axes % len(shape1), self.axes % len(shape2)]
              else:
                axes = [self.axes] * 2
            else:
              axes = self.axes
            if shape1[axes[0]] != shape2[axes[1]]:
              raise ValueError('Dimension incompatibility '
                               '%s != %s. ' % (shape1[axes[0]], shape2[axes[1]]) +
                               'Layer shapes: %s, %s' % (shape1, shape2))
            self.built = True
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def _compute_output_shape(self, input_shape):
            """Computes the output shape of the layer.

            Assumes that the layer will be built
            to match that input shape provided.

            Arguments:
                input_shape: Shape tuple (tuple of integers)
                    or list of shape tuples (one per output tensor of the layer).
                    Shape tuples can include None for free dimensions,
                    instead of an integer.

            Returns:
                An input shape tuple.
            """
            if isinstance(input_shape, list):
              return [tensor_shape.TensorShape(shape) for shape in input_shape]
            else:
              return tensor_shape.TensorShape(input_shape)
项目:LIE    作者:EmbraceLife    | 项目源码 | 文件源码
def _collect_input_shape(input_tensors):
          """Collects the output shape(s) of a list of Keras tensors.

          Arguments:
              input_tensors: list of input tensors (or single input tensor).

          Returns:
              List of shape tuples (or single tuple), one tuple per input.
          """
          input_tensors = _to_list(input_tensors)
          shapes = []
          for x in input_tensors:
            shapes.append(K.int_shape(x))
          if len(shapes) == 1:
            return shapes[0]
          return shapes
项目:SSD_tensorflow_VOC    作者:LevinJ    | 项目源码 | 文件源码
def _ImageDimensions(image):
    """Returns the dimensions of an image tensor.
    Args:
      image: A 3-D Tensor of shape `[height, width, channels]`.
    Returns:
      A list of `[height, width, channels]` corresponding to the dimensions of the
        input image.  Dimensions that are statically known are python integers,
        otherwise they are integer scalar tensors.
    """
    if image.get_shape().is_fully_defined():
        return image.get_shape().as_list()
    else:
        static_shape = image.get_shape().with_rank(3).as_list()
        dynamic_shape = array_ops.unstack(array_ops.shape(image), 3)
        return [s if s is not None else d
                for s, d in zip(static_shape, dynamic_shape)]
项目:SSD_tensorflow_VOC    作者:LevinJ    | 项目源码 | 文件源码
def fix_image_flip_shape(image, result):
    """Set the shape to 3 dimensional if we don't know anything else.
    Args:
      image: original image size
      result: flipped or transformed image
    Returns:
      An image whose shape is at least None,None,None.
    """
    image_shape = image.get_shape()
    if image_shape == tensor_shape.unknown_shape():
        result.set_shape([None, None, None])
    else:
        result.set_shape(image_shape)
    return result


# =========================================================================== #
# Image + BBoxes methods: cropping, resizing, flipping, ...
# =========================================================================== #
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def assert_scalar_int(tensor):
  """Assert `tensor` is 0-D, of type `tf.int32` or `tf.int64`.

  Args:
    tensor: Tensor to test.
  Returns:
    `tensor`, for chaining.
  Raises:
    ValueError: if `tensor` is not 0-D, of type `tf.int32` or `tf.int64`.
  """
  data_type = tensor.dtype
  if data_type.base_dtype not in [dtypes.int32, dtypes.int64]:
    raise ValueError('Unexpected type %s for %s.' % (data_type, tensor.name))
  shape = tensor.get_shape()
  if shape.ndims != 0:
    raise ValueError('Unexpected shape %s for %s.' % (shape, tensor.name))
  return tensor
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def _is_shape(expected_shape, actual_tensor, actual_shape=None):
  """Returns whether actual_tensor's shape is expected_shape.

  Args:
    expected_shape: Integer list defining the expected shape, or tensor of same.
    actual_tensor: Tensor to test.
    actual_shape: Shape of actual_tensor, if we already have it.
  Returns:
    New tensor.
  """
  with ops.name_scope('is_shape', values=[actual_tensor]) as scope:
    is_rank = _is_rank(array_ops.size(expected_shape), actual_tensor)
    if actual_shape is None:
      actual_shape = array_ops.shape(actual_tensor, name='actual')
    shape_equal = _all_equal(
        ops.convert_to_tensor(expected_shape, name='expected'),
        actual_shape)
    return math_ops.logical_and(is_rank, shape_equal, name=scope)
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def _assert_shape_op(expected_shape, actual_tensor):
  """Asserts actual_tensor's shape is expected_shape.

  Args:
    expected_shape: List of integers defining the expected shape, or tensor of
        same.
    actual_tensor: Tensor to test.
  Returns:
    New assert tensor.
  """
  with ops.name_scope('assert_shape', values=[actual_tensor]) as scope:
    actual_shape = array_ops.shape(actual_tensor, name='actual')
    is_shape = _is_shape(expected_shape, actual_tensor, actual_shape)
    return control_flow_ops.Assert(
        is_shape, [
            'Wrong shape for %s [expected] [actual].' % actual_tensor.name,
            expected_shape,
            actual_shape
        ], name=scope)
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def _lengths_to_masks(lengths, max_length):
  """Creates a binary matrix that can be used to mask away padding.

  Args:
    lengths: A vector of integers representing lengths.
    max_length: An integer indicating the maximum length. All values in
      lengths should be less than max_length.
  Returns:
    masks: Masks that can be used to get rid of padding.
  """
  tiled_ranges = array_ops.tile(
      array_ops.expand_dims(math_ops.range(max_length), 0),
      [array_ops.shape(lengths)[0], 1])
  lengths = array_ops.expand_dims(lengths, 1)
  masks = math_ops.to_float(
      math_ops.to_int64(tiled_ranges) < math_ops.to_int64(lengths))
  return masks
项目:lsdc    作者:febert    | 项目源码 | 文件源码
def _safe_div(numerator, denominator, name="value"):
  """Computes a safe divide which returns 0 if the denominator is zero.

  Note that the function contains an additional conditional check that is
  necessary for avoiding situations where the loss is zero causing NaNs to
  creep into the gradient computation.

  Args:
    numerator: An arbitrary `Tensor`.
    denominator: A `Tensor` whose shape matches `numerator` and whose values are
      assumed to be non-negative.
    name: An optional name for the returned op.

  Returns:
    The element-wise value of the numerator divided by the denominator.
  """
  return math_ops.select(
      math_ops.greater(denominator, 0),
      math_ops.div(numerator, math_ops.select(
          math_ops.equal(denominator, 0),
          array_ops.ones_like(denominator), denominator)),
      array_ops.zeros_like(numerator),
      name=name)
项目:seq2seq    作者:google    | 项目源码 | 文件源码
def _unstack_ta(inp):
  return tensor_array_ops.TensorArray(
      dtype=inp.dtype, size=array_ops.shape(inp)[0],
      element_shape=inp.get_shape()[1:]).unstack(inp)
项目:seq2seq    作者:google    | 项目源码 | 文件源码
def __init__(self, inputs, sequence_length, time_major=False, name=None):
    """Initializer.

    Args:
      inputs: A (structure of) input tensors.
      sequence_length: An int32 vector tensor.
      time_major: Python bool.  Whether the tensors in `inputs` are time major.
        If `False` (default), they are assumed to be batch major.
      name: Name scope for any created operations.

    Raises:
      ValueError: if `sequence_length` is not a 1D tensor.
    """
    with ops.name_scope(name, "TrainingHelper", [inputs, sequence_length]):
      inputs = ops.convert_to_tensor(inputs, name="inputs")
      if not time_major:
        inputs = nest.map_structure(_transpose_batch_time, inputs)

      self._input_tas = nest.map_structure(_unstack_ta, inputs)
      self._sequence_length = ops.convert_to_tensor(
          sequence_length, name="sequence_length")
      if self._sequence_length.get_shape().ndims != 1:
        raise ValueError(
            "Expected sequence_length to be a vector, but received shape: %s" %
            self._sequence_length.get_shape())

      self._zero_inputs = nest.map_structure(
          lambda inp: array_ops.zeros_like(inp[0, :]), inputs)

      self._batch_size = array_ops.size(sequence_length)
项目:seq2seq    作者:google    | 项目源码 | 文件源码
def __init__(self, inputs, sequence_length, embedding, sampling_probability,
               time_major=False, seed=None, scheduling_seed=None, name=None):
    """Initializer.

    Args:
      inputs: A (structure of) input tensors.
      sequence_length: An int32 vector tensor.
      embedding: A callable that takes a vector tensor of `ids` (argmax ids),
        or the `params` argument for `embedding_lookup`.
      sampling_probability: A 0D `float32` tensor: the probability of sampling
        categorically from the output ids instead of reading directly from the
        inputs.
      time_major: Python bool.  Whether the tensors in `inputs` are time major.
        If `False` (default), they are assumed to be batch major.
      seed: The sampling seed.
      scheduling_seed: The schedule decision rule sampling seed.
      name: Name scope for any created operations.

    Raises:
      ValueError: if `sampling_probability` is not a scalar or vector.
    """
    with ops.name_scope(name, "ScheduledEmbeddingSamplingWrapper",
                        [embedding, sampling_probability]):
      if callable(embedding):
        self._embedding_fn = embedding
      else:
        self._embedding_fn = (
            lambda ids: embedding_ops.embedding_lookup(embedding, ids))
      self._sampling_probability = ops.convert_to_tensor(
          sampling_probability, name="sampling_probability")
      if self._sampling_probability.get_shape().ndims not in (0, 1):
        raise ValueError(
            "sampling_probability must be either a scalar or a vector. "
            "saw shape: %s" % (self._sampling_probability.get_shape()))
      self._seed = seed
      self._scheduling_seed = scheduling_seed
      super(ScheduledEmbeddingTrainingHelper, self).__init__(
          inputs=inputs,
          sequence_length=sequence_length,
          time_major=time_major,
          name=name)
项目:seq2seq    作者:google    | 项目源码 | 文件源码
def next_inputs(self, time, outputs, state, sample_ids, name=None):
    with ops.name_scope(name, "ScheduledEmbeddingTrainingHelperSample",
                        [time, outputs, state, sample_ids]):
      (finished, base_next_inputs, state) = (
          super(ScheduledEmbeddingTrainingHelper, self).next_inputs(
              time=time,
              outputs=outputs,
              state=state,
              sample_ids=sample_ids,
              name=name))

      def maybe_sample():
        """Perform scheduled sampling."""
        where_sampling = math_ops.cast(
            array_ops.where(sample_ids > -1), dtypes.int32)
        where_not_sampling = math_ops.cast(
            array_ops.where(sample_ids <= -1), dtypes.int32)
        where_sampling_flat = array_ops.reshape(where_sampling, [-1])
        where_not_sampling_flat = array_ops.reshape(where_not_sampling, [-1])
        sample_ids_sampling = array_ops.gather(sample_ids, where_sampling_flat)
        inputs_not_sampling = array_ops.gather(
            base_next_inputs, where_not_sampling_flat)
        sampled_next_inputs = self._embedding_fn(sample_ids_sampling)
        base_shape = array_ops.shape(base_next_inputs)
        return (array_ops.scatter_nd(indices=where_sampling,
                                     updates=sampled_next_inputs,
                                     shape=base_shape)
                + array_ops.scatter_nd(indices=where_not_sampling,
                                       updates=inputs_not_sampling,
                                       shape=base_shape))

      all_finished = math_ops.reduce_all(finished)
      next_inputs = control_flow_ops.cond(
          all_finished, lambda: base_next_inputs, maybe_sample)
      return (finished, next_inputs, state)
项目:tensorflow_seq2seq_chatbot    作者:higepon    | 项目源码 | 文件源码
def sequence_loss_by_example(logits, targets, weights,
                             average_across_timesteps=True,
                             softmax_loss_function=None, name=None):
  """Weighted cross-entropy loss for a sequence of logits (per example).

  Args:
    logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].
    targets: List of 1D batch-sized int32 Tensors of the same length as logits.
    weights: List of 1D batch-sized float-Tensors of the same length as logits.
    average_across_timesteps: If set, divide the returned cost by the total
      label weight.
    softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch
      to be used instead of the standard softmax (the default if this is None).
    name: Optional name for this operation, default: "sequence_loss_by_example".

  Returns:
    1D batch-sized float Tensor: The log-perplexity for each sequence.

  Raises:
    ValueError: If len(logits) is different from len(targets) or len(weights).
  """
  if len(targets) != len(logits) or len(weights) != len(logits):
    raise ValueError("Lengths of logits, weights, and targets must be the same "
                     "%d, %d, %d." % (len(logits), len(weights), len(targets)))
  with ops.name_scope( name,
                    "sequence_loss_by_example",logits + targets + weights):
    log_perp_list = []
    for logit, target, weight in zip(logits, targets, weights):
      if softmax_loss_function is None:
        target = array_ops.reshape(target, [-1])
        crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(
            logit, target)
      else:
        crossent = softmax_loss_function(logit, target)
      log_perp_list.append(crossent * weight)
    log_perps = math_ops.add_n(log_perp_list)
    if average_across_timesteps:
      total_size = math_ops.add_n(weights)
      total_size += 1e-12  # Just to avoid division by 0 for all-0 weights.
      log_perps /= total_size
  return log_perps
项目:tensorflow_seq2seq_chatbot    作者:higepon    | 项目源码 | 文件源码
def sequence_loss(logits, targets, weights,
                  average_across_timesteps=True, average_across_batch=True,
                  softmax_loss_function=None, name=None):
  """Weighted cross-entropy loss for a sequence of logits, batch-collapsed.

  Args:
    logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].
    targets: List of 1D batch-sized int32 Tensors of the same length as logits.
    weights: List of 1D batch-sized float-Tensors of the same length as logits.
    average_across_timesteps: If set, divide the returned cost by the total
      label weight.
    average_across_batch: If set, divide the returned cost by the batch size.
    softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch
      to be used instead of the standard softmax (the default if this is None).
    name: Optional name for this operation, defaults to "sequence_loss".

  Returns:
    A scalar float Tensor: The average log-perplexity per symbol (weighted).

  Raises:
    ValueError: If len(logits) is different from len(targets) or len(weights).
  """
  with ops.name_scope( name, "sequence_loss",logits + targets + weights):
    cost = math_ops.reduce_sum(sequence_loss_by_example(
        logits, targets, weights,
        average_across_timesteps=average_across_timesteps,
        softmax_loss_function=softmax_loss_function))
    if average_across_batch:
      batch_size = array_ops.shape(targets[0])[0]
      return cost / math_ops.cast(batch_size, dtypes.float32)
    else:
      return cost