当前位置:   article > 正文

基于Pytorch的一个encdoer-decoder小模型,麻雀虽小,五脏俱全_pytorch encode-decode

pytorch encode-decode

主要代码,实现了一个简单的encoder-decoder模型,并把模型参数保存为numpy数组

# coding=utf-8
import configparser
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn.functional as F
import numpy as np
import itertools
import matplotlib.pyplot as plt


class SelfDataset(Dataset):
    def __init__(self,filename,norm=True):
        self.filename = filename
        self.norm = norm
        self.data = self.read_data()

    def read_data(self):
        data = []
        with open(self.filename,'r',encoding='utf-8') as f:
            f.readline()
            for line in f.readlines():
                digits = [float(x) for x in line.strip().split(',')]
                data.append(digits)
        data = np.array(data)
        if self.norm:
            data = (data-np.min(data,axis=0))/(np.max(data,axis=0)-np.min(data,axis=0))
        return data

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return len(self.data)


class Model(torch.nn.Module):
    def __init__(self, feature_in,hidden_units,bias=False,weight_path=None,bias_path=None):
        super(Model, self).__init__()
        self.bias = bias
        self.weighs_path = weight_path
        self.bias_path = bias_path
        self.input_layer = torch.nn.Linear(feature_in,hidden_units[0],bias=bias)
        self.encoder = torch.nn.Sequential(*[torch.nn.Linear(unit_in,unit_out,bias=bias) for unit_in,unit_out
                                            in zip(hidden_units[:-1],hidden_units[1:])])
        self.decoder = torch.nn.Sequential(*[torch.nn.Linear(unit_in,unit_out,bias=bias) for unit_in,unit_out
                                            in zip(hidden_units[1::-1],hidden_units[:-1:-1])])
        self.output_layer = torch.nn.Linear(hidden_units[-1],feature_in,bias=bias)

    def forward(self, x):
        x = self.input_layer(x)
        x = F.relu(x)
        x = self.encoder(x)
        x = self.decoder(x)
        return self.output_layer(x)

    def save_weights_as_numpy(self):
        weights = []
        bias = []
        with torch.no_grad():
            for name,module in itertools.chain(zip(["input_layer"], [self.input_layer]), self.encoder.named_children()):
                weights.append(module.weight.cpu().numpy())
                if self.bias:
                    bias.append(module.bias.cpu().numpy())
        if self.weighs_path is not None:
            np.save(self.weighs_path,weights)
        if self.bias and self.bias_path is not None:
            np.save(self.bias_path,bias)


def train(model, device, train_loader, criterion, optimizer, epochs=10):
    model.train()
    for epoch in range(epochs):
        for batch_idx, x in enumerate(train_loader):
            x = x.to(torch.float32)
            x = x.to(device)
            optimizer.zero_grad()
            output = model(x)
            loss = criterion(x, output)
            loss.backward()
            optimizer.step()
            if batch_idx % 100 == 0:
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(x), len(train_loader.dataset),
                           100. * batch_idx / len(train_loader), loss.item()))


if __name__ == '__main__':
    parser = configparser.ConfigParser()
    parser.read('config.ini',encoding='utf-8')
    feature_in = parser.getint('net','feature_in')
    hidden_units = [int(x) for x in parser.get('net','hidden_layers_units').split(',')]
    biased = parser.getboolean('net','bias')
    epochs = parser.getint('train','epochs')
    batch_size = parser.getint('train','batch_size')
    learning_rate = parser.getfloat('train','learning_rate')
    weight_path = parser.get('path','weight_path')
    data_path = parser.get('path','data_path')
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    data_set = SelfDataset(data_path,True)
    data_loader = DataLoader(data_set,batch_size=batch_size,shuffle=True)

    model = Model(feature_in,hidden_units,biased,weight_path=weight_path).to(device)
    criterion = torch.nn.MSELoss()
    optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)

    train(model,device,data_loader,criterion,optimizer,epochs)
    model.save_weights_as_numpy()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

配置文件

;保存的文件路径
[path]
data_path = C:/Users/xia/PycharmProjects/pytorch_demo/encoder/data/feature.csv
weight_path = weight.npy
bias_path = bias.npy
;第一参数,是否需要偏置,第二个是输入向量大小,第三个是decoder-encoder每层的节点数,比如下面,能获得9x4和4x2的两个矩阵
[net]
bias = False
feature_in = 9
hidden_layers_units = 4,2

;训练参数
[train]
batch_size = 32
epochs = 100
learning_rate = 0.01
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/852353
推荐阅读
相关标签
  

闽ICP备14008679号