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 ...
随机推荐
- 【CF1132F】Clear the String(动态规划)
[CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...
- TensorFlow深度学习,一篇文章就够了
http://blog.jobbole.com/105602/ 作者: 陈迪豪,就职小米科技,深度学习工程师,TensorFlow代码提交者. TensorFlow深度学习框架 Google不仅是大数 ...
- Python:正则表达式详解
正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技能,正则表达式的在不同的语言中使用方式可能 ...
- Python 文件读取
1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file ...
- 机器学习 - 损失计算-softmax_cross_entropy_with_logits
tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 第一个参数logits:就是神经网络最后一层的输出 第二个参数la ...
- Three ways to detect outliers
Z-score import numpy as np def outliers_z_score(ys): threshold = 3 mean_y = np.mean(ys) stdev_y = np ...
- 理解 Web 中的Session
===================================Session 工作原理是什么?===================================因为 http 协议是无状态 ...
- python 排序之sort
#coding:utf-8 #求列表的第二大值 list_test =[6,2,4,6,1,2,3,4,5] list_test.sort() print list_test[-2] "&q ...
- Windows下VSCode编译调试c/c++
参考链接: https://blog.csdn.net/c_duoduo/article/details/51615381 支持makefile编译: https://www.cnblogs.com ...
- salt软件远程控制服务器
1.salt安装服务器环境 准备2台机器 192.168.11.250 master端(主人) 192.168.11.167 minion端 (奴隶 ) 2.两台机器配置hosts文件,用于加速域名解 ...