pytorch积少成多

torch.nn.Softmax(-1)

X = torch.randn(3,2)
Y = nn.Softmax(dim = -1)(X)
print(X)
print('---')
print(Y)

tensor([[1.6717, 0.1819],
        [1.3746, 1.0038],
        [0.0052, 0.3082]])
---
tensor([[0.8161, 0.1839],
        [0.5917, 0.4083],
        [0.4248, 0.5752]])

可以看到通过dim可以对矩阵的某个维度求softmax,dim=-1表示最后一个维度,即对每一行求。

torch.cat()函数

C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)
C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)

import torch
>>> A=torch.ones(2,3)    #2x3的张量(矩阵)                                     
>>> A
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
>>> B=2*torch.ones(4,3)  #4x3的张量(矩阵)                                    
>>> B
tensor([[ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.],
        [ 2.,  2.,  2.]])
2x3和4x3按照第0个维度拼(2x3的2 + 4x3的4 = 6) 所以结果是6x3
>>> C=torch.cat((A,B),0)  #按维数0(行)拼接 
>>> C
tensor([[ 1.,  1.,  1.],
         [ 1.,  1.,  1.],
         [ 2.,  2.,  2.],
         [ 2.,  2.,  2.],
         [ 2.,  2.,  2.],
         [ 2.,  2.,  2.]])
>>> C.size()
torch.Size([6, 3])
>>> D=2*torch.ones(2,4) #2x4的张量(矩阵)
>>> C=torch.cat((A,D),1)#按维数1(列)拼接
>>> C
tensor([[ 1.,  1.,  1.,  2.,  2.,  2.,  2.],
        [ 1.,  1.,  1.,  2.,  2.,  2.,  2.]])
>>> C.size()
torch.Size([2, 7])

torch.repeat()函数

相当于把某个整体,在某个方向广播。

import torch
x = torch.tensor([1, 2, 3])
print(x.repeat(4, 1))
print("###################################")
print(x.repeat(4, 2, 1))
print("###################################")
print(x.repeat(4, 1, 2))

output:
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
###################################
tensor([[[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]]])
###################################
tensor([[[1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3]]])

torch.bmm()函数

torch.bmm(input, mat2, out=None) → Tensor

torch.bmm()是tensor中的一个相乘操作,类似于矩阵中的A*B。
input,mat2:两个要进行相乘的tensor结构,两者必须是3D维度的,每个维度中的大小是相同的。
并且相乘的两个矩阵,要满足一定的维度要求:input(p,m,n) * mat2(p,n,a) ->output(p,m,a)。这个要求,可以类比于矩阵相乘。前一个矩阵的列等于后面矩阵的行才可以相乘。

nn.Sequential()函数

# 写法一
net = nn.Sequential(
    nn.Linear(num_inputs, 1)
    # 此处还可以传入其他层
    )

# 写法二
net = nn.Sequential()
net.add_module('linear', nn.Linear(num_inputs, 1))
# net.add_module ......

# 写法三
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
          ('linear', nn.Linear(num_inputs, 1))
          # ......
        ]))

方式一:
这是一个有顺序的容器,将特定神经网络模块按照在传入构造器的顺序依次被添加到计算图中执行。
方式二:
也可以利用add_module函数将特定的神经网络模块插入到计算图中。add_module函数是神经网络模块的基础类(torch.nn.Module)中的方法,如下描述所示用于将子模块添加到现有模块中。
方式三:
也可以将以特定神经网络模块为元素的有序字典(OrderedDict)为参数传入。

net.parameter()

这个函数理解为类型转换函数,将一个不可训练的类型 Tensor 转换成可以训练的类型 parameter 并将这个 parameter 绑定到这个module 里面(net.parameter() 中就有这个绑定的 parameter,所以在参数优化的时候可以进行优化),所以经过类型转换这个变量就变成了模型的一部分,成为了模型中根据训练可以改动的参数。使用这个函数的目的也是想让某些变量在学习的过程中不断的修改其值以达到最优化。

class NN_Network(nn.Module):
    def __init__(self, in_dim, hid, out_dim):
        super(NN_Network, self).__init__()
        self.linear1 = nn.Linear(in_dim, hid)
        self.linear2 = nn.Linear(hid, out_dim)

        self.linear1.weight = torch.nn.Parameter(torch.zeros(in_dim,hid))
        self.linear1.bias = torch.nn.Parameter(torch.ones(hid))
        self.linear2.weight = torch.nn.Parameter(torch.zeros(hid,out_dim))
        self.linear2.bias = torch.nn.Parameter(torch.ones(out_dim))

    def forward(self, input_array):
        h = self.linear1(input_array)
        y_pred = self.linear2(h)
        return y_pred

你可能感兴趣的:(pytorch,深度学习)