1. 变量与张量介绍
import tensorflow as tf
tf.__version__
# 张量 Tensor 张量其实就是一个多维数组
# 张量的维度可以是0 : 1,2,3
# dim = 0 : 1,2,3 (一组数)
# dim = 1 : [1,2,3] (向量)
# dim = 2 : [ [2,3],[4,4] ] (二维矩阵)
tf.constant(1.)
#无法进行浮点数转整数
# tf.Variable(1.,dtype=tf.int32)
#但可以整数转浮点数
tf.Variable(1 ,dtype=tf.float32)
tf.constant("hello")
tf.constant(True)
tf.constant([1,2,3,4])
tf.range(10)
a = tf.reshape(tf.range(10) , [2,5])
a.shape
aa = a.cpu()
aa.device
aa = a.gpu()
aa.device
tf.is_tensor(aa)
isinstance(aa,tf.Tensor)
a = tf.zeros([3,5])
a
b = tf.ones_like(a)
b
tf.fill([5,7],3,0)
tf.random.normal([3,5],stddev=2,mean=5)
#这样的生成随机数的值,两边趋近于0,因此容易出现梯度消失的问题
#为了解决以上问题,把某些值截断
tf.random.truncated_normal([3,5])
# 索引和切片与numpy差不多
v = tf.Variable([1,2,3,4])
v[0]
a = tf.Variable(tf.reshape(tf.range(10),[2,5]))
a
a[1,2]
# 行全选,列选取第2列
a[:,2]
2. 张量的计算与转换
tf.add(1,2)
tf.add([1,2],[3,4])
a = tf.Variable(5)
a**2
tf.square(5)
# 一般求均值,求和,求最大值等都会有个降维的操作
tf.reduce_sum([1,2,3])
tf.reduce_sum([[1,2,3],[4,5,6]])
tf.reduce_sum([[1,2,3],[4,5,6]] ,axis = 0)
tf.reduce_sum([[1,2,3],[4,5,6]] ,axis = 1)
a = tf.constant([[1]])
b = tf.constant([[2,3]])
tf.matmul(a,b)
a@b
#转换为numpy
a.numpy()
# 转换为张量
tf.convert_to_tensor(a)
3. 张量的合并与分割
t1 = tf.constant( [[1,2,3],[4,5,6]])
t2 = tf.constant( [[7,8,9],[10,11,12]])
t1,t2
tf.concat([t1,t2],axis = 1)
tf.concat([t1,t2] ,axis=0)
# 假如img1,img2是一个图片,我们想把图片叠加在一起。
img1 = tf.random.uniform([3,4])
plt.matshow(img1)
img2 = tf.random.uniform([3,4])
plt.matshow(img2)
img_stack = tf.stack([img1,img2],axis = 0)
img_stack
img_stack.shape
# 除了要合并的维度可以不同,其他维度值是相同的。
#就比如,图片的数量可以是不相同的,但是每张图片的像素点是相同的
#就是说,stack 所有的张量的形状都是相同的
# unstack
a,b = tf.unstack(img_stack,axis=0)
a,b
#拆分 把数据按维度拆分为几份
# [2,3,4]
# [60000,28,28]
a,b = tf.split(img_stack , axis=2,num_or_size_splits=[1,3])
a,b
4. 张量的数据统计
# 向量的范数
x = tf.ones([2,3])
x
# 1范数
tf.norm(x , ord=1)
# 在某维度上求那一维度的范数
tf.norm(x,ord=1,axis = 0)
# 无穷范数
tf.norm(x,ord=np.inf)
# 求最大值、最小值、均值、和
a = tf.reduce_min(x,axis = 0)
b = tf.reduce_mean(x,axis = 0)
a,b
#比较
a = tf.ones([5],dtype=tf.int32)
b = tf.constant([1,2,1,2,3,])
a,b
z = tf.equal(a,b)
z
# 转变数据类型
z = tf.cast(z,dtype=tf.int32)
tf.reduce_sum(z)
5. 张量的填充与复制
tf.pad(x,[ ]) 第二个参数[ [a1,b1], [a2,b2] ,[a3 ,b3] ] 中填的是某维度(第几个就是第几维度),左边的参数是那个维度上,左边填充多少,如[a1,b1]意思是左边填充a1个单位,右边填充b1个单位
# 填充一般会在收/尾加0,通过填充来满足我们数据形状的要求
# padding
# I like the weather day .
# 1 2 3 4 5 6
# so do I .
# 7 8 9 6
# 我们想把这两个句子放在同一个张量中去,形状就要一样
a = tf.constant([1,2,3,4,5,6])
b = tf.constant([7,8,1,6])
# tf.pad(x,padding)
#padding [ [],[],[] ] # 第一个中括号对就是第几维度的,中括号左边的是在那一维度上,左边填充的0,右边就是右边填充的0
b = tf.pad( b,[ [0,2] ] )
b
c = tf.stack([a,b],axis = 0)
c
# 有些句子比较长,因此我们只需要找到适合大部分句子的最大长度
# 因此有些句子要截取相应的长度。一般取80
total_words = 1e5
max_review_len = 80
embedding_len = 100
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data()
len(x_train[0])
# tf.keras.preprocessing.sequence.pad_sequences 针对数据集进行操作
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train,maxlen=max_review_len,truncating='post',padding='post')
x_train.shape
# 截取,并且补0,并转变为数组
x_train
x = tf.random.truncated_normal([4,28,28])
x.shape
# 把x转变为 [32,32]
x = tf.pad(x,[[0,0],[2,2],[2,2]])
x.shape
# tf.tile() 实现长度为1的在任意维度复制功能
x = tf.random.truncated_normal([4,28,28,3])
x = tf.tile(x,[2,3,3,1])
x.shape
# [4,28,28,3],四张图片,像素点为28*28,有三个通道
x[1,:,:,1].shape
# 数据的限幅 tf.maximunm(x,a) # [a,+……]
x = tf.range(9)
x
#下限
tf.maximum(x,6)
#上限
tf.minimum(x,6)
# 另外 relu本身也是一个有限幅功能的激活函数
def relu(x):
return tf.maximum(x,0.)
# 一起用就是一个范围
tf.minimum(tf.maximum(x,2),7)
#或者使用
tf.clip_by_value(x,2,7)
6. tf.gather()
tf.gather(x, []) ,第二个参数中填的是坐标,最后组成一个数组。
如:班级i ,学生j ,成绩k
tf.gather_nd(x ,[ [1,1,2] ,[2,2,3], [3,3,4] ]) 就是 [1,1,2] ,[2,2,3], [3,3,4] 对应的三个值或数组组成的另一个数组。
# 4个班级 35个人 8个科目
x = tf.random.uniform( [4,35,8],maxval=100,dtype=tf.int32 )
x
x[:2]
tf.gather(x,[0,1], axis=0)
# 所有第0维度的,第1,5,8第1维度的所有第2维度的数据
tf.gather(x,[0,4,7] ,axis=1)
# 所有班的所有同学的,第3,5门成绩
tf.gather(x,[2,4],axis=2)
# 第2,3班的第第[3,4,5,6,27]的成绩
#gather具有语义性
ban = tf.gather(x,[1,2],axis = 0)
xues = tf.gather(ban,[2,3,5,6,27],axis = 1)
xues
# 抽2班的2号,3班的3号,4班的4号
x[1,1],x[2,2],x[3,3]
tf.stack([x[1,1],x[2,2],x[3,3]],axis = 0)
# tf.gather_nd,采样多个样本,后面的是坐标
tf.gather_nd(x,[[1,1],[2,2],[3,3]])
# 班级i ,学生j ,成绩k
tf.gather_nd(x ,[ [1,1,2] ,[2,2,3], [3,3,4] ])
7. tf.boolean_mask()
# 班级的维度上进行采样
mask = [True ,False ,False ,True ] #mask的形状必须与想选取的样本数据的维度相等
tf.boolean_mask(x,mask,axis=0)
8. tf.where(cond,x,y)
cond == True :x 否则y
a = tf.ones([2,2])
b = tf.zeros([2,2])
a,b
cond = tf.constant([[True,False] , [False,True]])
tf.where(cond , a,b)
# 不指定x,y 只返回为真的索引
tf.where(cond)
#假如我们想知道张量中为正数的索引是哪些
x = tf.random.normal([3,3])
x
# 直接返回了为true的坐标
indices = tf.where(x>0)
#获取索引后,又可以通过gather_nd获取相应的位置,并打包
tf.gather_nd(x,indices)
9. scatter_nd(indices,updates,shape)
可以高效的刷新张量的部分数据,可以根据索引,用update来更新shape
indices = tf.constant( [ [4],[3],[1],[7] ] ) # 坐标
updates = tf.constant( [1.,2.,3.,4.] ) # 对应坐标即将填充的值
tf.scatter_nd(indices , updates,[8]) # 在白板上填充值
10. tf.meshgrid()
# 暴力操作
points = []
for x in np.arange(-8,8,0.1):
for y in np.arange(-8,8,0.1):
z = x**2 + y**2
points.append([x,y,z])
len(points)
x = tf.linspace(-8.,8.,160) # 平均分为160份
y = tf.linspace(-8.,8.,160)
x,y = tf.meshgrid(x,y)
z = x**2 + y**2
fig = plt.figure()
ax = Axes3D(fig)
ax.contour3D(x,y,z,500)
plt.show()
11. 数据集加载与预处理
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 合并
train_db = tf.data.Dataset.from_tensor_slices( (x_train , y_train) )
# 打乱操作
train_db = train_db.shuffle(10000) #buffer_size 缓冲区大小,防止随机每次按照不一定的随机。
# 每次训练只是进行 小批量训练
#batch_size ,每一批训练的样本大小
train_db = train_db.batch(128)
# 预处理
#一开始的数据不满足我门的要求
def preprocess(x,y):
x = tf.cast(x,dtype=tf.float32)/255.
y = tf.one_hot(y,depth=10)
return x,y
# 直接就是一个映射
train_db = train_db.map(preprocess)
for x,y in train_db:
print(y)
# 一个batch 的训练 step
# 多个step完成一个epoch
# 多个epoch完成一个训练
for epoch in range(20):
for step , (x,y) in enumerate(train_db):
#训练
pass