网站建设的标准,小学生班级优化大师,wordpress站点地址灰显,白城网站开发一个简单记录#xff0c;后续慢慢补充。。。。。
一、函数
# 类似一个tensorflow操作
tf.function
def add(a, b):return ab # 即使传入数字#xff0c;函数运算也是python基本运算#xff0c;发返回值的类型也会变成tensor。print(add(1,2))
# tf.Tensor(3, shape(),…一个简单记录后续慢慢补充。。。。。
一、函数
# 类似一个tensorflow操作
tf.function
def add(a, b):return ab # 即使传入数字函数运算也是python基本运算发返回值的类型也会变成tensor。print(add(1,2))
# tf.Tensor(3, shape(), dtypeint32)print(add(tf.ones([2,2]), tf.ones([2,2]))) # 快速矩阵计算
# tf.Tensor(
# [[2. 2.]
# [2. 2.]], shape(2, 2), dtypefloat32)二、梯度
# 可以计算梯度
v tf.Variable(2.0)
with tf.GradientTape() as tape:res add(v, 1.0)
tape.gradient(res, v)三、for循环
tf.function # 可以使用if, for, while break, continue, return
def f(x): # 传入python标量或列表时会建立一个新的图传入参数大小不同建的图也不同。要避免这种情况就将数字作为参数传入ta tf.TensorArray(dtypetf.int32, size0, dynamic_sizeTrue)for i in range(len(x)): # for循环必须这样写不然只能执行一次ta ta.write(i, x[i] 1)return ta.stack()f(tf.constant([1, 2, 3]))四、追踪
def train_one_step():passtf.function
def train(num_steps):print(追踪 num_steps {}.format(num_steps))for _ in tf.range(num_steps):train_one_step()# Python参数发生变化那么必须回溯图。
train(num_steps10)
train(num_steps20)
# 追踪 num_steps 10
# 追踪 num_steps 20# 使用tensor同类型不会重复追踪
train(num_stepstf.constant(10))
train(num_stepstf.constant(20))
# 追踪 num_steps Tensor(num_steps:0, shape(), dtypeint32)# 使用tensor类型不同才会有新的追踪前一个单元格已追踪int型所以该处不追踪
train(num_stepstf.constant(10, dtypetf.int32))
train(num_stepstf.constant(20.6))
# 追踪 num_steps Tensor(num_steps:0, shape(), dtypefloat32)五、调试
tf.function
def f(x):print(追踪, x) # 使用Python副作用来调试跟踪。tf.print(执行, x) # tf.Variable.assigntf.print和tf.summary是确保TensorFlow运行时在每次调用时跟踪和执行代码的最佳方法。f(1)
f(1)
f(2)# 追踪 1
# 执行 1
# 执行 1
# 执行 2
# 追踪 2