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


深度学习入门之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.gitconfig
     文件          73  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.gitdescription
     文件          23  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitHEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.githooks
     文件         478  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githooksapplypatch-msg.sample
     文件         896  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookscommit-msg.sample
     文件        3327  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githooksfsmonitor-watchman.sample
     文件         189  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspost-update.sample
     文件         424  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspre-applypatch.sample
     文件        1642  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspre-commit.sample
     文件        1348  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspre-push.sample
     文件        4898  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspre-rebase.sample
     文件         544  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githookspre-receive.sample
     文件        1492  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githooksprepare-commit-msg.sample
     文件        3610  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.githooksupdate.sample
     文件       14291  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitindex
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitinfo
     文件         240  2018-04-29 22:37  code-of-learn-deep-learning-with-pytorch.gitinfoexclude
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogsHEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efs
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efsheads
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efsheadsmaster
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efs
emotes
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efs
emotesorigin
     文件         214  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitlogs
efs
emotesoriginHEAD
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitobjects
     目录           0  2018-04-29 22:38  code-of-learn-deep-learning-with-pytorch.gitobjectsinfo
............此处省略162个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)