import tensorflow as tf import numpy as np ############### tf.Variable(initial value,dtype) ############### print('############数字为参数###########') a = tf.Variable(3) print('数字为参数a:',a) print('############列表为参数###########') a = tf.Variable([1,6]) print('列表为参数a:',a) print('############np数组为参数###########') a = tf.Variable(np.array([3,6.0])) print('np数组为参数a:',a) print('############张量为参数###########') a = tf.Variable(tf.constant([[1,1],[2,2],[2,3]])) print('张量为参数a:',a) print('a.trainable:',a.trainable) # 该变量是否可以被训练 print('type(a):',type(a)) print() ############### 对象名.assign() ############### a = tf.Variable([1,2,3]) print('原可训练变量a:',a) a.assign([4,2,3]) # 将可训练变量改变 print('改变后的a:',a) a.assign_add([4,0,5]) # 将变量相加 print('相加后的变量a:',a) a.assign_sub([8,8,8]) # 将变量相减 print('相减后的变量a:',a) print() ############### isinstance() ############### a = tf.constant(5) b = tf.Variable(5) print('a:{} b{}'.format(a,b)) print("isinstance(a,tf.Tensor):{},isinstance(a,tf.Variable):{}".format(isinstance(a,tf.Tensor),isinstance(a,tf.Variable))) print("isinstance(b,tf.Tensor):{},isinstance(b,tf.Variable):{}".format(isinstance(b,tf.Tensor),isinstance(b,tf.Variable)))