TensorFlow2.0教程6:Variables

  创建一个变量

  import tensorflow as tf

  my_var = tf.Variable(tf.ones([2,3]))

  print(my_var)

  try:

  with tf.device("/device:GPU:0"):

  v = tf.Variable(tf.zeros([10, 10]))

  print(v)

  except:

  print('no gpu')

  array([[1., 1., 1.],

  [1., 1., 1.]], dtype=float32)>

  no gpu

  使用变量

  a = tf.Variable(1.0)

  b = (a+2) *3

  print(b)

  tf.Tensor(9.0, shape=(), dtype=float32)

  a = tf.Variable(1.0)

  b = (a.assign_add(2)) *3

  print(b)

  tf.Tensor(9.0, shape=(), dtype=float32)

  变量跟踪无锡人流多少钱 http://www.bhnnk120.com/

  class MyModuleOne(tf.Module):

  def __init__(self):

  self.v0 = tf.Variable(1.0)

  self.vs = [tf.Variable(x) for x in range(10)]

  class MyOtherModule(tf.Module):

  def __init__(self):

  self.m = MyModuleOne()

  self.v = tf.Variable(10.0)

  m = MyOtherModule()

  print(m.variables)

  len(m.variables)

  (, , , , , , , , , , , )

  12

猜你喜欢

转载自www.cnblogs.com/gnz49/p/11424887.html