torch learning 1

torch tensor

1. tensor的值value

import torch
x = torch.empty(5, 3)
print(x)
#tensor([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]])

x=torch.rand(3,requires_grad=True)#0-1均匀分布
x[0].item() #单一元素的访问
#0.1676112413406372
x[1].item()
#0.17526572942733765
x[2].item()
#0.8866558074951172

y=x*2
print(y) #有梯度属性
#tensor([0.3352, 0.3505, 1.7733], grad_fn=<MulBackward0>) 
y.data #无梯度属性
#tensor([0.3352, 0.3505, 1.7733])

2. L2正则化

y.data.norm()#L2正则化
#tensor(1.8384)

3. 反向传播

while y.data.norm()<100:
y=y*2 
y
#tensor([ 21.4542,  22.4340, 113.4919], grad_fn=<MulBackward0>)
gradients = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)

if your output has multiple values (e.g. loss=[loss1, loss2, loss3]), you can compute the gradients of loss w.r.t. the weights by loss.backward(torch.FloatTensor([1.0, 1.0, 1.0])).
Furthermore, if you want to add weights or importances to different losses, you can use loss.backward(torch.FloatTensor([-0.1, 1.0, 0.0001])).This means to calculate -0.1d(loss1)/dw, d(loss2)/dw, 0.0001d(loss3)/dw simultaneously.

y.backward(gradients)
#反向传播时损失若不为标量,必须传入与loss相同尺寸的gradients
print(x.grad)
#tensor([-1.3827e-01, -1.9381e-01, -6.9277e-05])

import torch 
x=torch.rand(5,5,requires_grad=True)
y=torch.rand(5,5,requires_grad=True)
z=x**2+y**2
z
#tensor([[0.1308, 0.3080, 0.0545, 0.4559, 0.0228],
    [0.0693, 0.5782, 0.2507, 0.0955, 0.3727],
    [0.2338, 0.4374, 0.4471, 0.5643, 0.0601],
    [0.1710, 0.7074, 1.2551, 0.9987, 0.5115],
    [0.2818, 0.3349, 0.4593, 0.4527, 0.4446]], grad_fn=<AddBackward0>)
x
#tensor([[0.0664, 0.4904, 0.2263, 0.5930, 0.0687],
    [0.2494, 0.3224, 0.0788, 0.0229, 0.3640],
    [0.4776, 0.0232, 0.4605, 0.5580, 0.1391],
    [0.4075, 0.8304, 0.5111, 0.6813, 0.5724],
    [0.3774, 0.1795, 0.3388, 0.5798, 0.5371]], requires_grad=True)
z.backward(torch.ones_like(x))
print(x.grad)
#tensor([[0.1328, 0.9808, 0.4525, 1.1860, 0.1374],
    [0.4989, 0.6449, 0.1576, 0.0457, 0.7280],
    [0.9551, 0.0464, 0.9210, 1.1161, 0.2782],
    [0.8150, 1.6607, 1.0222, 1.3627, 1.1448],
    [0.7547, 0.3589, 0.6777, 1.1596, 1.0742]])

4. numpy 与tensor 的转换

a = np.array([1, 0.1, 0.01])
b = torch.from_numpy(a).float()
x = torch.randn(3, requires_grad=True)
y = x * 2
y.backward(b)
x.grad
#tensor([2.0000, 0.2000, 0.0200])
b.numpy()
array([1.  , 0.1 , 0.01], dtype=float32)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值