pytorch入门(3)

语言模型

lstm

import torchtext
from torchtext.vocab import Vectors
import torch
import numpy as np
import random
BATCH_SIZE = 32
EMBEDDING_SIZE = 100
MAX_VOCAB_SIZE = 5000

import torch.nn as nn
class RNNModel(nn.Module):
    def __init__(self, rnn_type,vocab_size, embed_size,hidden_size)
        super (RNNModel,self).__init__()
        self.embed = nn.Embedding(vocab_size,embed_size)
        self.lstm = nn.LSTM(embed_size,hidden_size)
        self.decoder = nn.Linear(hidden_size, vocab_size)
    def forward(self,text,hidden):
        #text: seq_length * batch_size
        emb = self.embed(text)
        output,hidden = self.lstm(emb,hidden)
        #output: seq_len * batch_size * hidden_size
        #hidden: 1*batch_szie*hidden_size , 1*betch_Size*hidden*size
        output = output.view(-1,out.shape[2]) #把前两个维度拼到一起
        decoded = self.decoder(output.view(-1,output.shape[2]))
        return out_vocab,hidden
    def init_hidden(self,bsz,requires_grad = True):
        weight = next(self.parameters())
        return (weight.new_zeros((1,bsz,self.hidden_Size),requires_grad = True))
model = RNNModel()

loss_fn = nn.CrossEntropyLoss()
learning_rate =
optimizer = torch.optim.Adam(model.parameters(),lr = learning_rate)
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer,0.5)   #降learning rate 方法

for epoch in range(NUM_EPOCHs):
    model.train()
    it = iter(train_iter)
    hidden = model.init_hidden(BATCH_SIZE)
    for i , batch in enumerate(it):
        loss = loss_fn()
        optimizer.zero_grad()
        loss.backward()
        torch.nn.utils.clip_grad_norm(model.parameters().)
        optimizer.step()
    if i % 10000:
        torch.save(model.state_dict(),"..")#存参数
        if #lr不降。。。
        scheduler.step()


CNN

import torch
import torch.nn as nn

#text: 二维matrix 每个单词都有个embad vector

class CNN(nn.Module):
    def __init__(self,vocab_size,embedding_size,output_size,pad_idx,num_filter,filter_size,dropout):
        super(WordAVGModel,self).__init__()
        self.embed = nn.Embedding()
        self.convs = nn.ModuleList([    ])#存在model里,当成para传下去
        self.conv = nn.Conv2d()
        self.linear = nn.Linear()
        self.dropout = nn.Dropout(dropout)
    def forward(self,text):
        text = text.permute(1, 0, 2)  # permutation重新排列
        conved = F.relu(seld.conv(embedded)) #batch*filter*seq_len*1
        conbed = conved.squeeze()            #batch*filter*seq_len
        polled = F.max_pool1d(pooled,pooled.shape[2])
        pooled = pooled.sequeeze(2)#压扁第二位
        pooled = sled.dropout(pooled)

        return self.linear(pooled)

embad = embed.unsqueeze(1)  #在第一维加*1的维度,ie.a*b->a*1*b     unsqueeze/squeeze 可以变换维度

model.load_state_dict(torch.load("word-model.pth")) #下载以前的模型

model = CNN(vocab_size = VOCSB_SIZE,
            embedding_size = EMBEDDING_SIZE,
            ,
            ,
            num_filter = 10,
            filter_size = 3
            dropout = 0.5
                )# def __init__(self,vocab_size,embedding_size,output_size,pad_idx,num_filter,filter_size,dropout):

import torch
import torch.nn as nn
import numpy as np

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(1,20,5,1)
        self.conv2 = nn.Conv2d(20,50,5,1)
        self.fc1 = nn.Linear(4*4*50,500)
        self.fc2 = nn.Linear(500,10)
    def forward(self,x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x,2,2)
        x = F.relu(delf.conv2(x))
        x = F.max_pool2d(x,2,2)
        x = x.view(-1,4*4*50)
        x = F.relu(self.fc1(x))
        x = self.fc(x)
        return F.log_softmax(x,dim=1)  #log probability

你可能感兴趣的:(pytorch,phd)