1.用于创建张量的常用函数
①tf.constant(张量内容,dtype=数据类型(可选))
②tf.convert_to_tensor(数据名,dtype=数据类型(可选))
作用:将numpy的数据类型转换为Tensor数据类型
③tf.zeros(维度)
④tf.ones(维度)
⑤tf.fill(维度,指定值)
⑥tf.random.normal(维度,mean=均值,stddev=标准差)
作用:用于初始化参数生成正态分布的随机数,默认均值为0,标准差为1
⑦tf.random.truncated_normal(维度,mean=均值,stddev=标准差)
作用:生成截断式正态分布的随机数–>保证随机数在(m-2s,m+2s)之内
⑧tf.random.uniform(维度,minval=最小值,maxval=最大值) [min,max)
(维度:一维直接写个数,二维用[行,列],多维)
b = np.arange(0, 5)
a = tf.constant([[1, 2], [3, 4]], dtype=tf.int64)
c = tf.convert_to_tensor(b, dtype=tf.int64)
# d = tf.zeros(5, dtype=tf.int64)
# e = tf.ones([2, 4], dtype=tf.int64)
# f = tf.fill([2, 4], 6.7)
d = tf.random.normal([4, 4], mean=0, stddev=1)
e = tf.random.truncated_normal([4, 4], mean=0.5, stddev=1)
f = tf.random.uniform([4, 4], minval=-0.5, maxval=0.5)
print(a)
print(c)
print(d)
print(e)
print(f)
2. tf.cast(张量名,dtype=数据类型)
作用:强制tensor转换为该数据类型
x1 = tf.constant([[1.6, 2.4], [3.8, 4.0]], dtype=tf.float32)
x2 = tf.cast(x1, dtype=tf.int64)
print(x1)
print(x2)
3.tf.reduce_min(张量名),tf.reduce_max(张量名)
作用:计算张量维度上元素的最小(大)值
x1 = tf.constant([[1.6, 2.4], [3.8, 4.0]], dtype=tf.float32)
x2 = tf.cast(x1, dtype=tf.int64)
print(tf.reduce_min(x1), tf.reduce_max(x2))
4.tf.reduce_mean(张量名,axis=操作轴)
作用:计算张量沿着指定维度的平均值
理解axis:在一个二维张量或数组中,可以通过调整axis等于0或1控制执行维度。axis=0代表跨行(经度,down),axis=1代表跨列(纬度,across)(如果不指定axis,则所有元素参与运算)
x1 = tf.constant([[1.6, 2.4], [3.8, 4.0]], dtype=tf.float32)
x2 = tf.cast(x1, dtype=tf.int64)
print(tf.reduce_mean(x1, axis=1))
print(tf.reduce_mean(x1, axis=0))
5.tf.reduce_sum(张量名,axis=操作轴)
作用:计算张量沿着指定维度的和
x1 = tf.constant([[1.6, 2.4], [3.8, 4.0]], dtype=tf.float32)
x2 = tf.cast(x1, dtype=tf.int64)
print(tf.reduce_sum(x1, axis=1))
print(tf.reduce_sum(x1, axis=0))
6.tf.Variable(初始值)
作用:将变量标记为"可训练",被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记待训练参数。
7.数学运算
①四则:tf.add(张量1,张量2) tf.subtract tf.multiply tf.divide(维度相同)
②平方、次方、开方:tf.square,tf.pow(张量名,n次方数),tf.sqrt
③矩阵乘:tf.matmull()
8.tf.data.Dataset.from_tensor_slices(输入特征,标签)
作用:切分传入张量的第一维度,生成输入特征/标签对,构建数据集。(Numpy和Tensor格式都可用该语句读入数据)
feature = tf.constant([12, 4, 27, 9])
label = tf.constant([1, 0, 1, 0])
data = tf.data.Dataset.from_tensor_slices((feature, label))
print(data)
for element in data:
print(element)
9.tf.GradientTape()
作用:with结构记录计算过程,gradient求出张量的梯度
with tf.GradientTape() as tape:
w = tf.Variable(tf.constant(3.0))
loss = tf.pow(w, 2)
grad = tape.gradient(loss, w) # grad=tape.gradient(函数,对谁求导)
print(grad)
10.enumerate
作用:可遍历每个元素(如列表、元组或字符串)
组合为:索引 元素 ,常在for循环中使用。
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
11.tf.one_hot(待转换数据,depth=几分类)
作用:将待转换数据,转换为one-hot形式的数据输出。
独热编码:在分类问题中,常用独热编码做标签,标记类别:1表示是,0表示非。
例如:(0狗尾草鸢尾 1杂色鸢尾 2弗吉尼亚鸢尾)
标签:1 -->独热码:(0. 1. 0.)
classes = 3
labels = tf.constant([0, 1, 2])
output = tf.one_hot(labels, classes)
print(output)
12.tf.nn.softmax()
作用:使输出符合概率分布,才可以与独热码的标签作比较。
y1 = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y1)
print(y_pro)
13.assign_sub
作用:赋值操作,更新参数的值并返回。
调用assign_sub前,先用tf.Variable定义变量w为可训练(可自更新)
w.assign_sub(w要自减的内容)
14.tf.argmax(张量名,axis=操作轴)
作用返回张量沿指定维度最大值的索引
test = np.array([[3, 4, 5], [2, 5, 1], [6, 4, 1]])
print(test)
print(tf.argmax(test, axis=0))
print(tf.argmax(test, axis=1))