• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python tensorflow.sparse_concat函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.sparse_concat函数的典型用法代码示例。如果您正苦于以下问题:Python sparse_concat函数的具体用法?Python sparse_concat怎么用?Python sparse_concat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了sparse_concat函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: testMismatchedShapesExpandNonconcatDim

  def testMismatchedShapesExpandNonconcatDim(self):
    with self.test_session(use_gpu=False) as sess:
      sp_a = self._SparseTensor_3x3()
      sp_b = self._SparseTensor_3x5()
      sp_c = self._SparseTensor_3x2()
      sp_d = self._SparseTensor_2x3()
      sp_concat_dim0 = tf.sparse_concat(0, [sp_a, sp_b, sp_c, sp_d],
                                        expand_nonconcat_dim=True)
      sp_concat_dim1 = tf.sparse_concat(1, [sp_a, sp_b, sp_c, sp_d],
                                        expand_nonconcat_dim=True)

      sp_concat_dim0_out = sess.run(sp_concat_dim0)
      sp_concat_dim1_out = sess.run(sp_concat_dim1)

      self.assertAllEqual(
          sp_concat_dim0_out.indices,
          [[0, 2], [1, 0], [2, 0], [2, 2], [4, 1], [5, 0], [5, 3], [5, 4],
           [7, 0], [8, 0], [9, 1], [10, 0], [10, 2]])
      self.assertAllEqual(
          sp_concat_dim0_out.values,
          [1, 2, 3, 4, 1, 2, 1, 0, 1, 2, 1, 1, 2])
      self.assertAllEqual(
          sp_concat_dim0_out.shape,
          [11, 5])

      self.assertAllEqual(
          sp_concat_dim1_out.indices,
          [[0, 2], [0, 11], [1, 0], [1, 4], [1, 8], [1, 10], [1, 12], [2, 0],
           [2, 2], [2, 3], [2, 6], [2, 7], [2, 8]])
      self.assertAllEqual(
          sp_concat_dim1_out.values,
          [1, 1, 2, 1, 1, 1, 2, 3, 4, 2, 1, 0, 2])
      self.assertAllEqual(
          sp_concat_dim1_out.shape,
          [3, 13])
开发者ID:0ruben,项目名称:tensorflow,代码行数:35,代码来源:sparse_concat_op_test.py


示例2: testMismatchedRank

  def testMismatchedRank(self):
    with self.test_session(use_gpu=False):
      sp_a = self._SparseTensor_3x3()
      sp_e = self._SparseTensor_2x3x4()

      # Rank mismatches can be caught at shape-inference time
      with self.assertRaises(ValueError):
        tf.sparse_concat(1, [sp_a, sp_e])
开发者ID:shejianmin,项目名称:tensorflow,代码行数:8,代码来源:sparse_concat_op_test.py


示例3: testMismatchedRankExpandNonconcatDim

  def testMismatchedRankExpandNonconcatDim(self):
    with self.test_session(use_gpu=False):
      sp_a = self._SparseTensor_3x3()
      sp_e = self._SparseTensor_2x3x4()

      # Rank mismatches should be caught at shape-inference time, even for
      # expand_nonconcat_dim=True.
      with self.assertRaises(ValueError):
        tf.sparse_concat(1, [sp_a, sp_e], expand_nonconcat_dim=True)
开发者ID:0ruben,项目名称:tensorflow,代码行数:9,代码来源:sparse_concat_op_test.py


示例4: testConcatDim0

  def testConcatDim0(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A, D):
      # [    1]
      # [2    ]
      # [3   4]
      # [  1  ]
      # [1   2]
      sp_a = self._SparseTensor_3x3()
      sp_d = self._SparseTensor_2x3()

      sp_concat = tf.sparse_concat(0, [sp_a, sp_d])

      self.assertEqual(sp_concat.indices.get_shape(), [7, 2])
      self.assertEqual(sp_concat.values.get_shape(), [7])
      self.assertEqual(sp_concat.shape.get_shape(), [2])

      concat_out = sess.run(sp_concat)

      self.assertAllEqual(
          concat_out.indices,
          [[0, 2], [1, 0], [2, 0], [2, 2], [3, 1], [4, 0], [4, 2]])
      self.assertAllEqual(
          concat_out.values, np.array([1, 2, 3, 4, 1, 1, 2]))
      self.assertAllEqual(
          concat_out.shape, np.array([5, 3]))
开发者ID:shejianmin,项目名称:tensorflow,代码行数:26,代码来源:sparse_concat_op_test.py


示例5: testSliceConcat

 def testSliceConcat(self):
     for sp_input in (self._SparseTensorValue_3x4x2(), self._SparseTensor_3x4x2()):
         with self.test_session(use_gpu=False):
             sparse_tensors = tf.sparse_split(sp_input=sp_input, num_split=2, axis=1)
             concat_tensor = tf.sparse_concat(1, sparse_tensors)
             expected_output = self._SparseTensor_3x4x2()
             self.assertAllEqual(concat_tensor.indices.eval(), expected_output.indices.eval())
开发者ID:BloodD,项目名称:tensorflow,代码行数:7,代码来源:sparse_split_op_test.py


示例6: testSliceConcat

 def testSliceConcat(self):
   with self.test_session(use_gpu=False):
     sparse_tensors = tf.sparse_split(1, 2, self._SparseTensor_3x4x2())
     concat_tensor = tf.sparse_concat(1, sparse_tensors)
     expected_output = self._SparseTensor_3x4x2()
     self.assertAllEqual(concat_tensor.indices.eval(),
                         expected_output.indices.eval())
开发者ID:CdricGmd,项目名称:tensorflow,代码行数:7,代码来源:sparse_split_op_test.py


示例7: tensors_to_item

  def tensors_to_item(self, keys_to_tensors):
    """Maps the given dictionary of tensors to a concatenated list of bboxes.

    Args:
      keys_to_tensors: a mapping of TF-Example keys to parsed tensors.

    Returns:
      [time, num_boxes, 4] tensor of bounding box coordinates, in order
          [y_min, x_min, y_max, x_max]. Whether the tensor is a SparseTensor
          or a dense Tensor is determined by the return_dense parameter. Empty
          positions in the sparse tensor are filled with -1.0 values.
    """
    sides = []
    for key in self._full_keys:
      value = keys_to_tensors[key]
      expanded_dims = tf.concat(
          [tf.to_int64(tf.shape(value)),
           tf.constant([1], dtype=tf.int64)], 0)
      side = tf.sparse_reshape(value, expanded_dims)
      sides.append(side)
    bounding_boxes = tf.sparse_concat(2, sides)
    if self._return_dense:
      bounding_boxes = tf.sparse_tensor_to_dense(
          bounding_boxes, default_value=self._default_value)
    return bounding_boxes
开发者ID:Exscotticus,项目名称:models,代码行数:25,代码来源:tf_sequence_example_decoder.py


示例8: testMismatchedShapes

  def testMismatchedShapes(self):
    with self.test_session(use_gpu=False) as sess:
      sp_a = self._SparseTensor_3x3()
      sp_b = self._SparseTensor_3x5()
      sp_c = self._SparseTensor_3x2()
      sp_d = self._SparseTensor_2x3()
      sp_concat = tf.sparse_concat(1, [sp_a, sp_b, sp_c, sp_d])

      # Shape mismatches can only be caught when the op is run
      with self.assertRaisesOpError("Input shapes must match"):
        sess.run(sp_concat)
开发者ID:adeelzaman,项目名称:tensorflow,代码行数:11,代码来源:sparse_concat_op_test.py


示例9: testShapeInferenceUnknownShapes

  def testShapeInferenceUnknownShapes(self):
    with self.test_session(use_gpu=False):
      sp_inputs = [
          self._SparseTensor_UnknownShape(),
          self._SparseTensor_UnknownShape(val_shape=[3]),
          self._SparseTensor_UnknownShape(ind_shape=[1, 3]),
          self._SparseTensor_UnknownShape(shape_shape=[3])]

      sp_concat = tf.sparse_concat(0, sp_inputs)

      self.assertEqual(sp_concat.indices.get_shape().as_list(), [None, 3])
      self.assertEqual(sp_concat.values.get_shape().as_list(), [None])
      self.assertEqual(sp_concat.shape.get_shape(), [3])
开发者ID:adeelzaman,项目名称:tensorflow,代码行数:13,代码来源:sparse_concat_op_test.py


示例10: testConcat1

    def testConcat1(self):
        with self.test_session(use_gpu=False) as sess:
            # concat(A):
            # [    1]
            # [2    ]
            # [3   4]
            for sp_a in (self._SparseTensorValue_3x3(), self._SparseTensor_3x3()):
                sp_concat = tf.sparse_concat(1, [sp_a])

                self.assertEqual(sp_concat.indices.get_shape(), [4, 2])
                self.assertEqual(sp_concat.values.get_shape(), [4])
                self.assertEqual(sp_concat.shape.get_shape(), [2])

                concat_out = sess.run(sp_concat)

                self.assertAllEqual(concat_out.indices, [[0, 2], [1, 0], [2, 0], [2, 2]])
                self.assertAllEqual(concat_out.values, [1, 2, 3, 4])
                self.assertAllEqual(concat_out.shape, [3, 3])
开发者ID:apollos,项目名称:tensorflow,代码行数:18,代码来源:sparse_concat_op_test.py


示例11: testConcatNonNumeric

    def testConcatNonNumeric(self):
        with self.test_session(use_gpu=False) as sess:
            # concat(A, B):
            # [    a          ]
            # [b       e      ]
            # [c   d f     g h]
            sp_a = self._SparseTensor_String3x3()
            sp_b = self._SparseTensor_String3x5()

            sp_concat = tf.sparse_concat(1, [sp_a, sp_b])

            self.assertEqual(sp_concat.indices.get_shape(), [8, 2])
            self.assertEqual(sp_concat.values.get_shape(), [8])
            self.assertEqual(sp_concat.shape.get_shape(), [2])

            concat_out = sess.run(sp_concat)

            self.assertAllEqual(concat_out.indices, [[0, 2], [1, 0], [1, 4], [2, 0], [2, 2], [2, 3], [2, 6], [2, 7]])
            self.assertAllEqual(concat_out.values, [b"a", b"b", b"e", b"c", b"d", b"f", b"g", b"h"])
            self.assertAllEqual(concat_out.shape, [3, 8])
开发者ID:apollos,项目名称:tensorflow,代码行数:20,代码来源:sparse_concat_op_test.py


示例12: testConcat2

    def testConcat2(self):
        with self.test_session(use_gpu=False) as sess:
            # concat(A, B):
            # [    1          ]
            # [2       1      ]
            # [3   4 2     1 0]
            for sp_a in (self._SparseTensorValue_3x3(), self._SparseTensor_3x3()):
                for sp_b in (self._SparseTensorValue_3x5(), self._SparseTensor_3x5()):
                    sp_concat = tf.sparse_concat(1, [sp_a, sp_b])

                    self.assertEqual(sp_concat.indices.get_shape(), [8, 2])
                    self.assertEqual(sp_concat.values.get_shape(), [8])
                    self.assertEqual(sp_concat.shape.get_shape(), [2])

                    concat_out = sess.run(sp_concat)

                    self.assertAllEqual(
                        concat_out.indices, [[0, 2], [1, 0], [1, 4], [2, 0], [2, 2], [2, 3], [2, 6], [2, 7]]
                    )
                    self.assertAllEqual(concat_out.values, [1, 2, 1, 3, 4, 2, 1, 0])
                    self.assertAllEqual(concat_out.shape, [3, 8])
开发者ID:apollos,项目名称:tensorflow,代码行数:21,代码来源:sparse_concat_op_test.py


示例13: testConcat1

  def testConcat1(self):
    with self.test_session(use_gpu=False) as sess:
      # concat(A):
      # [    1]
      # [2    ]
      # [3   4]
      for sp_a in (self._SparseTensorValue_3x3(), self._SparseTensor_3x3()):
        # Note that we ignore concat_dim in this case since we short-circuit the
        # single-input case in python.
        for concat_dim in (-2000, 1, 2000):
          sp_concat = tf.sparse_concat(concat_dim, [sp_a])

          self.assertEqual(sp_concat.indices.get_shape(), [4, 2])
          self.assertEqual(sp_concat.values.get_shape(), [4])
          self.assertEqual(sp_concat.dense_shape.get_shape(), [2])

          concat_out = sess.run(sp_concat)

          self.assertAllEqual(concat_out.indices,
                              [[0, 2], [1, 0], [2, 0], [2, 2]])
          self.assertAllEqual(concat_out.values, [1, 2, 3, 4])
          self.assertAllEqual(concat_out.shape, [3, 3])
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:22,代码来源:sparse_concat_op_test.py


示例14: testConcat3

    def testConcat3(self):
        with self.test_session(use_gpu=False) as sess:
            # concat(A, B, C):
            # [    1              ]
            # [2       1       1  ]
            # [3   4 2     1 0 2  ]
            sp_a = self._SparseTensor_3x3()
            sp_b = self._SparseTensor_3x5()
            sp_c = self._SparseTensor_3x2()

            sp_concat = tf.sparse_concat(1, [sp_a, sp_b, sp_c])

            self.assertEqual(sp_concat.indices.get_shape(), [10, 2])
            self.assertEqual(sp_concat.values.get_shape(), [10])
            self.assertEqual(sp_concat.shape.get_shape(), [2])

            concat_out = sess.run(sp_concat)

            self.assertAllEqual(
                concat_out.indices, [[0, 2], [1, 0], [1, 4], [1, 8], [2, 0], [2, 2], [2, 3], [2, 6], [2, 7], [2, 8]]
            )
            self.assertAllEqual(concat_out.values, [1, 2, 1, 1, 3, 4, 2, 1, 0, 2])
            self.assertAllEqual(concat_out.shape, [3, 10])
开发者ID:apollos,项目名称:tensorflow,代码行数:23,代码来源:sparse_concat_op_test.py


示例15: _dnn_softmax_fn

  def _dnn_softmax_fn(features, targets, mode):
    """Creates the prediction, loss, and train ops.

    Args:
      features: A dictionary of tensors keyed by the feature name.
      targets: A tensor representing the labels (in this case,
        the ratings on the target movie).
      mode: The execution mode, as defined in tf.contrib.learn.ModeKeys.

    Returns:
      ModelFnOps with the mode, prediction, loss, train_op and
      output_alternatives a dictionary specifying the output for a
      classification request during serving.
    Raises:
      ValueError: When the wrong evaluation type is specified.
    """
    _ = targets  # Unused variable.
    class_weights = tf.get_variable(
        name='class_weights',
        shape=[MOVIE_VOCAB_SIZE, hparams.query_hidden_dims[-1]],
        initializer=tf.contrib.layers.xavier_initializer())
    class_biases = tf.get_variable(
        name='class_biases',
        shape=[MOVIE_VOCAB_SIZE],
        initializer=tf.zeros_initializer())
    query_embeddings = _embed_query_features(features, mode=mode)
    tf.summary.scalar('query_embeddings_zero_fraction',
                      tf.nn.zero_fraction(query_embeddings))

    # Create layers for target features.
    if mode != tf.contrib.learn.ModeKeys.INFER:
      logits_layer = tf.matmul(
          query_embeddings, tf.transpose(class_weights)) + class_biases
      target_one_hot = tf.one_hot(
          indices=features[CANDIDATE_MOVIE_ID].values,
          depth=MOVIE_VOCAB_SIZE,
          on_value=1.0)
      loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
          labels=target_one_hot, logits=logits_layer))

      if mode == tf.contrib.learn.ModeKeys.TRAIN:
        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=hparams.learning_rate,
            optimizer=hparams.optimizer)
        return tf.contrib.learn.ModelFnOps(
            mode=mode, loss=loss, train_op=train_op)
      elif mode == tf.contrib.learn.ModeKeys.EVAL:
        if hparams.eval_type == REGRESSION:
          raise ValueError('eval_type must be RANKING for DNN softmax model.')
        elif hparams.eval_type == RANKING:
          predictions = tf.matmul(
              query_embeddings, tf.transpose(class_weights)) + class_biases
          if hparams.use_ranking_candidate_movie_ids:
            # Get ranking candidate movie ids to rank our candidate movie
            # against.
            ranking_candidate_movie_ids = features[RANKING_CANDIDATE_MOVIE_IDS]
            movies_to_rank_condition = tf.sparse_to_indicator(
                tf.sparse_concat(
                    axis=1,
                    sp_inputs=[ranking_candidate_movie_ids,
                               features[CANDIDATE_MOVIE_ID]]),
                MOVIE_VOCAB_SIZE)
            predictions = tf.where(movies_to_rank_condition, predictions,
                                   tf.fill(
                                       tf.shape(predictions),
                                       tf.reduce_min(predictions)))
          return tf.contrib.learn.ModelFnOps(
              mode=mode, predictions=predictions, loss=loss)
    elif mode == tf.contrib.learn.ModeKeys.INFER:
      scores = tf.matmul(
          query_embeddings, tf.transpose(class_weights)) + class_biases

      rated_movie_ids = features[QUERY_RATED_MOVIE_IDS]
      pruned_scores = tf.where(
          tf.sparse_to_indicator(rated_movie_ids, MOVIE_VOCAB_SIZE),
          tf.fill(tf.shape(scores), tf.reduce_min(scores)), scores)
      predictions, output_alternatives = generate_top_k_scores_and_ids(
          pruned_scores, hparams.top_k_infer)
      return tf.contrib.learn.ModelFnOps(
          mode=mode,
          predictions=predictions,
          output_alternatives=output_alternatives)
开发者ID:cottrell,项目名称:notebooks,代码行数:84,代码来源:task.py


示例16: _matrix_factorization_model_fn

  def _matrix_factorization_model_fn(features, target_ratings, mode):
    """Creates a neighborhood factorization model.

    Each user is represented by a combination of embeddings of rated items,
    as described in the paper: "Factorization Meets the Neighborhood:
    a Multifaceted Collaborative Filtering Model - Yehuda Koren (KDD 2013)".

    Args:
      features: A dictionary of tensors keyed by the feature name.
      target_ratings: A tensor representing the labels (in this case,
        the ratings on the target movie).
      mode: The execution mode, as defined in tf.contrib.learn.ModeKeys.

    Returns:
      ModelFnOps with the mode, prediction, loss, train_op and
      output_alternatives a dictionary specifying the output for a
      classification request during serving.
    """
    _ = target_ratings  # Unused on this model.
    if hparams.embedding_weight_initializer == TRUNCATED_NORMAL:
      embedding_weight_initializer = tf.truncated_normal_initializer(stddev=0.1)
    else:
      embedding_weight_initializer = None
    query_movie_embedding_weights = tf.get_variable(
        'query_movie_ids_embedding_weights',
        [MOVIE_VOCAB_SIZE, hparams.movie_embedding_dim],
        initializer=embedding_weight_initializer,
        regularizer=tf.contrib.layers.l2_regularizer(hparams.l2_weight_decay))
    query_movie_ids = features[QUERY_RATED_MOVIE_IDS]
    query_embeddings = tf.nn.embedding_lookup_sparse(
        [query_movie_embedding_weights],
        query_movie_ids,
        None,
        combiner='sqrtn',
        name='query_embedding')
    query_biases, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
        columns_to_tensors=features,
        feature_columns=make_query_feature_columns(),
        num_outputs=1)
    global_rating_bias = tf.get_variable(
        name='global_rating_bias',
        initializer=tf.constant(RATING_BIAS, dtype=tf.float32))
    candidate_movie_embedding_weights = tf.get_variable(
        'candidate_movie_id_embedding_weights',
        [MOVIE_VOCAB_SIZE, hparams.movie_embedding_dim],
        initializer=embedding_weight_initializer,
        regularizer=tf.contrib.layers.l2_regularizer(hparams.l2_weight_decay))
    candidate_biases, _, _ = (
        tf.contrib.layers.weighted_sum_from_feature_columns(
            columns_to_tensors=features,
            feature_columns=make_candidate_feature_columns(),
            num_outputs=1))

    # Create layers for target features.
    if mode != tf.contrib.learn.ModeKeys.INFER:
      candidate_movie_ids = features[CANDIDATE_MOVIE_ID]
      candidate_embeddings = tf.nn.embedding_lookup_sparse(
          [candidate_movie_embedding_weights],
          candidate_movie_ids,
          None,
          name='candidate_embedding')
      predictions = tf.reduce_sum(tf.multiply(
          query_embeddings, candidate_embeddings), 1, keep_dims=True)
      if hparams.enable_bias:
        biases = tf.add(query_biases, candidate_biases)
        predictions = tf.add(predictions, biases)
        predictions = tf.add(predictions, global_rating_bias)

      labels = features[LABEL_RATING_SCORE]
      loss = tf.losses.mean_squared_error(labels, predictions)

      if mode == tf.contrib.learn.ModeKeys.TRAIN:
        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=hparams.learning_rate,
            optimizer=hparams.optimizer)
        return tf.contrib.learn.ModelFnOps(
            mode=mode, predictions=predictions, loss=loss, train_op=train_op)
      elif mode == tf.contrib.learn.ModeKeys.EVAL:
        if hparams.eval_type == REGRESSION:
          return tf.contrib.learn.ModelFnOps(
              mode=mode, predictions=predictions, loss=loss)
        elif hparams.eval_type == RANKING:
          # For 'RANKING' eval, we are interested in [email protected], [email protected]
          # metrics which require us to compute prediction/ranking scores for
          # all movies.
          predictions = tf.matmul(query_embeddings,
                                  candidate_movie_embedding_weights,
                                  transpose_b=True)
          if hparams.enable_bias:
            biases = tf.add(query_biases, candidate_biases)
            predictions = tf.add(predictions, biases)

          if hparams.use_ranking_candidate_movie_ids:
            # Get ranking candidate movie ids to rank our candidate movie
            # against.
            ranking_candidate_movie_ids = features[RANKING_CANDIDATE_MOVIE_IDS]
            movies_to_rank_condition = tf.sparse_to_indicator(
                tf.sparse_concat(
#.........这里部分代码省略.........
开发者ID:cottrell,项目名称:notebooks,代码行数:101,代码来源:task.py



注:本文中的tensorflow.sparse_concat函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python tensorflow.sparse_placeholder函数代码示例发布时间:2022-05-27
下一篇:
Python tensorflow.space_to_depth函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap