栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch基本操作(对比tensorflow 2)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch基本操作(对比tensorflow 2)

1.pytorch基本操作

无论是pytorch还是tensorflow 2,基本操作均与numpy相同

(1)创建张量

pytorch:

torch.empty(5,3)

tensor([[7.9604e-11, 4.2179e-43, 7.9606e-11],
        [4.2179e-43, 7.9598e-11, 4.2179e-43],
        [7.9581e-11, 4.2179e-43, 7.9608e-11],
        [4.2179e-43, 7.9601e-11, 4.2179e-43],
        [7.9590e-11, 4.2179e-43, 7.9608e-11]])

torch.rand(5,3)   # 随机值

tensor([[0.9154, 0.4700, 0.2335],
        [0.9463, 0.8901, 0.2002],
        [0.4432, 0.0426, 0.9394],
        [0.5706, 0.2271, 0.0835],
        [0.6255, 0.9812, 0.0346]])

torch.tensor([5,3])        #传入值

tensor([5, 3])

tensorflow 2

tf.constant([[1., 2., 3.], [4., 5., 6.]]) # matrix

(2)基本计算方法:

tf.add()/torch.add()

tensorflow 2与numpy有些函数名称上有些差异,但pytorch更接近numpy

(3)view操作改变矩阵维度

x=torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)
print(x.size(),y.size(),z.size())

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
(4) Numpy协同计算

pytorch:

import numpy as np
a = torch.ones(1,5)
a.numpy()

array([[1., 1., 1., 1., 1.]], dtype=float32)

 a = np.ones(5)
torch.from_numpy(a)

tensor([1., 1., 1., 1., 1.], dtype=torch.float64)

Tensorflow 2:

a = np.array([2., 4., 5.])
tf.constant(a)

t.numpy()

array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)

np.array(t)

array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)

tf.square(a)

np.square(t)

array([[ 1.,  4.,  9.],
       [16., 25., 36.]], dtype=float32)
2.自动求导机制 

b = torch.randn(1,requires_grad=True) # 可以求导
x=torch.rand(1,requires_grad=True)
w=torch.rand(1,requires_grad=True)
y = w*x
z = y+b
z.backward()
b.grad,x.grad

(tensor([1.]), tensor([0.8892]))

y.requires_grad,x.requires_grad,b.requires_grad,w.requires_grad,z.requires_grad

(True, True, True, True, True)

反向传播:

z.backward(retain_graph=True) # 如果不清空就会累加
w.grad

tensor([0.6140])

b.grad

tensor([2.])

链式法则:

 

 

 def f(w1, w2):
    return 3 * w1 ** 2 + 2 * w1 * w2

w1, w2 = tf.Variable(5.), tf.Variable(3.)
with tf.GradientTape() as tape:
    z = f(w1, w2)

gradients = tape.gradient(z, [w1, w2])

[,
 ]

# 二阶求导

with tf.GradientTape(persistent=True) as hessian_tape: # persistent = True :tpye具有持久性
    with tf.GradientTape() as jacobian_tape:
        z = f(w1, w2)
    jacobians = jacobian_tape.gradient(z, [w1, w2])
hessians = [hessian_tape.gradient(jacobian, [w1, w2])
            for jacobian in jacobians]
del hessian_tape 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号