tensorflow-tf基础

本文介绍了使用TensorFlow进行简单梯度下降的演示,包括变量的创建与更新,以及张量的基础操作如数据类型、创建方法、统计函数等。还涉及了tf.GradientTape用于自动微分和计算梯度,以及一些常用的数学函数和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TensorFlow快速入门

使用TF进行计算一个简单的梯度下降演示

import tensorflow as tf

w = tf.Variable(tf.constant(5, dtype=tf.float32))
# 学习率
lr = 0.2

# 迭代次数
epoch = 40

for i in range(epoch):
    with tf.GradientTape() as tape:
        loss = tf.square(w + 1)
    grads=tape.gradient(loss,w)

    # 对w进行自减操作
    w.assign_sub(lr*grads)
    print("After %s epoch, w is %f, loss is %f" % (i,w.numpy(),loss))

TF基础

Tenosor

Tensor即为张量,多维数组(列表)

维数名称举例
0-D0标量 Scalar1,2,3
1-D1向量 vectorv=[1,2,3]
2-D2矩阵 matrixv=[[1,2,3],[4,5,6],[7,8,9]]
n-Dn张量 tensor

TF数据类型

  • int
    • int8 int32…
  • float
    • float32, float64
  • bool
  • string

创建Tensor

tf直接创建

import tensorflow as tf

a=tf.constant([1,5],dtype=tf.int64)
print(a)
print(a.shape)
print(a.dtype)

numpy转化

import tensorflow as tf

a=tf.constant([1,5],dtype=tf.int64)
print(a)
print(a.shape)
print(a.dtype)

输出结果

[0 1 2 3 4]
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)

特殊张量创建

import tensorflow as tf
a = tf.zeros([2, 3])
b = tf.ones([5])
c = tf.fill([3, 4], 5)

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. 1.], shape=(5,), dtype=float32)
tf.Tensor(
[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]], shape=(3, 4), dtype=int32)

随机初始化

  • normal 正态分布的随机数,默认均值为0,标准差为1
  • truncated_normal 截断式正态分布随机数,生成数据在 ( μ − 2 σ , μ + 2 σ ) (\mu-2\sigma,\mu+2\sigma) μ2σ,μ+2σ之内
  • uniform 均匀分布
import tensorflow as tf
# 正态分布
d = tf.random.normal([3,3],mean=10,stddev=1)
print(d)
# 断式正态分布
e=tf.random.truncated_normal([3,3],mean=10,stddev=1)
print(e)
# 均匀分布
f = tf.random.uniform([3,2],minval= 0,maxval=2)
print(f)

输出

tf.Tensor(
[[ 9.77536    8.949589  11.8310585]
 [ 9.836057   9.8400545 10.046381 ]
 [ 9.736849   7.9972515  8.365251 ]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[10.3750925  9.8419485 10.2962885]
 [ 9.347204   9.267727  10.787585 ]
 [ 9.867645   9.521841   9.510477 ]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[1.2572455  0.13802814]
 [1.4926326  0.11922121]
 [1.4667287  1.0912795 ]], shape=(3, 2), dtype=float32)

tf.Variable

tf.Variable创建变量,标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。

示例

import tensorflow as tf

w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))
print(w)
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[-0.63499445, -1.9603122 ],
       [ 0.63590336, -1.2041956 ]], dtype=float32)>

常用计算语法

基础运算

  • 四则运算

    • tf.add
    • tf.subtract
    • tf.multiply
    • tf.divide
  • 平方

    • tf.square
  • 次方

    • tf.power
  • 开放

    • tf.sqrt
  • 矩阵乘法

    • tf.matmul

注意: 只有相同维度的张量才可以做四则运算(对应元素的四则运算)

assign_sub 自更新

w = tf.Variable(tf.constant(2, dtype=tf.int32))
w.assign_sub(1)
print(w)

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

tf.GradientTape

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

with tf.GradientTape() as tape:
    #若干个计算过程...
grad=tape.gradient(函数,对谁求导)

示例:

f ( w ) = w 2 f(w)=w^2 f(w)=w2在w=3时的导数

import tensorflow as tf

with tf.GradientTape() as tape:
    w = tf.Variable(tf.constant(3, dtype=tf.float64))
    loss = tf.pow(w, 2)
grad = tape.gradient(loss, w)
print(grad)

输出

tf.Tensor(6.0, shape=(), dtype=float64)

常用函数

统计函数

  • tf.cast 强制转化类型
  • tf.reduce_min/tf.reduce_max 计算张量维度上的最小值/最大值
  • tf.reduce_meax 计算均值
  • tf.reduce_sum 计算和
import tensorflow as tf

x1=tf.constant([1,2,3],dtype=tf.float64)
print(x1)

x2=tf.cast(x1,tf.int32)
print(x2)

x3=tf.random.uniform([3,2],minval=1,maxval=10,dtype=tf.int32)
print(x3)
print(tf.reduce_min(x3),tf.reduce_max(x3))

输出

tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(
[[7 8]
 [1 7]
 [2 4]], shape=(3, 2), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(8, shape=(), dtype=int32)

注:可以使用axis参数控制执行维度

  • axis=0代表跨行

  • axis=1代表跨列

  • 如果不指定axis,则所有元素参与计算

示例

x3=tf.random.uniform([3,2],minval=1,maxval=10,dtype=tf.int32)
print(x3)
print(tf.reduce_min(x3,axis=0),tf.reduce_max(x3,axis=1))
tf.Tensor(
[[8 7]
 [9 1]
 [9 9]], shape=(3, 2), dtype=int32)
tf.Tensor([8 1], shape=(2,), dtype=int32) tf.Tensor([8 9 9], shape=(3,), dtype=int32)

tf.argmax

返回张量沿指定维度最大值的索引

import tensorflow as tf

a = tf.random.uniform([4,3],0,10)
print(a)
print(tf.argmax(a,axis=0))
print(tf.argmax(a,axis=1))

输出

tf.Tensor(
[[2.8604698  3.8623106  5.3516445 ]
 [9.9638405  0.32884598 1.9902623 ]
 [4.15576    7.0319605  0.4528761 ]
 [9.675342   3.9883995  4.330988  ]], shape=(4, 3), dtype=float32)
tf.Tensor([1 2 0], shape=(3,), dtype=int64)
tf.Tensor([2 0 1 0], shape=(4,), dtype=int64)

tf.where

  • 条件语句为真返回A,为假返回B

    import tensorflow as tf
    
    a = tf.constant([1, 2, 3, 1, 1])
    b = tf.constant([0, 1, 3, 4, 5])
    c = tf.where(tf.greater(a, b), a, b)
    print(c)
    #输出 tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
    

enumerate

python内建函数,可以遍历每个元素,组合为(索引,运算),常在for循环中使用

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

输出

0 one
1 two
2 three

tf.one_hot

独热编码(one-hot encoding),在分类问题中,使用独热编码做标签

import tensorflow as tf

classes = 3
labels = tf.constant([1, 0, 2, 2, 0])
output = tf.one_hot(labels, depth=classes)
print(output)

运行结果

tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]
 [0. 0. 1.]
 [1. 0. 0.]], shape=(5, 3), dtype=float32)

softmax

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gR3aWAky-1673416974043)(Tensorflow2.assets/1671262865825.png)]

使用softmax使输出符合概率分布

import tensorflow as tf
y=tf.constant([1.01,2.01,-0.5],dtype=tf.float64)
y2=tf.nn.softmax(y)
print(y2)

输出

tf.Tensor([0.25385918 0.69006079 0.05608003], shape=(3,), dtype=float64)

np.random.RandomState.rand()

返回一个[0,1)之间的随机数

import numpy as np
rdm=np.random.RandomState(seed=1)
#维度为空,生成一个随机标量
a=rdm.rand() 
b=rdm.rand(2,3)
print(a)
print(b)
0.417022004702574
[[7.20324493e-01 1.14374817e-04 3.02332573e-01]
 [1.46755891e-01 9.23385948e-02 1.86260211e-01]]

np.vstack

将两个数组按垂直方向叠加

a = np.array([1,2,3])
b=np.array([4,5,6])
c=np.vstack((a,b))
print(c)

输出

[[1 2 3]
 [4 5 6]]

np.mgrid/ .ravel/np.c_[]

np.mgrid[起始值:结束值:步长]

x.ravel() 将x变为一维数组

np.c_[] 使返回的间隔数值点配对

import numpy as np
# mgrid输出的两个数组维度相同
x,y=np.mgrid[1:3:1,2:4:0.5]

grid=np.c_[x.ravel(),y.ravel()]
print(x)
print(y)
print(grid)

输出

[[1. 1. 1. 1.]
 [2. 2. 2. 2.]]
[[2.  2.5 3.  3.5]
 [2.  2.5 3.  3.5]]
[[1.  2. ]
 [1.  2.5]
 [1.  3. ]
 [1.  3.5]
 [2.  2. ]
 [2.  2.5]
 [2.  3. ]
 [2.  3.5]]

数据集构建

标签配对

tf.data.Dataset.from_tensor_slices

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

import tensorflow as tf
from sklearn.datasets import load_iris

origin = load_iris()
features = tf.convert_to_tensor(origin.data)
label = tf.convert_to_tensor(origin.target)


data = tf.data.Dataset.from_tensor_slices((features, label))
print(data)
for i in data:
    print(i)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_console_

您的关注与鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值