Tensoflow 基础知识

合并与分割

1.合并

合并是指将多个张量在某个维度上合并为一个张量。张量的合并可以使用拼接(Concatenate)和堆叠(Stack)操作实现,拼接操作并不会产生新的维度,仅在现有的维度上合并,而堆叠会创建新维度。选择使用拼接还是堆叠操作来合并张量,取决于具体的场景是否需要创建新维度。

  • 拼接
a = tf.random.normal([4,32,5])
b = tf.random.normal([6,32,5])
tf.concat([a,b],axis=0)
'''

  • 堆叠
a = tf.random.normal([32,32])
b = tf.random.normal([32,32])
tf.stack([a,b],axis=0)
'''

'''
分割

合并操作的逆过程就是分割,将一个张量分拆为多个张量。

x = tf.random.normal([10,35,8])
result = tf.split(x,num_or_size_splits=10,axis=0)
print(len(result))
print(result[0].shape)
'''
10
(1, 35, 8)
'''
x = tf.random.normal([10,35,8])
result = tf.split(x,num_or_size_splits=[4,2,2,2],axis=0)
print(len(result))
print(result[0].shape)
'''
4
(4, 35, 8)
'''
x = tf.random.normal([10,35,8])
result = tf.unstack(x,axis=0)
print(len(result))
print(result[0].shape)
'''
10
(35, 8)
'''

数据统计

向量范数

向量范数(Vector Norm)是表征向量“长度”的一种度量方法,它可以推广到张量上。在神经网络中,常用来表示张量的权值大小、梯度大小等。
L1范数:向量x的所有元素绝对值之和;
L2范数:向量x的所有元素的平方和,再开根号;
∞范数:向量x的所有元素绝对值的最大值。

x = tf.ones([3,3])
print(tf.norm(x,ord=1))#L1范数
print(tf.norm(x,ord=2))#L2范数
print(tf.norm(x,ord=np.inf))#∞范数
'''
tf.Tensor(9.0, shape=(), dtype=float32)
tf.Tensor(3.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
'''
最值、均值、和
  • 求出每个样本的概率最大值
x = tf.random.normal([4,10])
tf.reduce_max(x,axis=1)
tf.reduce_min(x,axis=1)
tf.reduce_mean(x,axis=1)
'''



'''

当不指定axis参数是,求出的解为全局元素的最大、最小、均值、和等数据

x = tf.random.normal([4,10])
tf.reduce_max(x),tf.reduce_min(x),tf.reduce_mean(x),tf.reduce_sum(x)
'''
(,
 ,
 ,
 )
'''
张量比较
out = tf.random.normal([100,10])#模拟输出
out = tf.nn.softmax(out,axis=1)#将输出转换为概率
#print(out)
pred = tf.argmax(out,axis=1)#计算预测值
#print(pred)
y = tf.random.uniform([100],dtype=tf.int64,maxval=10)#模拟产生标签
#print(y)
out = tf.equal(pred,y)#预测值与真实值进行对比,返回为bool值
#print(out)
out = tf.cast(out,dtype=tf.float32)#类型转换
#print(out)
correct = tf.reduce_sum(out)#统计正确的个数
correct
'''

'''
填充与复制
  • 填充
    对于图片数据的高和宽、序列信号的长度,维度长度可能各不相同。为了方便网络的并行计算,需要将不同长度的数据扩张为相同长度。
a = tf.constant([1,2,3,4,5,6])
b = tf.constant([1,2,3])
b = tf.pad(b,[[0,3]])#在末尾填充3个零
c = a+b
c
tf.stack([a,b],axis=0)#堆叠合并,创建新的维度
'''


'''
total_words = 10000
max_review = 80
embedding_len = 100
(x_train,x_test),(y_train,y_test) = tf.keras.datasets.imdb.load_data(num_words=total_words)
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train,maxlen=max_review)#将句子填充或截断到相同长度
  • 复制
x = tf.random.normal([2,1,2,1])
tf.tile(x,[2,2,2,2])
'''

数据限幅

如何实现非线性激活函数ReLU,可以通过简单的数据限幅运算实现,限制元素的范围为x∈[0,+∞)。

x = tf.range(8)
tf.maximum(x,3)#下限幅为2
'''

'''
y = tf.range(8)
tf.minimum(x,5)#上限幅为5
'''

'''
z = tf.range(8)
tf.minimum(tf.maximum(z,3),6)#限幅为3~6
'''

'''
x = tf.range(8)
tf.clip_by_value(x,2,5)
'''

'''
进阶操作
  • 根据索引号收集数据的目的
x = tf.random.uniform([3,10,4],maxval=20,dtype=tf.int32)
tf.gather(x,[0,1],axis=0)
'''

'''
x = tf.random.uniform([10,10],maxval=20,dtype=tf.int32)
tf.gather(x,[0,5,1,8],axis=0)
'''

'''
x = tf.random.uniform([10,10],maxval=20,dtype=tf.int32)
x
'''

'''
tf.stack([x[1,1],x[2,2],x[3,3]],axis=0)
'''

'''
tf.gather_nd(x,[[1,1],[2,2],[3,3]])#多维坐标实现多点采样
'''

'''
  • 通过给定掩码的方式进行采样
x = tf.random.uniform([4,10],maxval=20,dtype=tf.int32)
x
'''

'''
tf.boolean_mask(x,mask=[True,False,False,True],axis=0)#选取第一行和第四行数据
'''

'''
tf.boolean_mask(x,[[True,False,False],[False,True,True],[True,False,True],[False,True,False]])
'''

'''
  • 根据条件的真假从参数A或B
a = tf.ones([3,3])
b = tf.zeros([3,3])
cond = tf.constant([[True,False,True],[False,True,False],[False,False,True]])#条件
tf.where(cond)#True元素的索引位置
'''

'''
tf.where(cond,a,b)#根据条件采样
'''

'''
  • 刷新张量的部分数据
    需要刷新的数据索引号用indices表示,新数据为updates。根据indices给出的索引位置将updates中新的数据依次写入白板中,并返回更新后的结果张量。
indices = tf.constant([[2],[4],[6],[7]])#更新数据的索引位置
updates = tf.constant([2,4,6,7])#需要写入的数据
tf.scatter_nd(indices,updates,[8])#其余位置写入0向量
'''

'''
  • 二维网格
x = tf.linspace(-8,8,100)
y = tf.linspace(-8,8,100)
x,y = tf.meshgrid(x,y)#生成网格点
z =tf.sqrt(x**2+y**2)
z = tf.sin(z)/z
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax =Axes3D(fig)#设置三维坐标曲面
#根据网格点绘制sinc
ax.contour3D(x.numpy(),y.numpy(),z.numpy(),50)
plt.show()

Tensoflow 基础知识_第1张图片

你可能感兴趣的:(python,深度学习)