3 篇文章

# 低阶API示范

TensorFlow有5个不同的层次结构：即硬件层内核层低阶API中阶API高阶API。本章我们将以线性回归为例，直观对比展示在低阶API，中阶API，高阶API这三个层级实现模型的特点。

TensorFlow的层次结构从低到高可以分成如下五层。

``````import tensorflow as tf

#打印时间分割线
@tf.function
def printbar():
ts = tf.timestamp()
today_ts = ts%(24*60*60)

hour = tf.cast(today_ts//3600+8,tf.int32)%tf.constant(24)
minite = tf.cast((today_ts%3600)//60,tf.int32)
second = tf.cast(tf.floor(today_ts%60),tf.int32)

def timeformat(m):
if tf.strings.length(tf.strings.format("{}",m))==1:
return(tf.strings.format("0{}",m))
else:
return(tf.strings.format("{}",m))

timestring = tf.strings.join([timeformat(hour),timeformat(minite),
timeformat(second)],separator = ":")
tf.print("=========="*8,end = "")
tf.print(timestring)
``````
``````#样本数量
n = 400

# 生成测试用数据集
X = tf.random.uniform([n,2],minval=-10,maxval=10)
w0 = tf.constant([[2.0],[-1.0]])
b0 = tf.constant(3.0)
Y = X@w0 + b0 + tf.random.normal([n,1],mean = 0.0,stddev= 2.0)  # @表示矩阵乘法,增加正态扰动
``````
``````#使用动态图调试

w = tf.Variable(tf.random.normal(w0.shape))
b = tf.Variable(0.0)

def train(epoches):
for epoch in tf.range(1,epoches+1):
#正向传播求损失
Y_hat = X@w + b
loss = tf.squeeze(tf.transpose(Y-Y_hat)@(Y-Y_hat))/(2.0*n)

# 反向传播求梯度
# 梯度下降法更新参数
w.assign(w - 0.001*dloss_dw)
b.assign(b - 0.001*dloss_db)
if epoch%1000 == 0:
printbar()
tf.print("epoch =",epoch," loss =",loss,)
tf.print("w =",w)
tf.print("b =",b)
tf.print("")

train(5000)
``````
``````##使用autograph机制转换成静态图加速

w = tf.Variable(tf.random.normal(w0.shape))
b = tf.Variable(0.0)

@tf.function
def train(epoches):
for epoch in tf.range(1,epoches+1):
#正向传播求损失
Y_hat = X@w + b
loss = tf.squeeze(tf.transpose(Y-Y_hat)@(Y-Y_hat))/(2.0*n)

# 反向传播求梯度
# 梯度下降法更新参数
w.assign(w - 0.001*dloss_dw)
b.assign(b - 0.001*dloss_db)
if epoch%1000 == 0:
printbar()
tf.print("epoch =",epoch," loss =",loss,)
tf.print("w =",w)
tf.print("b =",b)
tf.print("")
train(5000)
``````