how to use pytorch
1.Tensor
we can create a tensor just like creating a matrix the default type of a tensor is float
import torch as t
a = t.Tensor([[1,2],[3,4],[5,6]])
a
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
we can also change the datatype of a tensor
b = t.LongTensor([[1,2],[3,4],[5,6]])
b
tensor([[1, 2],
[3, 4],
[5, 6]])
we can also create a tensor filled with zero or random values
c = t.zeros((3,2))
d = t.randn((3,2))
print(c)
print(d)
tensor([[0., 0.],
[0., 0.],
[0., 0.]])
tensor([[ 1.2880, -0.1640],
[-0.2654, 0.7187],
[-0.3156, 0.4489]])
we can change the value in a tensor we’ve created
a[0,1] = 100
a
tensor([[ 1., 100.],
[ 3., 4.],
[ 5., 6.]])
numpy and tensor can transfer from each other
import numpy as np
e = np.array([[1,2],[3,4],[5,6]])
torch_e = t.from_numpy(e)
torch_e
tensor([[1, 2],
[3, 4],
[5, 6]])
2.Variable
Variable consists of data, grad, and grad_fn
data为Tensor中的数值
grad是反向传播梯度
grad_fn是得到该Variable的操作 例如加减乘除
from torch.autograd import Variable
x = Variable(t.Tensor([1]),requires_grad = True)
w = Variable(t.Tensor([2]),requires_grad = True)
b = Variable(t.Tensor([3]),requires_grad = True)
y = w*x+b
y.backward()
print(x.grad)
print(w.grad)
print(b.grad)
tensor([2.])
tensor([1.])
tensor([1.])
we can also calculate the grad of a matrix
x = t.randn(3)
x = Variable(x,requires_grad=True)
y = x*2
print(y)
y.backward(t.FloatTensor([1,1,1]))
print(x.grad)
tensor([-2.4801, 0.6291, -0.4250], grad_fn=<MulBackward>)
tensor([2., 2., 2.])
3.dataset
you can define the function len and getitem to write your own dataset
import pandas as pd
from torch.utils.data import Dataset
class myDataset(Dataset):
def __init__(self, csv_file, txt_file, root_dir, other_file):
self.csv_data = pd.read_csv(csv_file)
with open(txt_file, 'r') as f:
data_list = f.readlines()
self.txt_data = data_list
self.root_dir = root_dir
def __len__(self):
return len(self.csv_data)
def __getitem(self,idx):
data = (self.csv_data[idx],self.txt_data[idx])
return data
4.nn.Module
from torch import nn
class net_name(nn.Module):
def __init(self,other_arguments):
super(net_name, self).__init__()
def forward(self,x):
x = self.convl(x)
return x
5.Optim
1.一阶优化算法
常见的是梯度下降法\(\theta = \theta-\eta\times \frac{\partial J(\theta)}{\partial\theta}\)
2.二阶优化算法
Hessian法