维数 | 阶 | 名字 | 例子 |
---|---|---|---|
0 | 0 | 标量() | s = 1 2 3 |
1 | 1 | 向量() | v = [1,2,3] |
2 | 2 | 矩阵() | m = [[1,2,3],[4,5,6],[7,8,9]] |
n | n | 张量() | t = [[[[[[[[ n个“[” |
tf.constant(张量内容,dtype=数据类型(可选))
#建立一个张量(Tensor) import tensorflow as tf a = tf.constant([1, 5], dtype=tf.int64) #建立1阶张量,里面两个元素分别为1,5 #指定数据类型为64位整型 print("a:", a) print("a.dtype:", a.dtype) #打印出a的数据类型 print("a.shape:", a.shape) #打印出a的形状
a: tf.Tensor([1 5], shape=(2,), dtype=int64) a.dtype: <dtype: 'int64'> a.shape: (2,)
张量的形状看shape=(2,)中的逗号隔开了几个数字,上面示例中的逗号隔开了一个数字2,因此是1维张量,数字是2,说明这个张量里有两个元素,也就是上面的数值1和数值5python
tf. convert_to_tensor(数据名,dtype=数据类型(可选))
#将numpy的数据类型转换为Tensor数据类型 import tensorflow as tf import numpy as np a = np.arange(0, 5) b = tf.convert_to_tensor(a, dtype=tf.int64) #将numpy格式a变成了Tensor格式b print("a:", a) print("b:", b)
a: [0 1 2 3 4] b: tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
tf. zeros(维度)
tf. ones(维度)
tf. fill(维度,指定值)
#建立全为0,1,指定值的张量 import tensorflow as tf a = tf.zeros([2, 3]) #建立一个2维张量,第一个维度有2个元素,第二个维度有3个元素,元素内容全是0 b = tf.ones(4) #建立一个一维张量,里面有4个元素,内容全是1 c = tf.fill([2, 2], 9) #建立一个两行两列的二维张量,第一个维度有2个元素,第二个维度有2个元素,元素都是9 print("a:", a) print("b:", b) print("c:", c)
a: tf.Tensor( [[0. 0. 0.] [0. 0. 0.]], shape=(2, 3), dtype=float32) b: tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32) c: tf.Tensor( [[9 9] [9 9]], shape=(2, 2), dtype=int32)
维度:
一维 括号里直接写数字
二维 用 [行,列]
多维 用 [n,m,j,k……],括号里写每一个维度的元素个数,中间用逗号隔开数组
tf. random.normal (维度,mean=均值,stddev=标准差)
tf. random.truncated_normal (维度,mean=均值,stddev=标准差)
在tf.truncated_normal中若是随机生成数据的取值在(μ-2σ,μ+2σ)以外
则从新进行生成,保证了生成值在均值附近。μ:均值, σ:标准差网络
import tensorflow as tf d = tf.random.normal([2, 2], mean=0.5, stddev=1) #生成2行,2列的张量,里面的元素符合以0.5为均值,1为标准差的正态分布 print("d:", d) e = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1) #生成2行,2列的张量,里面的元素符合以0.5为均值,1为标准差的截断式正态分布,生成的元素在{均值±(2*标准差)}以内 #数据更向均值0.5集中 print("e:", e)
d: tf.Tensor( [[0.78079236 0.36991078] [0.5447546 0.85526705]], shape=(2, 2), dtype=float32) e: tf.Tensor( [[-1.0678291 -0.20061862] [ 0.1491285 0.40372872]], shape=(2, 2), dtype=float32)
tf. random. uniform(维度,minval=最小值,maxval=最大值)
#生成平均分布的随机数 import tensorflow as tf f = tf.random.uniform([2, 2], minval=0, maxval=1) #生成两行两列张量,,其中的每一个元素都符合在0~1之间的平均分布 print("f:", f)
f: tf.Tensor( [[0.13252604 0.0960362 ] [0.35313892 0.33435488]], shape=(2, 2), dtype=float32)
tf.cast (张量名,dtype=数据类型)
tf.reduce_min (张量名)
tf.reduce_max (张量名)
#给定张量,转换张量的类型 #找出张量的最小值,最大值 import tensorflow as tf x1 = tf.constant([1., 2., 3.], dtype=tf.float64) #构建一个张量x1 print("x1:", x1) x2 = tf.cast(x1, tf.int32) #将x1变成32位整型 print("x2", x2) print("minimum of x2:", tf.reduce_min(x2)) print("maxmum of x2:", tf.reduce_max(x2)) #打印x2的最小值,最大值
x1: tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64) x2 tf.Tensor([1 2 3], shape=(3,), dtype=int32) minimum of x2: tf.Tensor(1, shape=(), dtype=int32) maxmum of x2: tf.Tensor(3, shape=(), dtype=int32)
在一个二维张量或数组中,能够经过调整 axis 等于0或1 控制执行维度。dom
tf.reduce_mean (张量名,axis=操做轴)
tf.reduce_sum (张量名,axis=操做轴)
#axis import tensorflow as tf x = tf.constant([[1, 2, 3], [2, 2, 3]]) print("x:", x) print("mean of x:", tf.reduce_mean(x)) # 不指定axis,求x中全部数的均值 print("sum of x:", tf.reduce_sum(x, axis=1)) # 沿横向方向求每一行的和
x: tf.Tensor( [[1 2 3] [2 2 3]], shape=(2, 3), dtype=int32) mean of x: tf.Tensor(2, shape=(), dtype=int32) sum of x: tf.Tensor([6 7], shape=(2,), dtype=int32)
被标记的变量会在反向传播中记录梯度信息。ide
神经网络训练中,经常使用该函数标记待训练参数。函数
tf.Variable(初始值)
#神经网络中初始化参数w的代码以下 w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1)) #首先随机生成正态分布随机数, #再给生成的随机数标记为可训练,
tf.add,tf.subtract,tf.multiply,tf.dividecode
#实现两个张量的对应元素相加 tf.add (张量1,张量2) #实现两个张量的对应元素相减 tf.subtract (张量1,张量2) #实现两个张量的对应元素相乘 tf.multiply (张量1,张量2) #实现两个张量的对应元素相除 tf.divide (张量1,张量2)
#张量之间的加减乘除 import tensorflow as tf a = tf.ones([1, 3]) #建立一个1行3列的张量a,全部元素是1 b = tf.fill([1, 3], 3.) #建立一个1行3列的张量2,全部元素是3 print("a:", a) print("b:", b) print("a+b:", tf.add(a, b)) #a和b对应元素相加 print("a-b:", tf.subtract(a, b)) #a和b对应元素相减 print("a*b:", tf.multiply(a, b)) #a和b对应元素相乘 print("b/a:", tf.divide(b, a)) #b中元素除以a
a: tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32) b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32) a+b: tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32) a-b: tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32) a*b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32) b/a: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
tf.square,tf.pow,tf.sqrtorm
#计算某个张量的平方 tf.square (张量名) #计算某个张量的n次方 tf.pow (张量名,n次方数) #计算某个张量的开方 tf.sqrt (张量名)
#张量次方,平方,开方操做 import tensorflow as tf a = tf.fill([1, 2], 3.) #构建一个1行2列的二维张量,填充数值都是3 print("a:", a) print("a的三次方:", tf.pow(a, 3)) #对a求三次方 print("a的平方:", tf.square(a)) #对a求平方 print("a的开方:", tf.sqrt(a)) #对a求开方
a: tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32) a的三次方: tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32) a的平方: tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32) a的开方: tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)
#实现两个矩阵的相乘 tf.matmul(矩阵1,矩阵2)
#矩阵相乘 import tensorflow as tf a = tf.ones([3, 2]) #3行2列全1矩阵a b = tf.fill([2, 3], 3.) #2行3列全3矩阵b print("a:", a) print("b:", b) print("a*b:", tf.matmul(a, b)) #结果是3行3列全6矩阵
a: tf.Tensor( [[1. 1.] [1. 1.] [1. 1.]], shape=(3, 2), dtype=float32) b: tf.Tensor( [[3. 3. 3.] [3. 3. 3.]], shape=(2, 3), dtype=float32) a*b: tf.Tensor( [[6. 6. 6.] [6. 6. 6.] [6. 6. 6.]], shape=(3, 3), dtype=float32)
神经网络是将输入特征和标签配对后再送入神经网络的,视频
tf.data.Dataset.from_tensor_slices能够实现将标签和特征进行配对,此函数对于numpy格式和tensor格式都适用索引
data = tf.data.Dataset.from_tensor_slices((输入特征, 标签))
#输入特征和标签配对 import tensorflow as tf features = tf.constant([12, 23, 10, 17]) #收集的特征是12,23,10,17 labels = tf.constant([0, 1, 1, 0]) #每一个特征对应的标签分别是0,1,1,0 dataset = tf.data.Dataset.from_tensor_slices((features, labels)) #使用tf.data.Dataset.from_tensor_slices()将特征和标签配上对 for element in dataset: #分别打印每个dataset print(element)
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>) (<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>) (<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=1>) (<tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
在with结构中使用tf.GradientTape实现某个函数对指定参数的求导运算
配合上面的tf.Variable()函数能够实现损失函数loss对参数w的求导运算
with tf.GradientTape( ) as tape: 若干个计算过程 grad=tape.gradient(函数,对谁求导)
#求导运算 import tensorflow as tf with tf.GradientTape() as tape: x = tf.Variable(tf.constant(3.0)) #将变量x设置为可训练的 y = tf.pow(x, 2) #y=x^2 grad = tape.gradient(y, x) #y对x求导,dy/dx=2*x,带入x=3,dy/dx=6.0 print(grad)
tf.Tensor(6.0, shape=(), dtype=float32)
enumerate是python的内建函数,它可遍历每一个元素(如列表、元组或字符串),组合为:索引 元素,常在for循环中使用。
enumerate(列表名)
#枚举 seq = ['one', 'two', 'three'] #新建列表赋值给seq for i, element in enumerate(seq): #enumerate()括号里面是列表名; #i接收索引号,element接收元素 print(i, element)
0 one 1 two 2 three
在实现分类问题时,常使用独热码来表示标签
(0)狗尾草鸢尾花 | (1)杂色鸢尾花 | (2)弗吉尼亚鸢尾花 | |
---|---|---|---|
标签为1对应的独热码 | 0 | 1 | 0 |
独热码表示的意义 | 0%的可能表示狗尾草鸢尾 | 100%的可能表示杂色鸢尾 | 0%的可能表示弗吉尼亚鸢尾 |
tf.one_hot (待转换数据, depth=几分类)
#独热码转换 import tensorflow as tf classes = 3 #3分类 labels = tf.constant([1, 0, 2]) #待转换的数据 #输入的三个标签分别是1,0,2 #输入的元素值最小为0,最大为2 output = tf.one_hot(labels, depth=classes) print("result of labels1:", output) print("\n")
result of labels1: tf.Tensor( [[0. 1. 0.] [1. 0. 0.] [0. 0. 1.]], shape=(3, 3), dtype=float32)
tf.nn.softmax使输出符合几率分布,也就是将不一样的输出转化成相对应的几率,输出的和为1
tf.nn.softmax(x) 使输出符合几率分布
使n分类的n个输出 (y0 ,y1, …… yn-1)经过softmax( ) 函数,符合几率分布,也就是将每一个输出值转化成0到1之间的几率值,,而且这些几率的和是1
#将神经网络前向传播结果转化成相对应的几率 import tensorflow as tf y = tf.constant([1.01, 2.01, -0.66]) #将前向传播结果1.01,2.01,-0.66组成张量y y_pro = tf.nn.softmax(y) #将相应的y送入softmax函数,转化成相对应的几率 print("After softmax, y_pro is:", y_pro) # y_pro 符合几率分布 print("The sum of y_pro:", tf.reduce_sum(y_pro)) # 经过softmax后,全部几率加起来和为1
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32) The sum of y_pro: tf.Tensor(1.0, shape=(), dtype=float32)
调用assign_sub前,先用 tf.Variable 将等待更新的变量 w 为可训练(可自更新),才能够实现自更新
w.assign_sub (w要自减的内容)
#自减,参数更新 import tensorflow as tf x = tf.Variable(4) #x先被定义为variable类型,初始值是4 x.assign_sub(1) #对x作自减操做,即x=x-1,自减的内容写在括号里,括号里是1,则表示自减1 print("x:", x) # 4-1=3
x: <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>
注意返回的是索引号而不是值
tf.argmax (张量名,axis=操做轴)
import numpy as np import tensorflow as tf test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]]) #2维张量test print("test:\n", test) print("每一列的最大值的索引号:", tf.argmax(test, axis=0)) # 返回纵向每一列最大值的索引号 print("每一行的最大值的索引号:", tf.argmax(test, axis=1)) # 返回横向每一行最大值的索引号
test: [[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)
参考视频及资料:https://www.bilibili.com/video/BV1B7411L7Qt?from=search&seid=202820015499098798