PyTorch学习笔记之Tensors
PyTorch Tensors are just like numpy arrays, but they can run on GPU.No built-in notion of computational graph, or gradients, or deep learning.Here we fit a two-layer net using PyTorch Tensors:
import torch dtype = torch.FloatTensor # step 1: create random tensors for data and weights
N, D_in, H, D_out = 64, 1000, 100, 10
x = torch.randn(N, D_in).type(dtype)
# print(x) # [torch.FloatTensor of size 64x1000]
y = torch.randn(N, D_out).type(dtype)
# print(y) # [torch.FloatTensor of size 64x10]
w1 = torch.randn(D_in, H).type(dtype)
# print(w1) # [torch.FloatTensor of size 1000x100]
w2 = torch.randn(H, D_out).type(dtype)
# print(w2) # [torch.FloatTensor of size 100x10]
step 2
learning_rate = 1e-6
for t in range(1):
# step 2: Forward pass: compute predictions and loss
h = x.mm(w1) # mm is which function ?
# print(h) # [torch.FloatTensor of size 64x100]
# print(x.mul(w1)) # RuntimeError: inconsistent tensor size
h_relu = h.clamp(min=0) # clamp(myValue, min, max)
# print(h_relu) # [torch.FloatTensor of size 64x100]
y_pred = h_relu.mm(w2)
# print(y_pred) # [torch.FloatTensor of size 64x10]
loss = (y_pred - y).pow(2).sum()
# print((y_pred - y).pow(2)) # pow() 方法返回 xy(x的y次方) 的值。
# print(loss) # 30832366.024527483 # define function clamp
# def clamp(minvalue, value, maxvalue):
# return max(minvalue, min(value, maxvalue))
''' h
6.2160e+00 -1.0304e+01 -2.1468e+01 ... 1.9651e+01 1.7158e+01 1.3336e+01
5.8056e+01 2.6900e+01 2.2681e+01 ... -3.0021e+01 -4.7533e+01 3.7371e+01
-1.6430e+01 -4.1532e+01 2.7384e+01 ... -3.2225e+01 -1.9597e+01 5.8636e+01
... ⋱ ...
9.2964e+00 6.5791e+01 1.8076e+01 ... 2.4620e+01 2.3355e+01 4.4987e-01
3.7563e+01 -2.6666e+01 3.5643e+01 ... 3.0626e+01 3.0002e+01 -1.3277e+01
-4.2287e+01 3.3466e+01 3.8845e+01 ... 2.1715e+01 -3.3691e+01 -2.5290e+01
[torch.FloatTensor of size 64x100] h_relu
6.2160 0.0000 0.0000 ... 19.6511 17.1578 13.3358
58.0565 26.8997 22.6810 ... 0.0000 0.0000 37.3708
0.0000 0.0000 27.3841 ... 0.0000 0.0000 58.6358
... ⋱ ...
9.2964 65.7915 18.0760 ... 24.6199 23.3550 0.4499
37.5627 0.0000 35.6430 ... 30.6257 30.0016 0.0000
0.0000 33.4656 38.8449 ... 21.7154 0.0000 0.0000
[torch.FloatTensor of size 64x100]
'''
step 3
for t in range(500):
# step 2: Forward pass: compute predictions and loss
h = x.mm(w1) # [torch.FloatTensor of size 64x100]
h_relu = h.clamp(min=0) # clamp(myValue, min, max)
# h_relu [torch.FloatTensor of size 64x100]
y_pred = h_relu.mm(w2) # # [torch.FloatTensor of size 64x10]
loss = (y_pred - y).pow(2).sum() # 30832366.024527483 # step 3: Backward pass: manually compute gradients
grad_y_pred = 2.0 * (y_pred - y) # [torch.FloatTensor of size 64x10]
grad_w2 = h_relu.t().mm(grad_y_pred) # .t()转置
grad_h_relu = grad_y_pred.mm(w2.t()) # [torch.FloatTensor of size 64x100]
grad_h = grad_h_relu.clone() # the same as
grad_h[h < 0] = 0
grad_w1 = x.t().mm(grad_h) # [torch.FloatTensor of size 1000x100] # print(h_relu)
# print(h_relu.t())
'''
0.0000 14.8044 0.0000 ... 0.0000 38.3654 0.0000
21.3853 0.0000 27.1789 ... 14.8747 14.6064 0.0000
33.8491 0.0000 0.0000 ... 26.2651 11.5845 0.0000
... ⋱ ...
11.2708 0.0000 0.0000 ... 0.0000 4.2082 0.0000
0.0000 0.0000 0.0000 ... 2.6930 5.6134 47.2977
0.0000 37.3445 0.0000 ... 31.3511 0.0000 64.6182
[torch.FloatTensor of size 64x100] 0.0000 21.3853 33.8491 ... 11.2708 0.0000 0.0000
14.8044 0.0000 0.0000 ... 0.0000 0.0000 37.3445
0.0000 27.1789 0.0000 ... 0.0000 0.0000 0.0000
... ⋱ ...
0.0000 14.8747 26.2651 ... 0.0000 2.6930 31.3511
38.3654 14.6064 11.5845 ... 4.2082 5.6134 0.0000
0.0000 0.0000 0.0000 ... 0.0000 47.2977 64.6182
[torch.FloatTensor of size 100x64]
''' # print(grad_h)
# grad_h[h < 0] = 0
# print(grad_h)
'''
-3.9989e+02 -9.3610e+02 -3.9592e+02 ... -1.0868e+03 6.9429e+02 3.3026e+02
9.4933e+02 1.2244e+03 2.4054e+02 ... 9.1655e+02 1.3783e+03 2.2368e+02
4.1473e+03 3.6368e+03 -3.2277e+02 ... 2.9705e+02 3.9689e+03 1.0691e+03
... ⋱ ...
1.2205e+03 -4.0321e+02 8.4314e+02 ... 1.0697e+03 1.0149e+02 -4.6613e+02
6.0660e+02 5.5411e+02 2.0111e+03 ... -7.9235e+02 7.9334e+02 -9.1837e+01
1.3468e+03 2.4743e+03 -3.9460e+02 ... 1.1505e+03 1.5951e+03 7.3752e+02
[torch.FloatTensor of size 64x100] 0.0000 0.0000 -395.9182 ... -1086.8199 0.0000 0.0000
949.3327 0.0000 240.5419 ... 0.0000 0.0000 223.6831
4147.3193 0.0000 0.0000 ... 297.0452 3968.9290 0.0000
... ⋱ ...
1220.4922 0.0000 843.1447 ... 1069.6855 101.4936 0.0000
0.0000 554.1067 2011.1219 ... -792.3494 0.0000 -91.8371
1346.8444 2474.3076 0.0000 ... 0.0000 1595.0582 737.5197
[torch.FloatTensor of size 64x100]
'''
step 4
# step 4: Gradient descent step on weights
w1 -= learning_rate * grad_w1 # [torch.FloatTensor of size 1000x100]
w2 -= learning_rate * grad_w2 # [torch.FloatTensor of size 100x10]
PyTorch学习笔记之Tensors的更多相关文章
- PyTorch学习笔记之Tensors 2
Tensors的一些应用 ''' Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来加速运算 ''' # from _future_ import print ...
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- [PyTorch 学习笔记] 1.3 张量操作与线性回归
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...
- Pytorch学习笔记(二)---- 神经网络搭建
记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...
- Pytorch学习笔记(一)---- 基础语法
书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...
- 【深度学习】Pytorch 学习笔记
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- Pytorch学习笔记(一)——简介
一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...
- [PyTorch 学习笔记] 1.1 PyTorch 简介与安装
PyTorch 的诞生 2017 年 1 月,FAIR(Facebook AI Research)发布了 PyTorch.PyTorch 是在 Torch 基础上用 python 语言重新打造的一款深 ...
- [PyTorch 学习笔记] 1.4 计算图与动态图机制
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/computational_graph.py 计算图 深 ...
随机推荐
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- 系统测试过程截获SQL方法
1 摘要 测试过程中,经常会遇到莫名的各种问题,可能从开发同学的日志无法发现具体出现问题的原因,本着测试同学深入分析.定位问题的目的,经常需要一些额外的手段获得更多的错误异常信息. 我们涉及 ...
- Android开发工具——Android Studio调试技巧
.调试的两种方式 到目前,调试的相关基础我们已经介绍完了,但是不少同学对Android Studio中这两个按钮感到困惑:Debug和Attach process. 这里我们就简单介绍一下这两者的区别 ...
- C指针问题
<!DOCTYPE html> 多级c指针传值问题 /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / Au ...
- iphone使用keychain来存取用户名和密码
1.在arc下系统提示使用__bridge http://www.cnblogs.com/zzltjnh/p/3885012.html 参考文档:http://blog.csdn.net/jerr ...
- Python学习——第一天
https://www.runoob.com/python/python-chinese-encoding.html 第一个python程序 [root@mini1 ~]# vi python01.p ...
- python-高级编程-03
[多进程与多线程] 调度 : 在传统计算机操作系统中 cpu的调度的基本单位是进程,随着线程的引入,线程变成操作系统的最小调度单位 而进程是作为资源的拥有单位. 并行:由于线程的引入 原先一个进程只能 ...
- [git 学习篇] git commit原理 --实践体会
1 现对readme.txt作出修改,增加一行内容: Git has a mutable index called stage. Git is a distributed version contro ...
- 股票交易(DP+单调队列优化)
题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi, ...
- 2017"百度之星"程序设计大赛 - 复赛
Arithmetic of Bomb Accepts: 1050 Submissions: 1762 Time Limit: 2000/1000 MS (Java/Others) Memory ...