Pytorch学习(一)基础语法篇
how to use pytorch
1.Tensor
we can create a tensor just like creating a matrix the default type of a tensor is float
import torch as t
a = t.Tensor([[1,2],[3,4],[5,6]])
a
tensor([[1., 2.],
[3., 4.],
[5., 6.]])
we can also change the datatype of a tensor
b = t.LongTensor([[1,2],[3,4],[5,6]])
b
tensor([[1, 2],
[3, 4],
[5, 6]])
we can also create a tensor filled with zero or random values
c = t.zeros((3,2))
d = t.randn((3,2))
print(c)
print(d)
tensor([[0., 0.],
[0., 0.],
[0., 0.]])
tensor([[ 1.2880, -0.1640],
[-0.2654, 0.7187],
[-0.3156, 0.4489]])
we can change the value in a tensor we've created
a[0,1] = 100
a
tensor([[ 1., 100.],
[ 3., 4.],
[ 5., 6.]])
numpy and tensor can transfer from each other
import numpy as np
e = np.array([[1,2],[3,4],[5,6]])
torch_e = t.from_numpy(e)
torch_e
tensor([[1, 2],
[3, 4],
[5, 6]])
2.Variable
Variable consists of data, grad, and grad_fn
data为Tensor中的数值
grad是反向传播梯度
grad_fn是得到该Variable的操作 例如加减乘除
from torch.autograd import Variable
x = Variable(t.Tensor([1]),requires_grad = True)
w = Variable(t.Tensor([2]),requires_grad = True)
b = Variable(t.Tensor([3]),requires_grad = True)
y = w*x+b
y.backward()
print(x.grad)
print(w.grad)
print(b.grad)
tensor([2.])
tensor([1.])
tensor([1.])
we can also calculate the grad of a matrix
x = t.randn(3)
x = Variable(x,requires_grad=True)
y = x*2
print(y)
y.backward(t.FloatTensor([1,1,1]))
print(x.grad)
tensor([-2.4801, 0.6291, -0.4250], grad_fn=<MulBackward>)
tensor([2., 2., 2.])
3.dataset
you can define the function len and getitem to write your own dataset
import pandas as pd
from torch.utils.data import Dataset
class myDataset(Dataset):
def __init__(self, csv_file, txt_file, root_dir, other_file):
self.csv_data = pd.read_csv(csv_file)
with open(txt_file, 'r') as f:
data_list = f.readlines()
self.txt_data = data_list
self.root_dir = root_dir
def __len__(self):
return len(self.csv_data)
def __getitem(self,idx):
data = (self.csv_data[idx],self.txt_data[idx])
return data
4.nn.Module
from torch import nn
class net_name(nn.Module):
def __init(self,other_arguments):
super(net_name, self).__init__()
def forward(self,x):
x = self.convl(x)
return x
5.Optim
1.一阶优化算法
常见的是梯度下降法\(\theta = \theta-\eta\times \frac{\partial J(\theta)}{\partial\theta}\)
2.二阶优化算法
Hessian法
Pytorch学习(一)基础语法篇的更多相关文章
- Python学习笔记——基础语法篇
一.Python初识(IDE环境及基本语法,Spyder快捷方式) Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,没有编译过程,可移植,可嵌入,可扩展. IDE 1.检查Pyth ...
- Xamarin XAML语言教程基础语法篇大学霸
Xamarin XAML语言教程基础语法篇大学霸 前 言 Xamarin是一个跨平台开发框架.它可以用来开发iOS.Android.Windows Phone和Mac的应用程序.使用Xamarin框 ...
- JavaScript学习02 基础语法
JavaScript学习02 基础语法 JavaScript中很多基础内容和Java中大体上基本一样,所以不需要再单独重复讲了,包括: 各种算术运算符.比较运算符.逻辑运算符: if else语句.s ...
- Scala快速入门 - 基础语法篇
本篇文章首发于头条号Scala快速入门 - 基础语法篇,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干货,也欢迎关注我的 ...
- 真香,理解记忆法学习Python基础语法
这篇文章很难写!我最开始学 Python,和大多数人一样,是看的菜鸟教程: 在写完这篇文章的第一遍后,我发现并没有写出新意,很可能读者看到后,会和我当初一样,很快就忘了.我现在已经不是读者而是作者了, ...
- JavaScript学习笔记-基础语法、类型、变量
基础语法.类型.变量 非数字值的判断方法:(因为Infinity和NaN他们不等于任何值,包括自身) 1.用x != x ,当x为NaN时才返回true; 2.用isNaN(x) ,当x为NaN或 ...
- less学习:基础语法总结
一. less是什么 Less 是一门 CSS 预处理语言,它扩充了 CSS 语言,增加了诸如变量.混合(mixin).函数等功能,让 CSS 更易维护.方便制作主题.扩充. 注意1):less使用. ...
- Python学习①. 基础语法
Python 简介 Python 是一种解释型,面向对象的语言.特点是语法简单,可跨平台 Python 基础语法 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编 ...
- 【Java基础总结】Java基础语法篇(上)
Java基础语法 思维导图 一.Java语言介绍 1.Java应用平台 JavaSE(Java Platform Standard Edition):开发普通桌面和商务应用程序,是另外两类的基础 Ja ...
随机推荐
- java 发送 mail 纯文本发送和html格式发送
一:需要引入mail maven jar包 <!--邮件发送包--> <dependency> <groupId>javax.mail</groupId> ...
- 20175221 2018-2019-2 《Java程序设计》第二周学习总结
20175221 <Java程序设计>第2周学习总结 教材学习内容总结 教材方面 本周学习了第二章的“基本数据类型与数组”的内容,以及粗略地看了一下第三章“运算符.表达式和语句”的内容 ...
- django系列8:优化vote页面,使用通用视图降低代码冗余
修改detail.html,将它变为一个可用的投票页面 <h1>{{ question.question_text }}</h1> {% if error_message %} ...
- linux镜像下载
https://blog.csdn.net/qq_42570879/article/details/82853708
- 前向分步算法 && AdaBoost算法 && 提升树(GBDT)算法 && XGBoost算法
1. 提升方法 提升(boosting)方法是一种常用的统计学方法,在分类问题中,它通过逐轮不断改变训练样本的权重,学习多个分类器,并将这些分类器进行线性组合,提高分类的性能 0x1: 提升方法的基本 ...
- Tuxedo 汇总
===================================C/S / Tuxedo 架构/ B/S 架构演进===================================Tuxed ...
- [物理学与PDEs]第1章第7节 媒质中的 Maxwell 方程组 7.3 媒质中电磁场量的表示
1. 电磁能量密度 $$\bex \cfrac{1}{2}({\bf E}\cdot{\bf D}+{\bf B}\cdot{\bf H}). \eex$$ 2. 电磁能量流密度向量 $$\bex { ...
- JavaScript 的正则也有单行模式了
正则表达式最早是由 Ken Thompson 于 1970 年在他改进过的 QED 编辑器里实现的,正则里最简单的元字符 “.” 在当时所匹配的就是除换行符外的任意字符: "." ...
- 程序通过ReportViewer对象来调用reporting services并导出pdf文件
ReportViewer tempReportViewer = new ReportViewer(); tempReportViewer.ProcessingMode = ProcessingMode ...
- IDEA 中使用MyBatis-generator 自动生成MyBatis代码
0.在Intellij IDEA创建maven项目 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 <build> < ...