当前位置: 首页>>代码示例>>Python>>正文


Python constant_op.constant函数代码示例

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


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

示例1: testNCELoss

    def testNCELoss(self):
        # A simple test to verify the numerics.

        def _SigmoidCrossEntropyWithLogits(logits, targets):
            # logits, targets: float arrays of the same shape.
            assert logits.shape == targets.shape
            pred = 1.0 / (1.0 + np.exp(-logits))
            eps = 0.0001
            pred = np.minimum(np.maximum(pred, eps), 1 - eps)
            return -targets * np.log(pred) - (1.0 - targets) * np.log(1.0 - pred)

        weights, biases, hidden_acts, sharded_weights = self._GenerateTestInputs()
        labels = [0, 1, 2]
        true_w, true_b = weights[labels], biases[labels]
        sampled = [1, 0, 2, 3]
        num_sampled = len(sampled)
        true_exp = np.empty([self._batch_size, 1], dtype=np.float32)
        true_exp.fill(0.5)
        sampled_exp = np.empty([num_sampled], dtype=np.float32)
        sampled_exp.fill(0.5)
        sampled_w, sampled_b = weights[sampled], biases[sampled]
        test_sampled_vals = (sampled, true_exp, sampled_exp)

        with self.test_session():
            logits_np, labels_np = self._ComputeSampledLogitsNP(
                true_w, true_b, sampled_w, sampled_b, hidden_acts, true_expected=true_exp, sampled_expected=sampled_exp
            )
            nce_loss_np = np.sum(_SigmoidCrossEntropyWithLogits(logits_np, labels_np), 1)

            labels_tf = constant_op.constant(labels, shape=(self._batch_size, 1))
            weights_tf = constant_op.constant(weights)
            biases_tf = constant_op.constant(biases)
            inputs_tf = constant_op.constant(hidden_acts)

            nce_loss_tf = nn.nce_loss(
                weights_tf,
                biases_tf,
                inputs_tf,
                labels_tf,
                num_sampled=1,
                num_classes=self._num_classes,
                num_true=1,
                sampled_values=test_sampled_vals,
            )

            self.assertAllClose(nce_loss_np, nce_loss_tf.eval(), 1e-4)

            # Test with sharded weights
            nce_loss_tf = nn.nce_loss(
                [constant_op.constant(shard) for shard in sharded_weights],
                biases_tf,
                inputs_tf,
                labels_tf,
                num_sampled=1,
                num_classes=self._num_classes,
                num_true=1,
                sampled_values=test_sampled_vals,
            )

            self.assertAllClose(nce_loss_np, nce_loss_tf.eval(), 1e-4)
开发者ID:adam-erickson,项目名称:tensorflow,代码行数:60,代码来源:nn_test.py

示例2: testRandomization

  def testRandomization(self):
    # Run 1x1 crop num_samples times in an image and ensure that one finds each
    # pixel 1/num_pixels of the time.
    num_samples = 1000
    height = 5
    width = 4

    num_pixels = height * width
    data = np.arange(num_pixels).reshape([height, width, 1])
    x_np = np.array(data).astype(np.int32)

    target_shape_np = np.array([1, 1], dtype=np.int64)

    y = []
    with self.test_session():
      x = constant_op.constant(x_np, shape=x_np.shape)
      target_shape = constant_op.constant(target_shape_np, shape=[2])
      y_tf = image_ops.random_crop(x, target_shape)
      for _ in xrange(num_samples):
        y_np = y_tf.eval()
        self.assertAllEqual(y_np.shape, [1, 1, 1])
        y.extend(y_np.flatten())

    # Calculate the mean and 4 * standard deviation.
    mean = [num_samples / num_pixels] * num_pixels
    four_stddev = 4.0 * np.sqrt(mean)

    # Ensure that each entry is observed in 1/num_pixels of the samples
    # within 4 standard deviations.
    counts = np.bincount(y)
    self.assertAllClose(counts, mean, atol=four_stddev)
开发者ID:hbali-sara,项目名称:tensorflow,代码行数:31,代码来源:image_ops_test.py

示例3: ones

def ones(shape, dtype=dtypes.float32, name=None):
  """Creates a tensor with all elements set to 1.

  This operation returns a tensor of type `dtype` with shape `shape` and all
  elements set to 1.

  For example:

  ```python
  tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
  ```

  Args:
    shape: Either a list of integers, or a 1-D `Tensor` of type `int32`.
    dtype: The type of an element in the resulting `Tensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` with all elements set to 1.
  """
  with ops.op_scope([shape], name, "ones") as name:
    if isinstance(shape, list):
      output = constant(1, shape=shape, dtype=dtype, name=name)
    else:
      shape = ops.convert_to_tensor(shape, name="shape")
      output = fill(shape, constant(1, dtype=dtype), name=name)
  assert output.dtype.base_dtype == dtypes.as_dtype(dtype).base_dtype
  return output
开发者ID:DapengLan,项目名称:tensorflow,代码行数:28,代码来源:array_ops.py

示例4: testFetchIndexedSlicesWithoutDenseShape

 def testFetchIndexedSlicesWithoutDenseShape(self):
   with session.Session() as s:
     indices = np.array([[3, 2, 0], [4, 5, 1]]).astype(np.int64)
     values = np.array([1.0, 2.0]).astype(np.float32)
     dense_shape = None
     ind = ops.IndexedSlices(
         constant_op.constant(values), constant_op.constant(indices), None)
     # Single fetch, use as tuple
     ind_out = s.run(ind)
     values_out, indices_out, dense_shape_out = ind_out
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(dense_shape_out, dense_shape)
     # Single fetch, use as IndexedSlicesValue
     ind_out = s.run(ind)
     self.assertAllEqual(ind_out.values, values)
     self.assertAllEqual(ind_out.indices, indices)
     self.assertAllEqual(ind_out.dense_shape, dense_shape)
     # Tuple fetch, use as tuple
     values_out, indices_out, dense_shape_out = s.run(ind)
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(dense_shape_out, dense_shape)
     # List fetch, use as tuple
     (values_out, indices_out, dense_shape_out), = s.run([ind])
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(dense_shape_out, dense_shape)
     # List fetch, use as IndexedSlicesValue
     ind_out, = s.run([ind])
     self.assertAllEqual(ind_out.values, values)
     self.assertAllEqual(ind_out.indices, indices)
     self.assertAllEqual(ind_out.dense_shape, dense_shape)
开发者ID:agouwin,项目名称:udacity_deep_learning_homework,代码行数:33,代码来源:session_test.py

示例5: testDeConv2DSame

  def testDeConv2DSame(self):
    with self.test_session():
      strides = [1, 2, 2, 1]

      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 12, 8, 2]

      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]

      x = constant_op.constant(1.0, shape=x_shape, name="x",
                               dtype=types.float32)
      f = constant_op.constant(1.0, shape=f_shape, name="filter",
                               dtype=types.float32)
      output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
      value = output.eval()

      for n in xrange(x_shape[0]):
        for k in xrange(f_shape[2]):
          for w in xrange(y_shape[2]):
            for h in xrange(y_shape[1]):
              target = 3.0
              # We add a case for locations divisible by the stride.
              h_in = h % strides[1] == 0 and h > 0 and h < y_shape[1] - 1
              w_in = w % strides[2] == 0 and w > 0 and w < y_shape[2] - 1
              if h_in and w_in:
                target += 9.0
              elif h_in or w_in:
                target += 3.0
              self.assertAllClose(target, value[n, h, w, k])
开发者ID:nickicindy,项目名称:tensorflow,代码行数:31,代码来源:nn_test.py

示例6: testFetchSparseTensor

 def testFetchSparseTensor(self):
   with session.Session() as s:
     indices = np.array([[3, 2, 0], [4, 5, 1]]).astype(np.int64)
     values = np.array([1.0, 2.0]).astype(np.float32)
     shape = np.array([7, 9, 2]).astype(np.int64)
     sp = ops.SparseTensor(
         constant_op.constant(indices),
         constant_op.constant(values),
         constant_op.constant(shape))
     # Single fetch, use as tuple
     sp_out = s.run(sp)
     indices_out, values_out, shape_out = sp_out
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(shape_out, shape)
     # Single fetch, use as SparseTensorValue
     sp_out = s.run(sp)
     self.assertAllEqual(sp_out.indices, indices)
     self.assertAllEqual(sp_out.values, values)
     self.assertAllEqual(sp_out.shape, shape)
     # Tuple fetch, use as tuple
     indices_out, values_out, shape_out = s.run(sp)
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(shape_out, shape)
     # List fetch, use as tuple
     (indices_out, values_out, shape_out), = s.run([sp])
     self.assertAllEqual(indices_out, indices)
     self.assertAllEqual(values_out, values)
     self.assertAllEqual(shape_out, shape)
     # List fetch, use as SparseTensorValue
     sp_out, = s.run([sp])
     self.assertAllEqual(sp_out.indices, indices)
     self.assertAllEqual(sp_out.values, values)
     self.assertAllEqual(sp_out.shape, shape)
开发者ID:agouwin,项目名称:udacity_deep_learning_homework,代码行数:35,代码来源:session_test.py

示例7: testNoOp

  def testNoOp(self):
    img_shape = [1, 6, 4, 1]
    single_shape = [6, 4, 1]
    # This test is also conducted with int8, so 127 is the maximum
    # value that can be used.
    data = [127, 127, 64, 64,
            127, 127, 64, 64,
            64, 64, 127, 127,
            64, 64, 127, 127,
            50, 50, 100, 100,
            50, 50, 100, 100]
    target_height = 6
    target_width = 4

    for nptype in self.TYPES:
      img_np = np.array(data, dtype=nptype).reshape(img_shape)

      for opt in self.OPTIONS:
        with self.test_session() as sess:
          image = constant_op.constant(img_np, shape=img_shape)
          y = image_ops.resize_images(image, target_height, target_width, opt)
          yshape = array_ops.shape(y)
          resized, newshape = sess.run([y, yshape])
          self.assertAllEqual(img_shape, newshape)
          self.assertAllClose(resized, img_np, atol=1e-5)

      # Resizing with a single image must leave the shape unchanged also.
      with self.test_session():
        img_single = img_np.reshape(single_shape)
        image = constant_op.constant(img_single, shape=single_shape)
        y = image_ops.resize_images(image, target_height, target_width,
                                    self.OPTIONS[0])
        yshape = array_ops.shape(y)
        newshape = yshape.eval()
        self.assertAllEqual(single_shape, newshape)
开发者ID:kevings14,项目名称:tensorflow,代码行数:35,代码来源:image_ops_test.py

示例8: testDeConv2DSingleStride

    def testDeConv2DSingleStride(self):
        with self.test_session():
            strides = [1, 1, 1, 1]

            # Input, output: [batch, height, width, depth]
            x_shape = [2, 6, 4, 3]
            y_shape = [2, 6, 4, 2]

            # Filter: [kernel_height, kernel_width, output_depth, input_depth]
            f_shape = [3, 3, 2, 3]

            x = constant_op.constant(1.0, shape=x_shape, name="x", dtype=dtypes.float32)
            f = constant_op.constant(1.0, shape=f_shape, name="filter", dtype=dtypes.float32)
            output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
            value = output.eval()

            # We count the number of cells being added at the locations in the output.
            # At the center, #cells=kernel_height * kernel_width
            # At the corners, #cells=ceil(kernel_height/2) * ceil(kernel_width/2)
            # At the borders, #cells=ceil(kernel_height/2)*kernel_width or
            #                        kernel_height * ceil(kernel_width/2)

            for n in xrange(x_shape[0]):
                for k in xrange(f_shape[2]):
                    for w in xrange(y_shape[2]):
                        for h in xrange(y_shape[1]):
                            target = 4 * 3.0
                            h_in = h > 0 and h < y_shape[1] - 1
                            w_in = w > 0 and w < y_shape[2] - 1
                            if h_in and w_in:
                                target += 5 * 3.0
                            elif h_in or w_in:
                                target += 2 * 3.0
                            self.assertAllClose(target, value[n, h, w, k])
开发者ID:adam-erickson,项目名称:tensorflow,代码行数:34,代码来源:nn_test.py

示例9: testFetchByNameDifferentStringTypes

  def testFetchByNameDifferentStringTypes(self):
    with session.Session() as sess:
      c = constant_op.constant(42.0, name='c')
      d = constant_op.constant(43.0, name=u'd')
      e = constant_op.constant(44.0, name=b'e')
      f = constant_op.constant(45.0, name=r'f')

      self.assertTrue(isinstance(c.name, six.text_type))
      self.assertTrue(isinstance(d.name, six.text_type))
      self.assertTrue(isinstance(e.name, six.text_type))
      self.assertTrue(isinstance(f.name, six.text_type))

      self.assertEqual(42.0, sess.run('c:0'))
      self.assertEqual(42.0, sess.run(u'c:0'))
      self.assertEqual(42.0, sess.run(b'c:0'))
      self.assertEqual(42.0, sess.run(r'c:0'))

      self.assertEqual(43.0, sess.run('d:0'))
      self.assertEqual(43.0, sess.run(u'd:0'))
      self.assertEqual(43.0, sess.run(b'd:0'))
      self.assertEqual(43.0, sess.run(r'd:0'))

      self.assertEqual(44.0, sess.run('e:0'))
      self.assertEqual(44.0, sess.run(u'e:0'))
      self.assertEqual(44.0, sess.run(b'e:0'))
      self.assertEqual(44.0, sess.run(r'e:0'))

      self.assertEqual(45.0, sess.run('f:0'))
      self.assertEqual(45.0, sess.run(u'f:0'))
      self.assertEqual(45.0, sess.run(b'f:0'))
      self.assertEqual(45.0, sess.run(r'f:0'))
开发者ID:agouwin,项目名称:udacity_deep_learning_homework,代码行数:31,代码来源:session_test.py

示例10: testColocationIgnoreStack

 def testColocationIgnoreStack(self):
   a = constant_op.constant([2.0], name="a")
   b = constant_op.constant(3.0, name="b")
   with ops.colocate_with(a.op):
     with ops.colocate_with(b.op, ignore_existing=True):
       c = constant_op.constant(4.0)
   self.assertEqual(set([b"loc:@b"]), set(c.op.colocation_groups()))
开发者ID:4chin,项目名称:tensorflow,代码行数:7,代码来源:ops_test.py

示例11: testCircularConvolution

 def testCircularConvolution(self):
     v = constant_op.constant([1,2,3,4,5,6,7], dtype=tf.float32)
     k = constant_op.constant([0,0,1], dtype=tf.float32)
     for use_gpu in [True, False]:
         with self.test_session(use_gpu=use_gpu):
             cir_conv = circular_convolution(v, k).eval()
             self.assertAllEqual(cir_conv, [7,1,2,3,4,5,6])
开发者ID:CosmosShadow,项目名称:NTM,代码行数:7,代码来源:ops_test.py

示例12: testNegation

 def testNegation(self):
   with self.test_session():
     values = constant_op.constant([2, 3, 5, 7], shape=[2, 2])
     indices = constant_op.constant([0, 2])
     x = -ops.IndexedSlices(values, indices)
     self.assertAllEqual(x.values.eval(), [[-2, -3], [-5, -7]])
     self.assertAllEqual(x.indices.eval(), [0, 2])
开发者ID:4chin,项目名称:tensorflow,代码行数:7,代码来源:ops_test.py

示例13: testFetchByNameDifferentStringTypes

    def testFetchByNameDifferentStringTypes(self):
        with session.Session() as sess:
            c = constant_op.constant(42.0, name="c")
            d = constant_op.constant(43.0, name=u"d")
            e = constant_op.constant(44.0, name=b"e")
            f = constant_op.constant(45.0, name=r"f")

            self.assertTrue(isinstance(c.name, six.text_type))
            self.assertTrue(isinstance(d.name, six.text_type))
            self.assertTrue(isinstance(e.name, six.text_type))
            self.assertTrue(isinstance(f.name, six.text_type))

            self.assertEqual(42.0, sess.run("c:0"))
            self.assertEqual(42.0, sess.run(u"c:0"))
            self.assertEqual(42.0, sess.run(b"c:0"))
            self.assertEqual(42.0, sess.run(r"c:0"))

            self.assertEqual(43.0, sess.run("d:0"))
            self.assertEqual(43.0, sess.run(u"d:0"))
            self.assertEqual(43.0, sess.run(b"d:0"))
            self.assertEqual(43.0, sess.run(r"d:0"))

            self.assertEqual(44.0, sess.run("e:0"))
            self.assertEqual(44.0, sess.run(u"e:0"))
            self.assertEqual(44.0, sess.run(b"e:0"))
            self.assertEqual(44.0, sess.run(r"e:0"))

            self.assertEqual(45.0, sess.run("f:0"))
            self.assertEqual(45.0, sess.run(u"f:0"))
            self.assertEqual(45.0, sess.run(b"f:0"))
            self.assertEqual(45.0, sess.run(r"f:0"))
开发者ID:niclar,项目名称:tensorflow,代码行数:31,代码来源:session_test.py

示例14: _testTypesForAdam

  def _testTypesForAdam(self, var, m, v, grad, use_gpu):
    self.setUp()
    with self.test_session(use_gpu=use_gpu):
      var_t = variables.Variable(var)
      m_t = variables.Variable(m)
      v_t = variables.Variable(v)

      t = 1
      beta1 = np.array(0.9, dtype=var.dtype)
      beta2 = np.array(0.999, dtype=var.dtype)
      beta1_power = beta1**t
      beta2_power = beta2**t
      lr = np.array(0.001, dtype=var.dtype)
      epsilon = np.array(1e-8, dtype=var.dtype)
      beta1_t = constant_op.constant(beta1, self._toType(var.dtype), [])
      beta2_t = constant_op.constant(beta2, self._toType(var.dtype), [])
      beta1_power_t = variables.Variable(beta1_power)
      beta2_power_t = variables.Variable(beta2_power)
      lr_t = constant_op.constant(lr, self._toType(var.dtype), [])
      epsilon_t = constant_op.constant(epsilon, self._toType(var.dtype), [])
      variables.initialize_all_variables().run()

      self.assertAllCloseAccordingToType(var, var_t.eval())
      new_var, _, _ = self._adamUpdateNumpy(var, grad, t, m, v,
                                            lr, beta1, beta2, epsilon)
      apply_adam = training_ops.apply_adam(var_t, m_t, v_t, beta1_power_t,
                                           beta2_power_t, lr_t,
                                           beta1_t, beta2_t, epsilon_t, grad)
      out = apply_adam.eval()
      self.assertShapeEqual(out, apply_adam)
      self.assertAllCloseAccordingToType(new_var, out)
开发者ID:0-T-0,项目名称:tensorflow,代码行数:31,代码来源:training_ops_test.py

示例15: testScalarMul

 def testScalarMul(self):
   with self.test_session():
     values = constant_op.constant([2, 3, 5, 7], shape=[2, 2])
     indices = constant_op.constant([0, 2])
     x = math_ops.scalar_mul(-2, ops.IndexedSlices(values, indices))
     self.assertAllEqual(x.values.eval(), [[-4, -6], [-10, -14]])
     self.assertAllEqual(x.indices.eval(), [0, 2])
开发者ID:4chin,项目名称:tensorflow,代码行数:7,代码来源:ops_test.py


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