美文网首页
PyTorch 笔记

PyTorch 笔记

作者: AntiGravity | 来源:发表于2020-05-10 13:27 被阅读0次

    tensor

    import torch
    import numpy as np
    
    my_tensor=torch.tensor([[0,1],[1,0]])# 新建
    my_tensor.float()# 改数据类型
    
    np_array=np.array([[0,1],[0.1,0.2]])
    t_f_n=torch.tensor(np_array)# numpy转tensor
    # t_f_n=t_f_n.to(torch.float32) # 双精度转单精度
    t_f_n=t_f_n.float()# 同上
    t_f_n.numpy() # tensor转numpy
    
    #转存cuda
    device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    my_tensor.to(device=device)
    
    torch.tensor([1]).item() # 单变量tensor转标量
    
    #设置梯度下降
    t_w_g=torch.tensor([[0,1],[0.1,0.2]],requires_grad=True)
    result=(t_w_g.pow(2)+t_w_g).sum() #result是关于t_w_g的函数,其值是个标量2.35
    result.backward() # 相当于对t_w_g求导
    
    t_w_g.detach_()# 从计算图分离
    

    einsum

    x=torch.randn(5)
    y=torch.randn(5)
    
    # 外积
    torch.einsum('i,j->ij',x,y)# 等价于如下代码
    a=torch.zeros([5,5]) 
    for i in range(5):
        for j in range(5):
            a[i][j]=x[i]*y[j]
    print(a)
    
    # 内积
    print(torch.einsum('i, i->',x,y))# 等价于如下代码
    a=0
    for i in range(5):
        a+=x[i]*y[i]
    
    x=torch.randn([2,3])
    y=torch.randn([3,4])
    
    # 矩阵乘积
    print(torch.einsum('ij,jk->ik',x,y))# 等价于如下代码
    def mmul(x,y):
        a=torch.zeros([len(x),len(y[0])])
        for i in range(len(x)):
            for k in range(len(y[0])):
                for j in range(len(y)):
                    a[i][k]+=x[i][j]*y[j][k]
        return a
    mmul(x,y)
    

    知识补充:
    requires_grad
    einsum

    自定义数据集

    import torch
    from torch.utils.data import DataLoader, Dataset
    
    class MyDataset(Dataset):
        def __init__(self):
            super(MyDataset, self).__init__()
            self.data=torch.randn(1024,10,10)
        def __len__(self):
            return 1024
        def __getitem__(self,idx):
            return self.data[idx, :, :]
        
    md=MyDataset()
    mdl=DataLoader(md, batch_size=64, shuffle=True)
    for batch in mdl:
        print(batch.shape)
    print(md.__len__())
    
    class MyDictDataset(Dataset):# 字典类型
        def __init__(self):
            super(MyDictDataset, self).__init__()
            self.x=torch.randn(1024,10)
            self.y=torch.rand(1042)
        def __len__(self):
            return 1024
        def __getitem__(self,idx):
            return {'x':self.x[idx,:],'y':self.y[idx]}
    
    mdd=MyDictDataset()
    mddl=DataLoader(mdd, batch_size=64, shuffle=True)
    for batch in mddl:
        print(batch['y'])
    print(mdd.__len__())
    
    from torch.utils.data import TensorDataset
    x=torch.randn(10, 100)
    y=torch.randn(10)
    td=TensorDataset(x,y) # 使用TensorDataset
    for i in td:
        print(len(i[0]), i[1])
    print(td.tensors)
    

    自定义网络

    import torch
    import torch.nn as nn
    
    x=torch.randn([8,100,10]).detach() # 8个观测(句子或文档),10个词,100维
    print('x:',x.shape,'\n',x)
    y=torch.rand(8)
    y=(y>0.5).int()
    print('y:',y)
    #多层感知器
    class MLP(nn.Module):
        def __init__(self):
            super(MLP, self).__init__()
            self.first_layer=nn.Linear(1000,50)
            self.second_layer=nn.Linear(50,1)#二分类,最终输出概率,所以是1维。多分类则输出多维
            
        def forward(self,x):
            x=torch.flatten(x, start_dim=1, end_dim=2)
            print('flatten:',x.shape, '\n', x)
            x=nn.functional.relu(self.first_layer(x))
            print('relu:',x.shape, '\n', x)
            x=self.second_layer(x)
            return x
    
    mlp=MLP()
    mlp(x)
    # 词嵌入
    class Embedding(nn.Module):
        def __init__(self):
            super(Embedding, self).__init__()
            self.embedding=nn.Embedding(4,100)# 4个词汇,100维
        def forward(self,x):
            return self.embedding(x)
        
    embd=Embedding()
    em_in=torch.tensor([[0,1,0],[2,3,2]])# 2*3为seq_len*batch_size
    em_out=embd(em_in)
    em_out.shape # [seq_len, batch_size, embedding](2, 3, 100)
    
    # LSTM
    class LSTM(nn.Module):
        def __init__(self):
            super(LSTM, self).__init__()
            self.lstm=nn.LSTM(10,15,num_layers=2,bidirectional=True, dropout=0.1)# 10,特征维度 15,隐藏层维度 2:2层LSTM
        def forward(self, x):
            output, (hidden, cell)=self.lstm(x)
            return output, hidden, cell
        
    permute_x=x.permute([1,0,2]) # LSTM的input.shape=(seq_len, batch, input_size(特征维度))
    lstm=LSTM()
    o, h, c=lstm(permute_x)#输入维度是(100, 8, 10)代表每文档100单词,8个文档,10维度的词向量
    print(o.shape,h.shape,c.shape)
    # o.shape=(seq_len, batch, num_directions * hidden_size)(100, 8, 30)
    # h.shape=(num_layers * num_directions, batch, hidden_size)(4, 8, 15)
    # c.shape=h.shape(4, 8, 15)
    
    # 卷积
    class Conv(nn.Module):
        def __init__(self):
            super(Conv, self).__init__()
            self.conv1d=nn.Conv1d(100,50,2)#卷积核
        def forward(self,x):
            return self.conv1d(x)
        
    conv=Conv()
    output=conv(x)
    output.shape
    

    pytorch的embedding:https://www.cnblogs.com/duye/p/10590146.html

    相关文章

      网友评论

          本文标题:PyTorch 笔记

          本文链接:https://www.haomeiwen.com/subject/pycjnhtx.html