pytorch学习

1)pytorch tensor初始化方法:

(1)使用empty构造空tensor

x = torch.empty(5,3)

(2)使用rand构造均匀分布的tensor,使用randn构造标准分布的tensor

x = torch.rand(5,3)

(3)使用zeros构造全0 tensor,ones构造全1 tensor

x = torch.zeros((5, 3), dtype=torch.double)
x = torch.ones((5, 3), dtype=torch.float64)

通过dtype参数设置数据类型。当前支持的类型有torch.double,torch.float,torch.long。

(4)使用Tensor函数构造tensor

x = torch.Tensor(2, 3) #构造全0 tensor#
x = torch.Tensor([[1, 2, 3], [4, 5, 6]]) #通过list构造tensor#

将tensor转化为list

a = x.tolist()

注意此处一定要使用赋值

(5)使用已有tensor构造新tensor,新tensor保留原维度,同时使用新的属性,例如数据类型

x = torch.zeros(5, 3, dtype=torch.long)
x = x.new_ones(5, 3, dtype=torch.double)
x = torch.randn_like(x, dtype=torch.float)

2)获取pytorch tensor尺寸方法

x.size()

3)pytorch tensor维度相关调整方法

(1)view方法可以调整tensor的维度,但必须保证调整前后的元素总数一致,同时view不会改变当前tensor的维度,而是通过返回值获得新的维度(pytorch 1.0)

a = torch.arange(0, 6)
x = a.view(2, 3)

(2)使用unsqueeze扩充维度,使用sequeeze压缩维度,同样使用返回值获得新tensor

 x = x.unsqueeze(0)
 x = x.squeeze(0)

(3)使用resize_调整tensor的维度,resize_与view的不同之处在于resize_可以扩充tensor的大小,同时resize_是原地操作。

a.resize_(3, 3)

4)pytorch tensor与numpy ndarray互转

(1)tensor转ndarray,转化后tensor与ndarray共享相同的内存。

    a = torch.ones(5)
    b = a.numpy()
    a.add_(1)

(2)ndarray转tensor,转化后tensor与ndarray同样共享相同的内存。

    a = np.ones(5)
    b = torch.from_numpy(a)
    np.add(a, 1, out=a)

5)pytorch 使用parameters()查看网络参数

    params = list(net.parameters())
    for param in params:
        print(param)

使用named_parameters()分别获取网络参数的名称与参数大小

    for name, parameter in net.named_parameters():
        print(name, ":", parameter.size())

6)pytorch 反向传播

(1)在pytorch中只有variable具有自动求导功能,variable由三部分组成,分别是data表示参数,grad表示data对应的梯度,grad_fn表示反向传播过程中计算梯度的函数。

(2)grad在计算过程是累加的,因此在反向传播之前需要调用zero_grad()把梯度清零。

(3)conv2d的输入必须是四维的,如果不是四维的,则需要先调用unsqueeze(0)扩展维度。

神经网络定义如下:

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

        self.max_pool2d = F.max_pool2d

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = self.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = self.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

正向传播过程代码如下:

    net = Net()
    input = torch.randn(1, 1, 32, 32)
    output = net(input)

    target = torch.randn(10)  # a dummy target, for example
    target = target.unsqueeze(0)
    criterion = nn.MSELoss()

    loss = criterion(output, target)
    print(loss)

反向传播过程代码如下:

    net.zero_grad()
    loss.backward()

手动参数更新,使用SGD算法:

    learning_rate = 0.01
    for name, parameters in net.named_parameters():
        print(name, " before:", parameters.data.data)
        parameters.data.sub_(parameters.grad.data * learning_rate)
        print(name, " after:", parameters.data.data)

使用已有参数更新方法:

import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值