PyTorch in Action: A Step by Step Tutorial

PyTorch in Action: A Step by Step Tutorial

Installation Guide

Step 1, donwload the Miniconda and installing it on your computer.python

The reason why explain installing conda is that some of classmates don`t have a conda environment on their computer.web

https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/bash

Step 2, create a conda virtual envriommentapp

In this ariticle, we assume that there is a CPU version of PyTorch is going to be installed. To specifically distinguish CPU version and GPU version, we`re going to create a virtual environment named "PyTorch-CPU".dom

In the Conda Prompt run the following commands:ide

conda create -n PyTorch-CPU pip

Step 3, install PyTorchui

On the website of PyTorch(https://pytorch.org/), there is a guidance on the page. To chose the most appropriate options(e.g. as the follow figure).this

In the Conda Prompt run the following commands:spa

activate PyTorh-CPU
conda install pytorch-cpu torchvision-cpu -c pytorch

Congratulations, installation of PyTorch is complete!code

Data Processing

Before we start ours building. We have to access the dataset and clean it.

Here we have accessed 西瓜數據集3.0. And we convert the character-described features to numeric.

# encoding:utf8
# 西瓜3.0 數據集

waterMelons = [
    # 1
    ['青綠', '蜷縮', '濁響', '清晰', '凹陷', '硬滑', '好瓜'],
    # 2
    ['烏黑', '蜷縮', '沉悶', '清晰', '凹陷', '硬滑', '好瓜'],
    # 3
    ['烏黑', '蜷縮', '濁響', '清晰', '凹陷', '硬滑', '好瓜'],
    # 4
    ['青綠', '蜷縮', '沉悶', '清晰', '凹陷', '硬滑', '好瓜'],
    # 5
    ['淺白', '蜷縮', '濁響', '清晰', '凹陷', '硬滑', '好瓜'],
    # 6
    ['青綠', '稍蜷', '濁響', '清晰', '稍凹', '軟粘', '好瓜'],
    # 7
    ['烏黑', '稍蜷', '濁響', '稍糊', '稍凹', '軟粘', '好瓜'],
    # 8
    ['烏黑', '稍蜷', '濁響', '清晰', '稍凹', '硬滑', '好瓜'],
    # 9
    ['烏黑', '稍蜷', '沉悶', '稍糊', '稍凹', '硬滑', '壞瓜'],
    # 10
    ['青綠', '硬挺', '清脆', '清晰', '平坦', '軟粘', '壞瓜'],
    # 11
    ['淺白', '硬挺', '清脆', '模糊', '平坦', '硬滑', '壞瓜'],
    # 12
    ['淺白', '蜷縮', '濁響', '模糊', '平坦', '軟粘', '壞瓜'],
    # 13
    ['青綠', '稍蜷', '濁響', '稍糊', '凹陷', '硬滑', '壞瓜'],
    # 14
    ['淺白', '稍蜷', '沉悶', '稍糊', '凹陷', '硬滑', '壞瓜'],
    # 15
    ['烏黑', '稍蜷', '濁響', '清晰', '稍凹', '軟粘', '壞瓜'],
    # 16
    ['淺白', '蜷縮', '濁響', '模糊', '平坦', '硬滑', '壞瓜'],
    # 17
    ['青綠', '蜷縮', '沉悶', '稍糊', '稍凹', '硬滑', '壞瓜']
]

features = list() # [[青綠, 烏黑, 淺白], [蜷縮, 硬挺...], ...]


def numeric(data):
    l = list()
    for i,s in enumerate(data):
        val = features[i].index(s)
        l.append(val)
    return l


if __name__ == '__main__':
    for melon in waterMelons:
        for i, feature in enumerate(melon):
            try:
                if feature not in features[i]:
                    features[i].append(feature)
            except IndexError:
                features.append([feature])

    f = open('data/WaterMelon.txt', encoding='utf8', mode='w')
    for melon in waterMelons:
        val = numeric(melon)
        f.write("%s\n" % val)

Your first Neural Network with PyTorch

Here we implement a neural network with input layer and log softmax layer.

There are 12 parameters need to be trained:
\[ input \times hiddens \times output = parameters\\ 6 \times 2 = 12 \]

# encoding:utf8

import torch
from sklearn.model_selection import train_test_split


class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.layer = torch.nn.Linear(6, 2)
        self.softmax = torch.nn.LogSoftmax(dim=1)

    def forward(self, x):
        out = self.layer(x)
        out = self.softmax(out)
        return out


if __name__ == '__main__':
    x, y = list(), list()
    with open('data/WaterMelon.txt', encoding='utf8') as f:
        for line in f:
            l = eval(line.strip())
            x.append(l[:-1])
            y.append(l[-1])

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33)
    x_train, x_test, y_train, y_test = torch.Tensor(x_train), torch.Tensor(x_test), torch.Tensor(y_train).long(), torch.Tensor(y_test).long()

    model = Model()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
    criticism = torch.nn.CrossEntropyLoss()

    # train
    for epoch in range(500):
        out = model(x_train)
        loss = criticism(out, y_train)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    # test
    y_pred = model(x_test)
    _, predicted = torch.max(y_pred, 1)
    acc = torch.sum(y_test == predicted ).numpy() / len(x_test)
    print(acc)

We got the accuracy 0.8, sometimes we got 1.

LOL!

相關文章
相關標籤/搜索