tf的张量改变可以使用python的方式
x=tf.ones([6,6])
x=x+3
print(x)
tf.Tensor( [[4. 4. 4. 4. 4. 4.] [4. 4. 4. 4. 4. 4.] [4. 4. 4. 4. 4. 4.] [4. 4. 4. 4. 4. 4.] [4. 4. 4. 4. 4. 4.] [4. 4. 4. 4. 4. 4.]], shape=(6, 6), dtype=float32)
然而机器学习中间需要变化的状态(每个参数朝损失变小的方向改变,所以TensorFlow也要内置有状态的操作,这就是Variables,它可以表示模型中的参数,而且方便高效。
Variables是一个存在值的对象,当其被使用是,它被隐式地被从存储中读取,而当有诸如tf.assign_sub, tf.scatter_update这样的操作时,得到的新值会储存到原对象中。
v=tf.Variable(2)
v.assign(6)
print(v)
v.assign_add(tf.square(3))
print(v)
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=6> <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=15>
构建一个线型函数进行简单预测
class Modeltest(object):
def __init__(self):