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

Python resource_variable_ops.assign_variable_op函数代码示例

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

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



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

示例1: testAssignAdd

 def testAssignAdd(self):
     with self.test_session():
         handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
         resource_variable_ops.assign_variable_op(handle, constant_op.constant(1, dtype=dtypes.int32)).run()
         resource_variable_ops.assign_add_variable_op(handle, constant_op.constant(1, dtype=dtypes.int32)).run()
         read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
         self.assertEqual(read.eval(), 2)
开发者ID:kdavis-mozilla,项目名称:tensorflow,代码行数:7,代码来源:resource_variable_ops_test.py


示例2: _custom_getter

    def _custom_getter(getter=None, name=None, shape=None, dtype=dtypes.float32,  # pylint: disable=missing-docstring
                       initializer=None, regularizer=None, reuse=None,
                       trainable=True, collections=None, caching_device=None,  # pylint: disable=redefined-outer-name
                       partitioner=None, validate_shape=True,
                       use_resource=None):
      del getter, regularizer, collections, caching_device, partitioner
      del use_resource, validate_shape
      if name in self.tf_variables:
        if reuse:
          return self.tf_variables[name].initialized_value()
        else:
          raise ValueError("Specified reuse=%s but tried to reuse variables."
                           % reuse)
      # TODO(apassos): ensure this is on the same device as above
      v = _CapturedVariable(name, initializer, shape, dtype, trainable)
      self.variables[name] = v

      graph_mode_resource = resource_variable_ops.var_handle_op(
          shared_name=name, shape=shape, dtype=dtype)
      if initializer is None:
        initializer = _default_initializer(name, shape, dtype)
      resource_variable_ops.assign_variable_op(
          graph_mode_resource, initializer(shape, dtype))
      return _VariableFromResource(
          graph_mode_resource, dtype, name, shape=v.shape)
开发者ID:rajeev921,项目名称:tensorflow,代码行数:25,代码来源:graph_callable.py


示例3: assign_fn

 def assign_fn():
   with ops.name_scope("Assign") as n, ops.colocate_with(self._handle):
     resource_variable_ops.assign_variable_op(
         self._handle,
         initial_value,
         name=n)
     # Returning values to keep tf.cond happy.
   return ops.convert_to_tensor(1)
开发者ID:kylin9872,项目名称:tensorflow,代码行数:8,代码来源:def_function.py


示例4: testDtypeSurvivesIdentity

 def testDtypeSurvivesIdentity(self):
   with self.test_session():
     handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
     id_handle = array_ops.identity(handle)
     resource_variable_ops.assign_variable_op(id_handle,
                                              constant_op.constant(
                                                  0,
                                                  dtype=dtypes.int32)).run()
开发者ID:piyushjaiswal98,项目名称:tensorflow,代码行数:8,代码来源:resource_variable_ops_test.py


示例5: testCreateRead

 def testCreateRead(self):
   with self.test_session():
     handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
     resource_variable_ops.assign_variable_op(
         handle, constant_op.constant(1, dtype=dtypes.int32)).run()
     value = resource_variable_ops.read_variable_op(
         handle, dtype=dtypes.int32).eval()
     self.assertAllEqual(1, value)
开发者ID:AutumnQYN,项目名称:tensorflow,代码行数:8,代码来源:resource_variable_ops_test.py


示例6: testScatterAdd

 def testScatterAdd(self):
     with self.test_session():
         handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[1, 1])
         resource_variable_ops.assign_variable_op(handle, constant_op.constant([[1]], dtype=dtypes.int32)).run()
         resource_variable_ops.resource_scatter_add(
             handle, [0], constant_op.constant([[2]], dtype=dtypes.int32)
         ).run()
         read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
         self.assertEqual(read.eval(), [[3]])
开发者ID:kdavis-mozilla,项目名称:tensorflow,代码行数:9,代码来源:resource_variable_ops_test.py


示例7: testReadVariableDtypeMismatchEager

 def testReadVariableDtypeMismatchEager(self):
   with context.eager_mode():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[1], name="foo")
     resource_variable_ops.assign_variable_op(handle, 1)
     with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                  "Trying to read variable with wrong dtype. "
                                  "Expected float got int32."):
       _ = resource_variable_ops.read_variable_op(handle, dtype=dtypes.float32)
开发者ID:aeverall,项目名称:tensorflow,代码行数:9,代码来源:resource_variable_ops_test.py


示例8: testAssignVariableDtypeMismatchEager

 def testAssignVariableDtypeMismatchEager(self):
   with context.eager_mode():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[1], name="foo")
     resource_variable_ops.assign_variable_op(
         handle, constant_op.constant([1]))
     with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                  "Trying to assign variable with wrong "
                                  "dtype. Expected int32 got float."):
       resource_variable_ops.assign_variable_op(
           handle, constant_op.constant([1.], dtype=dtypes.float32))
开发者ID:aeverall,项目名称:tensorflow,代码行数:11,代码来源:resource_variable_ops_test.py


示例9: testManyAssigns

 def testManyAssigns(self):
     with self.test_session() as session:
         handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
         create = resource_variable_ops.assign_variable_op(handle, constant_op.constant(1, dtype=dtypes.int32))
         with ops.control_dependencies([create]):
             first_read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
         with ops.control_dependencies([first_read]):
             write = resource_variable_ops.assign_variable_op(handle, constant_op.constant(2, dtype=dtypes.int32))
         with ops.control_dependencies([write]):
             second_read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
         f, s = session.run([first_read, second_read])
         self.assertEqual(f, 1)
         self.assertEqual(s, 2)
开发者ID:kdavis-mozilla,项目名称:tensorflow,代码行数:13,代码来源:resource_variable_ops_test.py


示例10: testCreateRead

 def testCreateRead(self):
   handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
   self.evaluate(resource_variable_ops.assign_variable_op(
       handle, constant_op.constant(1, dtype=dtypes.int32)))
   value = self.evaluate(
       resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32))
   self.assertAllEqual(1, value)
开发者ID:aeverall,项目名称:tensorflow,代码行数:7,代码来源:resource_variable_ops_test.py


示例11: testAssignAdd

 def testAssignAdd(self):
   handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
   self.evaluate(resource_variable_ops.assign_variable_op(
       handle, constant_op.constant(1, dtype=dtypes.int32)))
   self.evaluate(resource_variable_ops.assign_add_variable_op(
       handle, constant_op.constant(1, dtype=dtypes.int32)))
   read = self.evaluate(
       resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32))
   self.assertEqual(read, 2)
开发者ID:aeverall,项目名称:tensorflow,代码行数:9,代码来源:resource_variable_ops_test.py


示例12: testScatterAdd

 def testScatterAdd(self):
   handle = resource_variable_ops.var_handle_op(
       dtype=dtypes.int32, shape=[1, 1])
   self.evaluate(resource_variable_ops.assign_variable_op(
       handle, constant_op.constant([[1]], dtype=dtypes.int32)))
   self.evaluate(resource_variable_ops.resource_scatter_add(
       handle, [0], constant_op.constant([[2]], dtype=dtypes.int32)))
   read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
   self.assertEqual(self.evaluate(read), [[3]])
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:9,代码来源:resource_variable_ops_test.py


示例13: testScatterUpdateString

 def testScatterUpdateString(self):
   handle = resource_variable_ops.var_handle_op(
       dtype=dtypes.string, shape=[1, 1])
   self.evaluate(resource_variable_ops.assign_variable_op(
       handle, constant_op.constant([["a"]], dtype=dtypes.string)))
   self.evaluate(resource_variable_ops.resource_scatter_update(
       handle, [0], constant_op.constant([["b"]], dtype=dtypes.string)))
   read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.string)
   self.assertEqual(compat.as_bytes(self.evaluate(read)[0][0]),
                    compat.as_bytes("b"))
开发者ID:aeverall,项目名称:tensorflow,代码行数:10,代码来源:resource_variable_ops_test.py


示例14: testScatterDiv

 def testScatterDiv(self):
   with self.test_session() as sess, self.test_scope():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[1, 1])
     sess.run(
         resource_variable_ops.assign_variable_op(
             handle, constant_op.constant([[6]], dtype=dtypes.int32)))
     sess.run(
         resource_variable_ops.resource_scatter_div(
             handle, [0], constant_op.constant([[3]], dtype=dtypes.int32)))
     read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
     self.assertAllEqual(sess.run(read), [[2]])
开发者ID:AnishShah,项目名称:tensorflow,代码行数:12,代码来源:variable_ops_test.py


示例15: testScatterMaxScalar

 def testScatterMaxScalar(self):
   with self.test_session() as sess, self.test_scope():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[1, 1])
     sess.run(
         resource_variable_ops.assign_variable_op(
             handle, constant_op.constant([[6]], dtype=dtypes.int32)))
     sess.run(
         resource_variable_ops.resource_scatter_max(
             handle, [0], constant_op.constant(3, dtype=dtypes.int32)))
     read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
     self.assertEqual(self.evaluate(read), [[6]])
开发者ID:Albert-Z-Guo,项目名称:tensorflow,代码行数:12,代码来源:variable_ops_test.py


示例16: testScatterSub

 def testScatterSub(self):
   with self.test_session() as sess, self.test_scope():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[2, 1])
     sess.run(
         resource_variable_ops.assign_variable_op(
             handle, constant_op.constant([[4], [1]], dtype=dtypes.int32)))
     sess.run(
         resource_variable_ops.resource_scatter_sub(
             handle, [1], constant_op.constant([[2]], dtype=dtypes.int32)))
     read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
     self.assertAllEqual(self.evaluate(read), [[4], [-1]])
开发者ID:Albert-Z-Guo,项目名称:tensorflow,代码行数:12,代码来源:variable_ops_test.py


示例17: testHandleDtypeShapeMatch

 def testHandleDtypeShapeMatch(self):
     with self.test_session():
         handle = resource_variable_ops.var_handle_op(dtype=dtypes.int32, shape=[])
         with self.assertRaises(ValueError):
             resource_variable_ops.assign_variable_op(handle, constant_op.constant(0.0, dtype=dtypes.float32)).run()
         with self.assertRaises(ValueError):
             resource_variable_ops.assign_variable_op(handle, constant_op.constant([0], dtype=dtypes.int32)).run()
         resource_variable_ops.assign_variable_op(handle, constant_op.constant(0, dtype=dtypes.int32)).run()
开发者ID:kdavis-mozilla,项目名称:tensorflow,代码行数:8,代码来源:resource_variable_ops_test.py


示例18: testScatterNdAddOps

 def testScatterNdAddOps(self):
   with self.test_session() as sess, self.test_scope():
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.float32, shape=[8])
     sess.run(
         resource_variable_ops.assign_variable_op(
             handle, constant_op.constant([1] * 8, dtype=dtypes.float32)))
     indices = constant_op.constant([[4], [3], [1], [7]], dtype=dtypes.int32)
     updates = constant_op.constant([9, 10, 11, 12], dtype=dtypes.float32)
     expected = np.array([1, 12, 1, 11, 10, 1, 1, 13])
     sess.run(gen_state_ops.resource_scatter_nd_add(handle, indices, updates))
     read = resource_variable_ops.read_variable_op(
         handle, dtype=dtypes.float32)
     self.assertAllClose(expected, self.evaluate(read))
开发者ID:Albert-Z-Guo,项目名称:tensorflow,代码行数:14,代码来源:variable_ops_test.py


示例19: testScatterMin

 def testScatterMin(self):
   with ops.device("cpu:0"):
     handle = resource_variable_ops.var_handle_op(
         dtype=dtypes.int32, shape=[1, 1])
     self.evaluate(
         resource_variable_ops.assign_variable_op(handle,
                                                  constant_op.constant(
                                                      [[6]],
                                                      dtype=dtypes.int32)))
     self.evaluate(
         resource_variable_ops.resource_scatter_min(handle, [0],
                                                    constant_op.constant(
                                                        [[3]],
                                                        dtype=dtypes.int32)))
     read = resource_variable_ops.read_variable_op(handle, dtype=dtypes.int32)
     self.assertEqual(self.evaluate(read), [[3]])
开发者ID:aeverall,项目名称:tensorflow,代码行数:16,代码来源:resource_variable_ops_test.py


示例20: __init__


#.........这里部分代码省略.........
    init_from_fn = callable(initial_value)

    if constraint is not None and not callable(constraint):
      raise ValueError("The `constraint` argument must be a callable.")

    if isinstance(initial_value, trackable.CheckpointInitialValue):
      self._maybe_initialize_trackable()
      self._update_uid = initial_value.checkpoint_position.restore_uid
      initial_value = initial_value.wrapped_value

    if trainable is None:
      trainable = True
    self._trainable = trainable
    self._save_slice_info = None
    self._initial_value = None
    self._initializer_op = None
    self._is_initialized_op = None
    self._graph_element = None
    self._cached_value = None
    # Store the graph key so optimizers know how to only retrieve variables from
    # this graph. Guaranteed to be the same as the eager graph_key.
    self._graph_key = ops.get_default_graph()._graph_key  # pylint: disable=protected-access
    with ops.name_scope(name, "Variable", []
                        if init_from_fn else [initial_value]) as name:
      # pylint: disable=protected-access
      with ops.init_scope():
        handle_name = ops._name_from_scope_name(name)
        unique_id = "%s_%d" % (handle_name, ops.uid())
        shared_name = context.shared_name(unique_id)
      with ops.name_scope("Initializer"), ops.device(None):
        initial_value = ops.convert_to_tensor(
            initial_value() if init_from_fn else initial_value,
            name="initial_value", dtype=dtype)
      with ops.init_scope():
        self._handle = resource_variable_ops.eager_safe_variable_handle(
            initial_value=initial_value,
            shared_name=shared_name,
            name=name,
            graph_mode=self._in_graph_mode)
      self._shape = initial_value.shape
      self._unique_id = unique_id
      self._handle_name = handle_name + ":0"
      self._dtype = initial_value.dtype.base_dtype
      self._constraint = constraint
      assert initial_value is not None
      if self._in_graph_mode:
        with ops.init_scope():
          outer_graph = ops.get_default_graph()
        func_graph = ops.get_default_graph()
        function_placeholders = (
            func_graph.inputs + func_graph.internal_captures)
        placeholder_ops = set(
            [tensor.op for tensor in function_placeholders])
        lifted_initializer = lift_to_graph.lift_to_graph(
            [initial_value], outer_graph,
            disallowed_placeholders=placeholder_ops)[initial_value]
        with ops.init_scope():
          self._initial_value = lifted_initializer
          with ops.name_scope("IsInitialized"):
            self._is_initialized_op = (
                resource_variable_ops.var_is_initialized_op(self._handle))
          if initial_value is not None:
            with ops.name_scope("Assign") as n, ops.colocate_with(self._handle):
              self._initializer_op = resource_variable_ops.assign_variable_op(
                  self._handle, lifted_initializer, name=n)
          with ops.name_scope("Read"), ops.colocate_with(self._handle):
            # Manually assign reads to the handle's device to avoid log
            # messages.
            with ops.device(self._handle.device):
              value = self._read_variable_op()
            self._graph_element = value
          ops.add_to_collection(ops.GraphKeys.GLOBAL_VARIABLES, self)
      else:
        if add_initializers_to is not None:
          add_initializers_to[self] = initial_value
        def assign_fn():
          with ops.name_scope("Assign") as n, ops.colocate_with(self._handle):
            resource_variable_ops.assign_variable_op(
                self._handle,
                initial_value,
                name=n)
            # Returning values to keep tf.cond happy.
          return ops.convert_to_tensor(1)
        def not_assign_fn():
          return ops.convert_to_tensor(0)
        # Note: this cond is always guaranteed to run because we're inside a
        # defun which will insert automatic control dependencies.
        control_flow_ops.cond(
            resource_variable_ops.var_is_initialized_op(self._handle),
            not_assign_fn, assign_fn)

    # After the handle has been created, set up a way to clean it up when
    # executing eagerly. We'll hold the only reference to the deleter, so that
    # when this object is garbage collected the deleter will be too. This
    # means ResourceVariables can be part of reference cycles without those
    # cycles being uncollectable.
    if not self._in_graph_mode:
      self._handle_deleter = resource_variable_ops.EagerResourceDeleter(
          handle=self._handle, handle_device=self._handle.device)
    self._cached_shape_as_list = None
开发者ID:kylin9872,项目名称:tensorflow,代码行数:101,代码来源:def_function.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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