• 大小:
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2022-02-04
  • 语言: 其他
  • 标签: 深度学习  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 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        337  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\config

     文件         73  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\description

     文件         23  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\HEAD

     文件        478  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\applypatch-msg.sample

     文件        896  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\commit-msg.sample

     文件       3327  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\fsmonitor-watchman.sample

     文件        189  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\post-update.sample

     文件        424  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\pre-applypatch.sample

     文件       1642  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\pre-commit.sample

     文件       1348  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\pre-push.sample

     文件       4898  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\pre-rebase.sample

     文件        544  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\pre-receive.sample

     文件       1492  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\prepare-commit-msg.sample

     文件       3610  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\hooks\update.sample

     文件      14291  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\index

     文件        240  2018-04-29 22:37  深度学习入门之Pytorch源码\.git\info\exclude

     文件        214  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\logs\HEAD

     文件        214  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\logs\refs\heads\master

     文件        214  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\logs\refs\remotes\origin\HEAD

     文件      18992  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\objects\pack\pack-cdeb506a0b05e58a6ebfc7a6b6d7883cadaa6aea.idx

     文件   19641426  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\objects\pack\pack-cdeb506a0b05e58a6ebfc7a6b6d7883cadaa6aea.pack

     文件        114  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\packed-refs

     文件         41  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\refs\heads\master

     文件         32  2018-04-29 22:38  深度学习入门之Pytorch源码\.git\refs\remotes\origin\HEAD

     文件         25  2018-04-29 22:38  深度学习入门之Pytorch源码\.gitignore

     文件      10171  2018-04-29 22:38  深度学习入门之Pytorch源码\aws.md

     文件      25120  2018-04-29 22:38  深度学习入门之Pytorch源码\chapter10_Natural-Language-Process\char_rnn\char_rnn.ipynb

     文件       1732  2018-04-29 22:38  深度学习入门之Pytorch源码\chapter10_Natural-Language-Process\char_rnn\config.py

     文件       3031  2018-04-29 22:38  深度学习入门之Pytorch源码\chapter10_Natural-Language-Process\char_rnn\data\dataset.py

     文件        124  2018-04-29 22:38  深度学习入门之Pytorch源码\chapter10_Natural-Language-Process\char_rnn\data\__init__.py

............此处省略166个文件信息

评论

共有 条评论