【TensorFlow】TensorFlow的常用函数

强制tensor转换为该数据类型

tf.cast(张量名,dtype=数据类型)

最大最小值

计算张量维度上元素的最小值

tf.reduce_min(张量名)

计算张量维度上元素的最大值

tf.reduce_max(张量名)
x1 = tf.constant([1., 2., 3.],dtype=tf.float64)
print(x1)
x2 = tf.cast(x1, tf.int32)
print(x2)
print (tf.reduce_min(x2), tf.reduce_max(x2))
运行结果:
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=intt32)

axis

在一个二维张量或数组中,可以通过调整axis 等于0或1 控制执行维度。

axis=0代表跨行(经度,down),而axis=1代表跨列(纬度,across)
如果不指定axis,则所有元素参与计算。
在这里插入图片描述
计算张量沿着指定维度的平均值

tf.reduce_mean(张量名,axis=操作轴)

计算张量沿着指定维度的和

tf.reduce_sum(张量名,axis=操作轴)
x=tf.constant( [ [ 1, 2, 3],[ 2, 2, 3] ] )
print(x)
print(tf.reduce_mean( x ))
print(tf.reduce_sum( x, axis=1 ))
运行结果:
tf.Tensor(
[[1 2 3]
[2 2 3]], shape=(2, 3), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor([6 7], shape=(2,), dtype=int32)

tf.Variable

tf.Variable()将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记待训练参数。

tf.Variable(初始值)
w=tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))

数学计算

对应元素的四则运算:tf.add,tf.subtract,tf.multiply,tf.divide

平方、次方与开方:tf.square,tf.pow,tf.sqrt

矩阵乘:tf.matmul


实现两个张量的对应元素相加

tf.add(张量1,张量2)

实现两个张量的对应元素相减

tf.subtract(张量1,张量2)

实现两个张量的对应元素相乘

tf.multiply(张量1,张量2)

实现两个张量的对应元素相除

tf.divide(张量1,张量2)

只有维度相同的张量才可以做四则运算

a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.)
print(a)
print(b)
print(tf.add(a,b))
print(tf.subtract(a,b))
print(tf.multiply(a,b))
print(tf.divide(b,a))
运行结果:
tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32
tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)

计算某个张量的平方

tf.square(张量名)

计算某个张量的n次方

tf.pow(张量名,n次方数)

计算某个张量的开方

tf.sqrt(张量名)
a = tf.fill([1, 2], 3.)
print(a)
print(tf.pow(a, 3))
print(tf.square(a))
print(tf.sqrt(a))
运行结果:
tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32)
tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32)
tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32)
tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)

实现两个矩阵的相乘

tf.matmul(矩阵1,矩阵2)
a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.)
print(tf.matmul(a, b))
运行结果:
tf.Tensor(
[[6. 6. 6.]
[6. 6. 6.]
[6. 6. 6.]], shape=(3, 3), dtype=float32)

生成输入特征/标签对,构建数据集

切分传入张量的第一维度,生成输入特征/标签对,构建数据集

data = tf.data.Dataset.from_tensor_slices((输入特征, 标签))

(Numpy和Tensor格式都可用该语句读入数据)

features = tf.constant([12,23,10,17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(dataset)
for element in dataset:
print(element)
运行结果:
<TensorSliceDatasetshapes: ((),()), types: (tf.int32, tf.int32))>
(<tf.Tensor: id=9, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)

求梯度

with结构记录计算过程,gradient求出张量的梯度

with tf.GradientTape( ) as tape:
	若干个计算过程
grad=tape.gradient(函数,对谁求导)
with tf.GradientTape( ) as tape:
	w = tf.Variable(tf.constant(3.0))
	loss = tf.pow(w,2) #loss=w^2 loss’=2w
grad = tape.gradient(loss,w)
print(grad)
运行结果:
tf.Tensor(6.0, shape=(), dtype=float32)

enumerate

enumerate是python的内建函数,它可遍历每个元素(如列表、元组或字符串),组合为:索引 、元素,常在for循环中使用。

enumerate(列表名)
seq= ['one', 'two', 'three']
for i, element in enumerate(seq):
	print(i, element)
运行结果:
0 one
1 two
2 three

独热编码

独热编码(one-hot encoding):在分类问题中,常用独热码做标签,标记类别:1表示是,0表示非。

在这里插入图片描述
tf.one_hot()函数将待转换数据,转换为one-hot形式的数据输出。

tf.one_hot(待转换数据, depth=几分类)
classes = 3
labels = tf.constant([1,0,2]) #输入的元素值最小为0,最大为2
output = tf.one_hot( labels, depth=classes )
print(output)
运行结果:
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]], shape=(3, 3), dtype=float32)

将结果得分转化为符合概率分布的函数

在这里插入图片描述

在这里插入图片描述

公式如下:
在这里插入图片描述

在TensorFlow中,提供了tf.nn.softmax(x)函数,使输出符合概率分布:
当n分类的n个输出(y0,y1, …… yn-1)通过softmax( ) 函数,便符合概率分布了。

在这里插入图片描述

y = tf.constant( [1.01, 2.01, -0.66] )
y_pro= tf.nn.softmax(y)
print("After softmax, y_prois:", y_pro)
输出结果:
After softmax, y_prois: tf.Tensor([0.255981740.695830460.0481878], shape=(3,), dtype=float32)

自减

赋值操作,更新参数的值并返回。

调用assign_sub前,先用tf.Variable定义变量w为可训练(可自更新)

w.assign_sub(w要自减的内容)

w = tf.Variable(4)
w.assign_sub(1)
print(w)
运行结果:
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

求指定维度的最大值

返回张量沿指定维度最大值的索引
在这里插入图片描述

tf.argmax(张量名,axis=操作轴)
import numpyas np
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print(test)
print( tf.argmax(test, axis=0)) # 返回每一列(经度)最大值的索引
print( tf.argmax(test, axis=1)) # 返回每一行(纬度)最大值的索引
运行结果:
[[1 2 3]
[2 3 4]
[5 4 3]
[8 7 2]]
tf.Tensor([3 3 1], shape=(3,), dtype=int64)
tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)

猜你喜欢

转载自blog.csdn.net/qq_45654306/article/details/112657420