[tensorflow2笔记二] 常用函数

常用函数整理:

函数说明
tf.cast(张量名,dtype=数据类型)强制数据类型转换
reduce_mean(张量名, axis=操作轴)计算张量沿指定维度的平均值
tf.Variable()将变量标记为“可训练”
tf.data.Dataset.from_tensor_slices((输入特征,标签))将数据和标签配对
tf.GradientTape()在with结构中,使用gradient求出张量的梯度
enumerate可以遍历每个元素(组合:索引,元素)
tf.one_hot(代转换数据,depth=几分类)可以将标签直接转换为独热码。
tf.nn.softmax(x)使得输出符合概率分布
assign_sub(a)更新参数的值,自减a操作
tf.argmax(张量名,axis=操作轴)返回张量沿指定维度最大值的索引。

(1) 常用函数1

tf.cast(张量名,dtype=数据类型): 强制数据类型转换

tf.reduce_min(张量名): 计算张量中的最小值

tf.reduce_max(张量名): 计算张量中的最大值

例子:

x1 = tf.constant([2,4],dtype=tf.float64)
print(x1)
x2 = tf.cast(x1,dtype=tf.int32)
print(x2)
print(tf.reduce_min(x2),tf.reduce_max(x2))

(2)常用函数2

axis:在一个二维张量中,可以通过调整axis等于0或者1,控制执行维度。
axis=0表示按列操作,axie=1表示按行操作。

reduce_mean(张量名, axis=操作轴): 计算张量沿指定维度的平均值。如果不指定axis,则计算全部值的平均值,下同。

reduce_sum(张量名, axis=操作轴):计算张量沿指定维度的和。

例子:

x = tf.constant([[1,2,3],
                [3,4,5]])
print(tf.reduce_mean(x))
print(tf.reduce_sum(x, axis=0))

输出:
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor([4 6 8], shape=(3,), dtype=int32)

(3)常用函数3

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

tf.Variable(初始值)

w = tf.Variable(tf.random.normal([2,2]))

(4)常用函数4

加减乘除:tf.add, tf.subtract, tf.multiply, tf.divide   #只有维度相同的张量才可以加减乘除
平方,次方,开方: tf.square, tf.pow, tf.sqrt
矩阵乘:tf.matmul

例子1:

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)

例子2:

b = tf.fill([1,2],3.)     # 生成一个一行两列的二维张量
print(b)
print(tf.square(b))  # 平方
print(tf.pow(b,3))   # 3次方
print(tf.sqrt(b))    # 开方

输出:
tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32)
tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32)
tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32)
tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)

例子3:

b = tf.fill([3,2],3.)
c = tf.ones([2,3])
d = tf.matmul(b,c)
print(d)

输出:
tf.Tensor(
[[6. 6. 6.]
 [6. 6. 6.]
 [6. 6. 6.]], shape=(3, 3), dtype=float32)

(5)常用函数5

将数据和标签配对:

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

(Numpy和Temsor格式都适用)

例子:

features = tf.constant([12,5,6,8])
labels = tf.constant([1,0,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: id=9, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=5>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=6>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=8>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)

(6)常用函数6

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)           # 计算loss对w的导数计算
print(grad)

输出:
tf.Tensor(6.0, shape=(), dtype=float32)

(7)常用函数7

enumerate: 可以遍历每个元素(列表、元组或字符串),组合为:索引 元素,常在for循环中使用。

例子:

seq = ['one','two']
for i,element in enumerate(seq):
    print(i,element)
    
输出:
0 one
1 two
seq = ['one','two']
for element in seq:
    print(element)
    
输出:
one
two

(8)常用函数8

在分类问题中,常使用独热码作标签,可以表示出每个分类的概率。标记类别: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)

(9)常用函数9

tf.nn.softmax(x):使得输出符合概率分布。

y = tf.constant([1.01, -0.5, 2])
y_pro = tf.nn.softmax(y)
print(y_pro)

输出:
tf.Tensor([0.25561428 0.05646774 0.68791795], shape=(3,), dtype=float32)

(10)常用函数10

assign_sub(a):更新参数的值并返回,对参数做自减a操作。注意:在调用之前,应该先用tf.Variable定义变量为可训练(可更新)。

w = tf.Variable(4)
w.assign_sub(1)
print(w)

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

(11)常用函数11

tf.argmax(张量名,axis=操作轴):返回张量沿指定维度最大值的索引。

axis=0,沿着纵轴;axis=1,沿着横轴。

import numpy as np
test = np.array([[1,2,3],
                [2,3,4],
                [4,1,6]])
print(tf.argmax(test,axis=0))

输出:
tf.Tensor([2 1 2], shape=(3,), dtype=int64)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值