0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch

************************************************************

1.pytorch数据结构

1)初始化方式:

eg1: 列表初始化:

data = [-1, -2, 1, 2] tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor

data = [[1,2], [3,4]] tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor

data = torch.FloatTensor([1,2,3])

eg2: numpy 到torch

import torch

import numpy as np

np_data = np.arange(6).reshape((2, 3))

torch_data = torch.from_numpy(np_data)

eg3: 直接用自带函数初始化

import torch

a=torch.rand(3,4)

b=torch.eye(3,4)

c=torch.ones(3,4)

d=torch.zeros(3,4)

x = torch.linspace(1, 10, 10)

eg4: 分配和其他相同size的内存,然后fill_

y = dist_an.data.new()
y.resize_as_(dist_an.data)
y.fill_(1)

eg5: 没有初始化,需要对其进行赋值操作

a = torch.Tensor(2,4)
c = torch.IntTensor(2,3);print(c) -- 也可以指定类型

2)数据结构类型转换

eg1: cpu,gpu之间数据转换

d=b.cuda()

e=d.cpu()

net = Net().cuda()

eg2:numpy, torch转换

c=b.numpy()

b = torch.from_numpy(a)

eg3: torch转可导的Variable

y = Variable(y)

eg4: ByteTensor, FloatTensor转换

dtype =  tensor.FloatTensor

# dtype =  tensor.cuda.FloatTensor

x=torch.rand(3,4).type(dtype)

eg5: torch的Variable转tensor

y = y.data

************************************************************

2.哪些基本运算

矩阵乘法:torch.mm(tensor, tensor)

均值:torch.mean(tensor)

三角函数:np.sin(data)

绝对值:torch.abs(tensor)

************************************************************

3.哪些包,哪些函数

1)官网中文api

2) 官网英文

************************************************************

4. 反向传播例子:

a=Variable(torch.FloatTensor(torch.randn(2,2)),requires_grad=True)

b=a+2

c=b*b*3

out=c.mean()

out.backward()

a.grad()

************************************************************

5. 网络定义、训练、保存、恢复、打印网络(莫烦)

见下一个博客吧。参考博客1博客2博客3莫烦自定义function,module(重要), 自定义examples, (重要)拓新module,一个自定义的解释自定义解释

1)无参数的:用function足矣

from torch.autograd import Function
 import torch
from torch.autograd import Function class ReLUF(Function):
def forward(self, input):
self.save_for_backward(input) output = input.clamp(min=0)
return output def backward(self, output_grad):
input = self.to_save[0] input_grad = output_grad.clone()
input_grad[input < 0] = 0
return input_grad ## Test
if __name__ == "__main__":
from torch.autograd import Variable torch.manual_seed(1111)
a = torch.randn(2, 3) va = Variable(a, requires_grad=True)
vb = ReLUF()(va)
print va.data, vb.data vb.backward(torch.ones(va.size()))
print vb.grad.data, va.grad.data

2)有参数,先用function,然后用module+参数打包

step 1:

 import torch
from torch.autograd import Function class LinearF(Function): def forward(self, input, weight, bias=None):
self.save_for_backward(input, weight, bias) output = torch.mm(input, weight.t())
if bias is not None:
output += bias.unsqueeze(0).expand_as(output) return output def backward(self, grad_output):
input, weight, bias = self.saved_tensors grad_input = grad_weight = grad_bias = None
if self.needs_input_grad[0]:
grad_input = torch.mm(grad_output, weight)
if self.needs_input_grad[1]:
grad_weight = torch.mm(grad_output.t(), input)
if bias is not None and self.needs_input_grad[2]:
grad_bias = grad_output.sum(0).squeeze(0) if bias is not None:
return grad_input, grad_weight, grad_bias
else:
return grad_input, grad_weight

step 2:

 import torch
import torch.nn as nn class Linear(nn.Module): def __init__(self, in_features, out_features, bias=True):
super(Linear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.Tensor(out_features, in_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(out_features))
else:
self.register_parameter('bias', None) def forward(self, input):
return LinearF()(input, self.weight, self.bias)

************************************************************

6.哪些教程

1)官网

2)github

3)莫烦的视频

4)官网中文api

5) 官网英文

************************************************************

广告:

np_data = np.arange(6).reshape((2, 3))

start_time = time.time()

end_time = time.time() print("Spend time:", end_time - start_time)

pytorch基础教程1的更多相关文章

  1. pytorch基础教程2

    1. 四部曲 1)forward; 2) 计算误差 :3)backward; 4) 更新 eg: 1)outputs = net(inputs) 2)loss = criterion(outputs, ...

  2. Note | PyTorch官方教程学习笔记

    目录 1. 快速入门PYTORCH 1.1. 什么是PyTorch 1.1.1. 基础概念 1.1.2. 与NumPy之间的桥梁 1.2. Autograd: Automatic Differenti ...

  3. 【新生学习】第一周:深度学习及pytorch基础

    DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...

  4. matlab基础教程——根据Andrew Ng的machine learning整理

    matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...

  5. <<Bootstrap基础教程>> 新书出手,有心栽花花不开,无心插柳柳成荫

    并非闲的蛋疼,做技术也经常喜欢蛋疼,纠结于各种技术,各种需求变更,还有一个很苦恼的就是UI总是那么不尽人意.前不久自己开源了自己做了多年的仓储项目(开源地址:https://github.com/he ...

  6. Memcache教程 Memcache零基础教程

    Memcache是什么 Memcache是danga.com的一个项目,来分担数据库的压力. 它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建立一个Hash ...

  7. Selenium IDE 基础教程

    Selenium IDE 基础教程 1.下载安装     a 在火狐浏览其中搜索附件组件,查找 Selenium IDE     b 下载安装,然后重启firefox 2.界面讲解      在菜单- ...

  8. html快速入门(基础教程+资源推荐)

    1.html究竟是什么? 从字面上理解,html是超文本标记语言hyper text mark-up language的首字母缩写,指的是一种通用web页面描述语言,是用来描述我们打开浏览器就能看到的 ...

  9. 转发-UI基础教程 – 原生App切图的那些事儿

    UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切 ...

随机推荐

  1. express中app.get和app.use的解析

    app.get(path,callback)中的callback只能是函数 app.use(path,callback)中的callback既可以是router对象又可以是函数 当其是router对象 ...

  2. iOS封装功能生成 .framework

    前言 如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这些实现你可能并不想开源出来.此外,开发者也可能并不想看到你的所有代码,因为 ...

  3. 更改pip源至国内镜像

    更改pip源至国内镜像   经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样 ...

  4. Mockito:一个强大的用于Java开发的模拟测试框架

    https://blog.csdn.net/zhoudaxia/article/details/33056093 介绍 本文将介绍模拟测试框架Mockito的一些基础概念, 介绍该框架的优点,讲解应用 ...

  5. python-flask基础

    get请求: 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. 传参:get请求传参是放在url中,并且是通过’?’的形式来指定key和value的. post ...

  6. Java Web(三) Servlet会话管理

    会话跟踪 什么是会话? 可简单理解为,用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭服务器,整个过程称为一个会话.从特定客户端到服务器的一系列请求称为会话.记录会话信息的技术称 ...

  7. js评分

    js评分    原理:给ele挂载一个自定义属性保存选中的星星数,鼠标经过时,显示所在星数的评价内容,以及给他星星亮起来,鼠标移开时显示的星星数时选择的星星数,没选的话是默认星星数,点击时,将选中的星 ...

  8. UVALive 3401 - Colored Cubes 旋转 难度: 1

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  9. 【转】c++ make_pair函数使用

    [好记性不如烂笔头:在<C++ Templates>看到这个函数,发现正是前段时间写项目程序所要用到的,可惜当时还不知道有这个用法,当时是自己写了个结构体..]Utilities < ...

  10. 【转】Java中static关键字用法总结

    1.     静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: · 它们仅能调用其他的static 方法. · 它 ...