资源简介

深度学习入门之Pytorch(带书签和源码)

资源截图

代码片段和文件信息

from datetime import datetime

import torch
import torch.nn.functional as F
from torch import nn
from torch.autograd import Variable


def get_acc(output label):
    total = output.shape[0]
    _ pred_label = output.max(1)
    num_correct = (pred_label == label).sum().data[0]
    return num_correct / total


def train(net train_data valid_data num_epochs optimizer criterion):
    if torch.cuda.is_available():
        net = net.cuda()
    prev_time = datetime.now()
    for epoch in range(num_epochs):
        train_loss = 0
        train_acc = 0
        net = net.train()
        for im label in train_data:
            if torch.cuda.is_available():
                im = Variable(im.cuda())  # (bs 3 h w)
                label = Variable(label.cuda())  # (bs h w)
            else:
                im = Variable(im)
                label = Variable(label)
            # forward
            output = net(im)
            loss = criterion(output label)
            # backward
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            train_loss += loss.data[0]
            train_acc += get_acc(output label)

        cur_time = datetime.now()
        h remainder = divmod((cur_time - prev_time).seconds 3600)
        m s = divmod(remainder 60)
        time_str = “Time %02d:%02d:%02d“ % (h m s)
        if valid_data is not None:
            valid_loss = 0
            valid_acc = 0
            net = net.eval()
            for im label in valid_data:
                if torch.cuda.is_available():
                    im = Variable(im.cuda() volatile=True)
                    label = Variable(label.cuda() volatile=True)
                else:
                    im = Variable(im volatile=True)
                    label = Variable(label volatile=True)
                output = net(im)
                loss = criterion(output label)
                valid_loss += loss.data[0]
                valid_acc += get_acc(output label)
            epoch_str = (
                “Epoch %d. Train Loss: %f Train Acc: %f Valid Loss: %f Valid Acc: %f “
                % (epoch train_loss / len(train_data)
                   train_acc / len(train_data) valid_loss / len(valid_data)
                   valid_acc / len(valid_data)))
        else:
            epoch_str = (“Epoch %d. Train Loss: %f Train Acc: %f “ %
                         (epoch train_loss / len(train_data)
                          train_acc / len(train_data)))
        prev_time = cur_time
        print(epoch_str + time_str)


def conv3x3(in_channel out_channel stride=1):
    return nn.Conv2d(
        in_channel out_channel 3 stride=stride padding=1 bias=False)


class residual_block(nn.Module):
    def __init__(self in_channel out_channel same_shape=True):
        super(residual_block self).__init__()
        self.same_shape = same_shape
        stride 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-04-29 22:39  code-of-learn-deep-learning-with-pytorch\
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\
     文件          25  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.gitignore
     文件         337  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\config
     文件          73  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\description
     文件          23  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\HEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\hooks\
     文件         478  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\applypatch-msg.sample
     文件         896  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\commit-msg.sample
     文件        3327  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\fsmonitor-watchman.sample
     文件         189  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\post-update.sample
     文件         424  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\pre-applypatch.sample
     文件        1642  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\pre-commit.sample
     文件        1348  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\pre-push.sample
     文件        4898  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\pre-rebase.sample
     文件         544  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\pre-receive.sample
     文件        1492  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\prepare-commit-msg.sample
     文件        3610  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\hooks\update.sample
     文件       14291  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\index
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\info\
     文件         240  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch\.git\info\exclude
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\HEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\heads\
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\heads\master
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\remotes\
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\remotes\origin\
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\logs\refs\remotes\origin\HEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\objects\
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch\.git\objects\info\
............此处省略162个文件信息

评论

共有 条评论