import tensorflow as tf
w = tf.Variable(tf.constant(5, dtype=tf.float32))#5是张量的内容,dtype=数据类型(可选)
#w = tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))
# tf.random.normal(维度,mean=均值, stddev=标准差)生成正态分布的随机数,默认均值是0,标准差是1
#tf.random.truncated_normal()生成截断式正态分布的随机数,如果随机生成的数据取值在(mean-2*stddev,mean+2*stddev)之外,则重新生成
#tf.random.uniform(维度,minval=最小值,maxval=最大值)
#常用函数1
#tf.cast(张量名,dtype=数据类型) 强制tensor转换为该数据类型,比如float(64)->int(32)
#tf.reduce_min(张量名)计算张量维度上元素的最小值,返回的就是最小的一个数。max同理
#tf.reduce_mean(张量名,axis=操作轴)计算沿着指定维度的平均值
#tf.reduce_sum(张量名,axis=操作轴)计算张量沿着指定维度的和 如不指定axis则对所有元素进行操作,axis=0纵向操作,axis=1横向操作
#举例子
x=tf.constant([[1,2,3],[2,2,3]])
print(x)
print(tf.reduce_mean(x))#mean是2,因为类型是int32
print(tf.reduce_sum(x,axis=1))#把row相等的值相加 tf.Tensor([6 7], shape=(2,), dtype=int32)
print(tf.reduce_sum(x,axis=0))#把col相等的值相加 tf.Tensor([3 4 6], shape=(3,), dtype=int32)
x=tf.constant([[[1,2,3],[2,2,3],[4,6,9]],[[2,3,4],[3,4,5],[4,5,6]],[[1,2,3],[2,3,8],[2,7,9]]])
print(x)#shape=(3, 3, 3),为三维张量
print(tf.reduce_mean(x))#tf.Tensor(3, shape=(), dtype=int32)
print(tf.reduce_sum(x,axis=0))
print(tf.reduce_sum(x,axis=1))#shape=(3, 3) 也就是说经过此操作,张量向下降一维
print(tf.reduce_sum(x,axis=2))
#通过观察,得知axis=0时,对三维张量中的每个二维张量中的相同row和col的数相加,得到二维张量
#当axis=1时,对每个二维张量的列相加,从左到右,从上到下的顺序得到二维张量
#axis=2时,对每个二维张量的行求和
#tf.add(a,b)加,tf.subtract(a,b)减,tf.multiply(a,b)乘,tf.divide(b,a)除。张量的维数必须相同,且是对相应的元素之间进行操作
#tf.square(张量名)
#tf.pow(张量名)
#tf.sqrt(张量名)
#常用函数2
#用with结构记录计算过程,gradient求出张量的梯度
# with tf.GradientTape()as tape:
#若干个计算过程
#grad=tape.gradient(函数,对谁求导)
with tf.GradientTape()as tape:
w = tf.Variable(tf.constant(3.0))#就是w=3.0,同时也意味着这是个浮点类型。注意不能是3,不然就会显示None,报错
loss = tf.pow(w,2)#loss是w的平方
grad = tape.gradient(loss,w)#对w求导,代值
print(grad)
#enumerate是python的内建函数是枚举的意思,可以遍历每个元素(如列表、元组或字符串),组合为:索引 元素 常在for循环中使用
seq = ['one','two','three']
for i,element in enumerate(seq):
print(i,element)
#tf.one_hot 独热编码(one_hot encoding):在分类问题中,常用独热码做标签,标记类别:1表示是,0非
#比如鸢尾花分类,有三类
classes = 3
labels = tf.constant([1,0,2,2,1,0])#在一维张量中给出每一个鸢尾花的类别
output = tf.one_hot(labels,depth=classes)#用0和1在每朵鸢尾花的类别下标记,depth是列数,和类别数相等
print(output)
#tf.nn.softmax()使输出符合概率分布,也就是输出之和为1
y = tf.constant([1.01,2.01,-0.66])
y_pro = tf.nn.softmax(y)
print('After softmax,y_pro is: ',y_pro)
#assign_sub赋值操作,更新参数值并返回,调用assign_sub前,要用tf.variable定义变量w为可训练
w = tf.Variable(4)
w.assign_sub(1)#w自减1,变为3
print(w)
#tf.argmax(张量名,axis=操作轴) 返回张量沿指定维度的最大值的索引号
import numpy as 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))#返回每一行最大值的索引
t_2 = tf.constant([4,3,2])
#要创建一个所有元素为零的张量,可以使用 tf.zeros() 函数
print(tf.zeros([2,3],tf.int32))#和variable一样,[2,3]是shape
#创建与现有 Numpy 数组或张量常量具有相同形状的张量常量
print(tf.zeros_like(t_2))#这个张量会和t_2有相同的形状
print(tf.ones_like(t_2))
# 在一定范围内生成一个从初值到终值等差排布的序列:
# tf.linspace(start,stop,num)
w = tf.Variable(tf.constant(5,dtype=tf.float32))#其实就是个5.0
lr = 0.5
epoch = 40
for epoch in range(epoch): # for epoch 定义顶层循环,表示对数据集循环epoch次,此例数据集数据仅有1个w,初始化时候constant赋值为5,循环40次迭代。
with tf.GradientTape() as tape: # with结构到grads框起了梯度的计算过程。
loss = tf.square(w + 1)
grads = tape.gradient(loss, w) # .gradient函数告知谁对谁求导
w.assign_sub(lr * grads) # .assign_sub 对变量做自减 即:w -= lr*grads 即 w = w - lr*grads
#既然lr就只是个常数,我们能直接用常数来代替这个lr吗?答案是肯定的
print("After %s epoch,w is %f,loss is %f" % (epoch, w.numpy(), loss))
# lr初始值:0.2 请自改学习率 0.001 0.999 看收敛过程
# 最终目的:找到 loss 最小 即 w = -1 的最优参数w
深度学习第一章的常用函数与神经网络设计
最新推荐文章于 2024-01-17 14:51:55 发布