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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章