2.预备知识
2.1 数据操作
import tensorflow as tf
x = tf.range(12)
x
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])>
x.shape
TensorShape([12])
tf.size(x)
<tf.Tensor: shape=(), dtype=int32, numpy=12>
X = tf.reshape(x,(3,-1))
X
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])>
tf.zeros((2,3,4))
<tf.Tensor: shape=(2, 3, 4), dtype=float32, numpy=
array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]], dtype=float32)>
tf.ones((2,3,4))
<tf.Tensor: shape=(2, 3, 4), dtype=float32, numpy=
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]], dtype=float32)>
x = tf.range(12)
x
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])>
tf.random.normal(shape=[3,4])
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0.20366655, 0.76086736, 0.11236531, 0.7315972 ],
[-0.2376917 , 2.199889 , 0.08802697, -1.2448792 ],
[-0.42119586, 1.4910423 , -0.88865596, -0.8695782 ]],
dtype=float32)>
tf.constant([[1,2,4,3],[1,2,3,4],[4,3,2,1]])
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[1, 2, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])>
x = tf.constant([1.0, 2, 4,8])
y = tf.constant([2.0,2,2,2])
x + y,x-y,x*y,x/y,x**y
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 3., 4., 6., 10.], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([-1., 0., 2., 6.], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 2., 4., 8., 16.], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.5, 1. , 2. , 4. ], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 1., 4., 16., 64.], dtype=float32)>)
tf.exp(x)
<tf.Tensor: shape=(4,), dtype=float32, numpy=
array([2.7182817e+00, 7.3890562e+00, 5.4598152e+01, 2.9809580e+03],
dtype=float32)>
X = tf.reshape(tf.range(12,dtype=tf.float32),(3,4))
Y = tf.constant([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]])
tf.concat([X,Y],axis=0),tf.concat([X,Y],axis=1)
(<tf.Tensor: shape=(6, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 2., 1., 4., 3.],
[ 1., 2., 3., 4.],
[ 4., 3., 2., 1.]], dtype=float32)>,
<tf.Tensor: shape=(3, 8), dtype=float32, numpy=
array([[ 0., 1., 2., 3., 2., 1., 4., 3.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 4., 3., 2., 1.]], dtype=float32)>)
tf.concat 张量连接
X ==Y
<tf.Tensor: shape=(3, 4), dtype=bool, numpy=
array([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])>
tf.reduce_sum(X)
<tf.Tensor: shape=(), dtype=float32, numpy=66.0>
a = tf.reshape(tf.range(3),(3,1))
b = tf.reshape(tf.range(2),(1,2))
a,b
(<tf.Tensor: shape=(3, 1), dtype=int32, numpy=
array([[0],
[1],
[2]])>,
<tf.Tensor: shape=(1, 2), dtype=int32, numpy=array([[0, 1]])>)
a+b
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[0, 1],
[1, 2],
[2, 3]])>
矩阵a将复制列,矩阵b将复制行,然后再按元素相加。
X[-1],X[1:3]
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 8., 9., 10., 11.], dtype=float32)>,
<tf.Tensor: shape=(2, 4), dtype=float32, numpy=
array([[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]], dtype=float32)>)
X_var = tf.Variable(X)
#X_var[1,2].assign(9)
X_var
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]], dtype=float32)>
X_var = tf.Variable(X)
X_var[0:2,:].assign(tf.ones(X_var[0:2,:].shape)*12)
X_var
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[ 8., 9., 10., 11.]], dtype=float32)>
before = id(Y)
Y=Y + X
id(Y) == before
False
Z = tf.Variable(tf.zeros_like(Y))
print('id(Z):',id(Z))
Z.assign(X+Y)
print('id(Z)',id(Z))
id(Z): 2370919107408
id(Z) 2370919107408
@tf.function
def computation(X,Y):
Z = tf.zeros_like(Y)
A = X+Y
B = A + Y
C = B + Y
return C + Y
computation(X,Y)
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 8., 17., 42., 51.],
[ 56., 73., 90., 107.],
[120., 129., 138., 147.]], dtype=float32)>
X,Y
(<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]], dtype=float32)>,
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 2., 4., 10., 12.],
[13., 17., 21., 25.],
[28., 30., 32., 34.]], dtype=float32)>)
A = X.numpy()
B = tf.constant(A)
type(A),type(B)
(numpy.ndarray, tensorflow.python.framework.ops.EagerTensor)
a = tf.constant([3.5]).numpy()
a,a.item(),float(a),int(a)
(array([3.5], dtype=float32), 3.5, 3.5, 3)
2.2 数据预处理
在Python中常用的数据分析工具中,通常使用pandas软件包。主要学习使用pandas预处理原始数据并将原始数据转换成张量格式的步骤
import os
os.makedirs(os.path.join('..','data'),exist_ok=True)
data_file = os.path.join('..','data',';house_tiny.csv')
with open(data_file,'w') as f:
f.write('NumRooms,Alley,Price\n')#列名 房间数量、巷子类型、房屋价格,NA表示未知
f.write('NA,Pave,127500\n')#每行表示一个数据样本
f.write('2,NA,106000\n')
f.write('4,NA,178100\n')
f.write('NA,NA,140000\n')
import pandas as pd#pandas用于数据分析,各种形式数据
data = pd.read_csv(data_file)#读取数据格式为_cvs的数据
print(data)
NumRooms Alley Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000
NaN代表数据缺失,处理缺失数据一般用插值和删除。插值使用代替值代替缺失值,删除则是直接忽略缺失值
inputs,outputs = data.iloc[:,0:2],data.iloc[:,2]
inputs = inputs.fillna(inputs.mean())
print(inputs)
NumRooms Alley
0 3.0 Pave
1 2.0 NaN
2 4.0 NaN
3 3.0 NaN
.iloc位置索引
inputs = pd.get_dummies(inputs)
print(inputs)
NumRooms Alley_Pave Alley_nan
0 3.0 1 0
1 2.0 0 1
2 4.0 0 1
3 3.0 0 1
pd.get_dummies()#one-hot编码
import tensorflow as tf
X,y = tf.constant(inputs.values),tf.constant(outputs.values)
X,y
(<tf.Tensor: shape=(4, 3), dtype=float64, numpy=
array([[3., 1., 0.],
[2., 0., 1.],
[4., 0., 1.],
[3., 0., 1.]])>,
<tf.Tensor: shape=(4,), dtype=int64, numpy=array([127500, 106000, 178100, 140000], dtype=int64)>)
这部分主要学习了pd.read_csv和pd.get_dummies()
2.3线性代数
标量由只有一个元素的张量表示。
import tensorflow as tf
x = tf.constant([3.0])
y = tf.constant([2.0])
x+y,x-y,x*y,x/y,x**y
(<tf.Tensor: shape=(1,), dtype=float32, numpy=array([5.], dtype=float32)>,
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>,
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([6.], dtype=float32)>,
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.5], dtype=float32)>,
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([9.], dtype=float32)>)
向量由标量组成的列表。标量值成为向量的元素(element)或者分量(componnent)
x = tf.range(4)
x
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3])>
列向量是向量的默认方向
x[3]
<tf.Tensor: shape=(), dtype=int32, numpy=3>
向量的长度称为向量的维度,可以用len()函数访问向量长度
len(x)
4
当用张量表示一个向量(只有一个轴)时,我们也可以通过.shape属性访问向量的长度。形状(shape)是一个元组,列出了张量沿每个轴的长度(维数)。对于只有一个轴的张量,形状只有一个元素。
x.shape
TensorShape([4])
矩阵,表示有两个轴的张量。当有轴时,轴的数量就是张量的维数。
A = tf.reshape(tf.range(20),(5,4))
A
<tf.Tensor: shape=(5, 4), dtype=int32, numpy=
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])>
tf.transpose(A)#矩阵的转置,即交换矩阵的行和列
<tf.Tensor: shape=(4, 5), dtype=int32, numpy=
array([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])>
B = tf.constant([[1,2,3],[2,0,4],[3,4,5]])
B
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
[2, 0, 4],
[3, 4, 5]])>
B == tf.transpose(B)
<tf.Tensor: shape=(3, 3), dtype=bool, numpy=
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])>
矩阵的默认方向是列向量,但是在表示数据集的矩阵中,将每个数据样本作为行向量更为常见
张量,任意数量轴的n维数组
X = tf.reshape(tf.range(24),(2,3,4))
X
<tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])>
在图像处理中,图像以n维数组形式出现,三个轴通常对应高度宽度华人通道数。
A = tf.reshape(tf.range(20,dtype=tf.float32),(5,4))#dtype是为了确定数据类型
B = A
A, A+B
(<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]], dtype=float32)>,
<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[16., 18., 20., 22.],
[24., 26., 28., 30.],
[32., 34., 36., 38.]], dtype=float32)>)
A*B#矩阵中元素对应相乘,内积,也称哈达玛积。张量乘以或者加上一个标量并不会改变张量的形状,
#只是对应的元素运算
<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]], dtype=float32)>
a = 2
X = tf.reshape(tf.range(24),(2,3,4))
a+X,(a*X).shape
(<tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=
array([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]])>,
TensorShape([2, 3, 4]))
降维
x = tf.range(4,dtype=tf.float32)
x,tf.reduce_sum(x)#tf.reduce_sum()求和
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 1., 2., 3.], dtype=float32)>,
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>)
A,A.shape,tf.reduce_sum(A)
(<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]], dtype=float32)>,
TensorShape([5, 4]),
<tf.Tensor: shape=(), dtype=float32, numpy=190.0>)
A_sum_axis0 = tf.reduce_sum(A,axis=0)
A_sum_axis0,A_sum_axis0.shape
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([40., 45., 50., 55.], dtype=float32)>,
TensorShape([4]))
A_sum_axis1 = tf.reduce_sum(A,axis=1)
A_sum_axis1,A_sum_axis1.shape
(<tf.Tensor: shape=(5,), dtype=float32, numpy=array([ 6., 22., 38., 54., 70.], dtype=float32)>,
TensorShape([5]))
tf.reduce_sum(A)
<tf.Tensor: shape=(), dtype=float32, numpy=190.0>
tf.reduce_mean(A),tf.reduce_sum(A)/tf.size(A).numpy()
(<tf.Tensor: shape=(), dtype=float32, numpy=9.5>,
<tf.Tensor: shape=(), dtype=float32, numpy=9.5>)
tf.reduce_mean(A,axis=0)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 8., 9., 10., 11.], dtype=float32)>
无论是求和还是求均值,得到的都是列向量。并不是说对每列求和就会得到行向量
非降维求和
调用函数计算总和或均值时保持轴数不变
sum_A = tf.reduce_sum(A,axis=1,keepdims=True)#axis=1是按行相加,keepdims保持轴数不变
sum_A
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[ 6.],
[22.],
[38.],
[54.],
[70.]], dtype=float32)>
A/sum_A
<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[0. , 0.16666667, 0.33333334, 0.5 ],
[0.18181819, 0.22727273, 0.27272728, 0.3181818 ],
[0.21052632, 0.23684211, 0.2631579 , 0.28947368],
[0.22222222, 0.24074075, 0.25925925, 0.2777778 ],
[0.22857143, 0.24285714, 0.25714287, 0.27142859]], dtype=float32)>
A,tf.cumsum(A,axis=0)
(<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]], dtype=float32)>,
<tf.Tensor: shape=(5, 4), dtype=float32, numpy=
array([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]], dtype=float32)>)
点积(Dot Product)
相同位置元素乘积的和
y = tf.ones(4,dtype=tf.float32)
x,y,tf.tensordot(x,y,axes=1)
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 1., 2., 3.], dtype=float32)>,
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>,
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>)
tf.reduce_sum(x*y)
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
矩阵–向量积
线性代数里面学的最常见的矩阵乘法
A.shape,x.shape,tf.linalg.matvec(A,x)
(TensorShape([5, 4]),
TensorShape([4]),
<tf.Tensor: shape=(5,), dtype=float32, numpy=array([ 14., 38., 62., 86., 110.], dtype=float32)>)
B = tf.ones((4,3),tf.float32)
tf.matmul(A,B)#矩阵相乘
<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[ 6., 6., 6.],
[22., 22., 22.],
[38., 38., 38.],
[54., 54., 54.],
[70., 70., 70.]], dtype=float32)>
范数
范数(norm)。非正式地说,一个向量的范数告诉我们一个向量有多大(无关维度,指分量的大小)。线代中,向量范数是将向量映射到标量的函数f。L2范数:欧几里得距离。
u = tf.constant([3.0,-4.0])
tf.norm(u)
<tf.Tensor: shape=(), dtype=float32, numpy=5.0>
L1范数,表示向量元素的绝对值之和。与L2范数相比,L1范数受异常值的影响较小。
tf.reduce_sum(tf.abs(u))
<tf.Tensor: shape=(), dtype=float32, numpy=7.0>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dyWgTS5j-1632574408508)(attachment:image.png)]
tf.norm(tf.ones((4,9)))#所有元素平方和,然后开平方
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
a = tf.ones((4,9))
a
<tf.Tensor: shape=(4, 9), dtype=float32, numpy=
array([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)>
2.4微分
拟合模型的两个关键性问题:优化(optimization)和泛化(generalization).
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
from IPython import display
from d2l import tensorflow as d2l
def f(x):
return 3 * x ** 2 - 4 * x
def numerical_lim(f,x,h):
return (f(x+h)-f(x))/h
h = 0.1
for i in range(5):
print(f'h={h:.5f},numerical limit={numerical_lim(f,1,h):.5f}')
h*=0.1
h=0.10000,numerical limit=2.30000
h=0.01000,numerical limit=2.03000
h=0.00100,numerical limit=2.00300
h=0.00010,numerical limit=2.00030
h=0.00001,numerical limit=2.00003
def use_svg_display(): #@save
"""使用svg格式在Jupyter中显示绘图。svg是矢量图"""
display.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5, 2.5)): #@save
"""设置matplotlib的图表大小。"""
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figsize
#@save
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
"""设置matplotlib的轴。"""
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
#@save
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear',
fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):
"""绘制数据点。"""
if legend is None:
legend = []
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()
# 如果 `X` 有一个轴,输出True
def has_one_axis(X):
return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list)
and not hasattr(X[0], "__len__"))
if has_one_axis(X):
X = [X]
if Y is None:
X, Y = [[]] * len(X), X
elif has_one_axis(Y):
Y = [Y]
if len(X) != len(Y):
X = X * len(Y)
axes.cla()
for x, y, fmt in zip(X, Y, fmts):
if len(x):
axes.plot(x, y, fmt)
else:
axes.plot(y, fmt)
set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
x = np.arange(0, 3, 0.1)
plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])#tangent line切线 legend应该就是角上的那个注释
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fkGjkwqL-1632574408515)(output_100_0.svg)]
import numpy as np
from matplotlib import pyplot as plt
def fun1(x):
return x**3-1/x
def numberical_lim(f,x):
h=1e-4
return (f(x+h)-f(x))/h
def tangent_line(f,x):
"""求切线,调用numberical"""
d=numberical_lim(f,x)
y=f(x)-d*x
return lambda t:d*t+y
x=np.arange(0.0,20.0,0.1)
y=fun1(x)
plt.xlabel('x')
plt.ylabel('f(x)')
tf=tangent_line(fun1,1)
y2=tf(x)
plt.plot(x,y)
plt.plot(x,y2)
plt.show()
<ipython-input-27-83e068b80091>:2: RuntimeWarning: divide by zero encountered in true_divide
return x**3-1/x
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Phr2It4j-1632574408517)(output_105_1.svg)]
2.5自动求导
import tensorflow as tf
x = tf.range(4,dtype=tf.float32)
x
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 1., 2., 3.], dtype=float32)>
在我们计算 y 关于 x 的梯度之前,我们需要一个地方来存储梯度
x = tf.Variable(x)
with tf.GradientTape() as t:
y = 2 * tf.tensordot(x,x,axes=1)#计算y=2x⊤x
y
<tf.Tensor: shape=(), dtype=float32, numpy=28.0>
x_grad = t.gradient(y,x)
x_grad
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 0., 4., 8., 12.], dtype=float32)>
x_grad == 4*x
<tf.Tensor: shape=(4,), dtype=bool, numpy=array([ True, True, True, True])>
with tf.GradientTape() as t:
y = tf.reduce_sum(x)
y
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
t.gradient(y,x)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>
with tf.GradientTape() as t:
y=x*x
t.gradient(y,x)#等价于y=tf.reduce_sum(x*x)
#可以认为这是一个一般求导的方法
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 2., 4., 6.], dtype=float32)>
#设置 persistent=True以多次运行t.gradient多次。(原本运行一次
#就会被删除以不占用空间)
with tf.GradientTape(persistent=True) as t:
y = x*x
u = tf.stop_gradient(y)#产生一个新变量u,与y具有相同的值,但
#但丢弃计算图中如何计算y的任何信息。换句话说,梯度不会向后流
#经u到x。因此,下面的反向传播函数计算z=u*x关于x的偏导数,同时
#将u作为常数处理,而不是z=x*x*x关于x的偏导数。
z = u*x
x_grad = t.gradient(z,x)
x_grad == u
<tf.Tensor: shape=(4,), dtype=bool, numpy=array([ True, True, True, True])>
t.gradient(y,x) == 2*x
<tf.Tensor: shape=(4,), dtype=bool, numpy=array([ True, True, True, True])>
t.gradient(u,x) == 2*x
False
def f(a):
b = a*2
while tf.norm(b)<1000:#tf.norm()求范数
b=b*2
if tf.reduce_sum(b)>0:
c=b
else:
c=100*b
return c
a = tf.Variable(tf.random.normal(shape=()))
with tf.GradientTape() as t:
d = f(a)
d_grad = t.gradient(d, a)
d_grad
<tf.Tensor: shape=(), dtype=float32, numpy=409600.0>
d_grad ==d/a
<tf.Tensor: shape=(), dtype=bool, numpy=True>
2.6 概率
%matplotlib inline
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from d2l import tensorflow as d2l
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-28-d5324e917c9e> in <module>
2 import numpy as np
3 import tensorflow as tf
----> 4 import tensorflow_probability as tfp
5 from d2l import tensorflow as d2l
~\anaconda3\lib\site-packages\tensorflow_probability\__init__.py in <module>
18 # `python/__init__.py` as necessary.
19
---> 20 from tensorflow_probability import substrates
21 # from tensorflow_probability.google import staging # DisableOnExport
22 # from tensorflow_probability.google import tfp_google # DisableOnExport
~\anaconda3\lib\site-packages\tensorflow_probability\substrates\__init__.py in <module>
19 from __future__ import print_function
20
---> 21 from tensorflow_probability.python.internal import all_util
22 from tensorflow_probability.python.internal import lazy_loader # pylint: disable=g-direct-tensorflow-import
23
~\anaconda3\lib\site-packages\tensorflow_probability\python\__init__.py in <module>
140 # Non-lazy load of packages that register with tensorflow or keras.
141 for pkg_name in _maybe_nonlazy_load:
--> 142 dir(globals()[pkg_name]) # Forces loading the package from its lazy loader.
143
144
~\anaconda3\lib\site-packages\tensorflow_probability\python\internal\lazy_loader.py in __dir__(self)
59
60 def __dir__(self):
---> 61 module = self._load()
62 return dir(module)
63
~\anaconda3\lib\site-packages\tensorflow_probability\python\internal\lazy_loader.py in _load(self)
39 """Load the module and insert it into the parent's globals."""
40 if callable(self._on_first_access):
---> 41 self._on_first_access()
42 self._on_first_access = None
43 # Import the target module and insert it into the parent's namespace
~\anaconda3\lib\site-packages\tensorflow_probability\python\__init__.py in _validate_tf_environment(package)
61 if (distutils.version.LooseVersion(tf.__version__) <
62 distutils.version.LooseVersion(required_tensorflow_version)):
---> 63 raise ImportError(
64 'This version of TensorFlow Probability requires TensorFlow '
65 'version >= {required}; Detected an installation of version {present}. '
ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.6; Detected an installation of version 2.3.0. Please upgrade TensorFlow to proceed.
fair_probs = tf.ones(6)/6
tfp.distribution.Multinomial(1,fair_probs).sample()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-29-b6ed5f3e2caa> in <module>
1 fair_probs = tf.ones(6)/6
----> 2 tfp.distribution.Multinomial(1,fair_probs).sample()
NameError: name 'tfp' is not defined
tfp.distribution.Multinomial(10,fair_probs).sample()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-30-6fe67a5bcdde> in <module>
----> 1 tfp.distribution.Multinomial(10,fair_probs).sample()
NameError: name 'tfp' is not defined
count = tfp.distributions.Multinomial(1000,fair_probs).sample()
count / 1000
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-33-433b26767ce2> in <module>
----> 1 count = tfp.distributions.Multinomial(1000,fair_probs).sample()
2 count / 1000
NameError: name 'tfp' is not defined
2.7查阅文档
import tensorflow as tf
print(dir(tf.random))
['Algorithm', 'Generator', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_sys', 'all_candidate_sampler', 'categorical', 'create_rng_state', 'experimental', 'fixed_unigram_candidate_sampler', 'gamma', 'get_global_generator', 'learned_unigram_candidate_sampler', 'log_uniform_candidate_sampler', 'normal', 'poisson', 'set_global_generator', 'set_seed', 'shuffle', 'stateless_binomial', 'stateless_categorical', 'stateless_gamma', 'stateless_normal', 'stateless_parameterized_truncated_normal', 'stateless_poisson', 'stateless_truncated_normal', 'stateless_uniform', 'truncated_normal', 'uniform', 'uniform_candidate_sampler']
help(tf.ones)
Help on function ones in module tensorflow.python.ops.array_ops:
ones(shape, dtype=tf.float32, name=None)
Creates a tensor with all elements set to one (1).
See also `tf.ones_like`, `tf.zeros`, `tf.fill`, `tf.eye`.
This operation returns a tensor of type `dtype` with shape `shape` and
all elements set to one.
>>> tf.ones([3, 4], tf.int32)
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=int32)>
Args:
shape: A `list` of integers, a `tuple` of integers, or
a 1-D `Tensor` of type `int32`.
dtype: Optional DType of an element in the resulting `Tensor`. Default is
`tf.float32`.
name: Optional string. A name for the operation.
Returns:
A `Tensor` with all elements set to one (1).
help(tf.random)
Help on package tensorflow._api.v2.random in tensorflow._api.v2:
NAME
tensorflow._api.v2.random - Public API for tf.random namespace.
PACKAGE CONTENTS
experimental (package)
FILE
c:\users\23886\anaconda3\lib\site-packages\tensorflow\_api\v2\random\__init__.py
tf.ones([3,4,2])
<tf.Tensor: shape=(3, 4, 2), dtype=float32, numpy=
array([[[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]]], dtype=float32)>
import random
print(random.randint(0,8))
2
print(random.random())
print(random.uniform(3,4))
0.32450031554967884
3.2835757453930174