Tensorflow 入门学习日志1

版本 tensoflow 2.5.0

张量(tensor):多维数组(列表) 阶:张量的维数

维数名字例子
0-D0标量scalars=123
1-D1向量vectorv=[1,2,3]
2-D2矩阵matrixm=[[1,2,3],[4,5,6],[7,8,9]]
n-Dn张量tensort=[[[(n个方括号)

张量可表示0阶到n阶数组(列表) 

ps:有几个方括号就是几阶张量

 数据类型

tf.int,tf.float,......

tf.int32, tf.float32, tf.float64

tf.bool

tf.constant([True,False])

tf.string

tf.constant("Hello world")

创建张量的方法:

tf.constant(张量内容,dtype=数据类型(可选))
———————————————————————————————————————————————————————————————————————————————————————————
import tensorflow as tf
a = tf.constant([1,5],dtype=tf.int64)
print(a)
print(a.dtype)
print(a.shape)
tf.Tensor([1 5], shape=(2,), dtype=int64)
<dtype: 'int64'>
(2,)

其中,张量的形状看shape的逗号隔开了几个数字,上例中隔开了一个数字,所以是一维张量,数字2表示的是这个一维张量中包含两个元素(分别是数值1和5)

tf.zeros(维度)#创建元素全为0的张量
tf.ones(维度)#创建元素全为1的张量
tf.fill(维度,指定值)#创建全为指定值的张量
———————————————————————————————————————————————————————————————————————————————————————————
a=tf.zeros([2,3])#2行3列的0矩阵,2维张量
b=tf.ones(4)#创建一个1维张量,其中有4个元素
c=tf.fill([2,2],9)#创建一个2维张量,全部元素都为9
print(a)
print(b)
print(c)

结果:
tf.Tensor([[0. 0. 0.][0. 0. 0.]], shape=(2, 3), dtype=float32)
tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)
tf.Tensor([[9 9][9 9]], shape=(2, 2), dtype=int32)

随机生成初始化参数:

1.生成正态分布的随机数,默认均值为0,标准差为1

tf.random.normal(维度,mean=均值, stddev=标准差)

2.生成截断式正态分布随机数(能确保所生成的随机数在(\mu -2\sigma,\mu +2\sigma) 之间)

tf.random.truncated_normal(维度,mean=均值,stddev=标准差)

3.生成均匀分布的随机数     [minval,maxval) 左闭右开区间

tf.random.uniform(维度,minval=最小值,maxval=最大值)

常用函数

1.强制类型转换

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

2.计算张量维度上元素的最小值、最大值

tf.reduce_min(张量名)
tf.reduce_max(张量名)

3.理解axis

在二维张量中,通过调整axis等于0或1控制执行维度

(1)axis=0代表沿纵向计算,即计算列,axis=1代表沿横向方向,即计算行

(2)若不指定axis,则所有元素都将参与计算

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

4.tf.Variable

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

tf.Variable(初始值)

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

5.数学运算(必须保证在维度相同的情况下才能进行四则运算)

tf.add, tf.subtract, tf.multiply, tf.divide (加减乘除)
tf.square, tf.pow(张量名,n次方数), tf.sqrt (平方,次方,开方)
tf.matmul (矩阵乘法)

6.特征与标签配对函数

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

data = tf.data.Dataset.from_tensor_slices((输入特征,标签))
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)

结果:
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)> (特征,标签)配对
(<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>)

7.求导运算

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

with tf.GradientTape() as tape:
    若干计算过程
gards = tape.gradient(函数,对谁求导)
——————————————————————————————————————————————————————————————————————————————————————————
with tf.GradientTape() as tape:
    w = tf.Variable(tf.constant(3.0))
    loss = tf.pow(w,2)
grad = tape.gradient(loss,w)
print(grad)

结果:
tf.Tensor(6.0, shape=(), dtype=float32)

8.枚举函数

enumerate可遍历每个元素,组合为:索引 元素,常在for循环中使用

seq=['one','two','three']
for i,element in enumerate(seq):
    print(i,element)

结果:
0 one
1 two
2 three

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

tf.one_hot(待转换数据,depth=几分类)
———————————————————————————————————————————————————————————————————————————————————————————
classes = 3
labels = tf.constant([1,0,2])
output = tf.one_hot(labels,depth=classes)
print(output)

结果:
tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

10.使输出结果符合概率分布

tf.nn.softmax(x)
———————————————————————————————————————————————————————————————————————————————————————————
y=tf.constant([1.01,2.01,-0.66])
y_pro=tf.nn.softmax(y)
print("After softmax, y_pro is:",y_pro)

结果:
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)

11.参数的自更新函数

常用于:(1)赋值操作,更新参数的值并返回(2)调用assign_sub前,先用tf.Variable定义变量w为可训练(可自更新)

w.assign_sub(w要自减的内容)
———————————————————————————————————————————————————————————————————————————————————————————
w=tf.Variable(4)#使用tf.Variable()对参数w定义为可训练
w.assign_sub(1)#自减操作,即w=w-1
print(w)

结果:
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

 12.返回张量沿指定维度的最大索引值函数tf.argmax()

tf.argmax(张量名,axis=操作轴)
———————————————————————————————————————————————————————————————————————————————————————————
test = tf.constant([[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))

结果:
tf.Tensor(
[[1 2 3]
 [2 3 4]
 [5 4 3]
 [8 7 2]], shape=(4, 3), dtype=int32)
tf.Tensor([3 3 1], shape=(3,), dtype=int64)
tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值