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的更多相关文章

  1. PyTorch学习笔记之Tensors 2

    Tensors的一些应用 ''' Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来加速运算 ''' # from _future_ import print ...

  2. 【pytorch】pytorch学习笔记(一)

    原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...

  3. [PyTorch 学习笔记] 1.3 张量操作与线性回归

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...

  4. Pytorch学习笔记(二)---- 神经网络搭建

    记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...

  5. Pytorch学习笔记(一)---- 基础语法

    书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...

  6. 【深度学习】Pytorch 学习笔记

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  7. Pytorch学习笔记(一)——简介

    一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...

  8. [PyTorch 学习笔记] 1.1 PyTorch 简介与安装

    PyTorch 的诞生 2017 年 1 月,FAIR(Facebook AI Research)发布了 PyTorch.PyTorch 是在 Torch 基础上用 python 语言重新打造的一款深 ...

  9. [PyTorch 学习笔记] 1.4 计算图与动态图机制

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/computational_graph.py 计算图 深 ...

随机推荐

  1. LeetCode(200) Number of Islands

    题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...

  2. HDU 1827 强连通 缩点 Summer Holiday

    求出强连通分量,因为强连通中只要有一个人被通知到了,所有人都能被通知到. 缩点以后形成一个DAG,找出那些入度为0的点,累加上它们的权值就是答案.一个点的权值等于SCC中权值最小的那个点. #incl ...

  3. jQuery ui 百叶窗blind方向设置

    百叶窗特效(Blind Effect)参数direction设置 $( document ).click(function() { $( "#toggle" ).toggle( & ...

  4. js 常用判断

    JS三元运算符 三元运算符: 如名字表示的三元运算符需要三个操作数. 语法是 条件 ? 结果1 : 结果2;. 这里你把条件写在问号(?)的前面后面跟着用冒号(:)分隔的结果1和结果2.满足条件时结果 ...

  5. Linux inode 之我见

    Linux硬盘组织方式为:引导区.超级块(superblock),索引结点(inode),数据块(datablock),目录块(diredtory block).其中超级块中包含了关于该硬盘或分区上的 ...

  6. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  7. 30、自定义gridview

    要想实现自定义gridview效果,有下边几个步骤: 1.定义grivew中的item的xml文件 假如item需要显示一个图片,图片下边显示文字: <?xml version="1. ...

  8. 数据库导入Exel,输入到浏览器

    db.php <?php require dirname(__FILE__)."/dbconfig.php";//引入配置文件 class db{ public $conn= ...

  9. Wannafly挑战赛5

    珂朵莉与宇宙 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 星神是来自宇宙的 所以珂朵莉也是吧 ...

  10. 在SAE搭建微信公众账号服务

    让我们回到2014年11月,从公司请假回成都,在天府软件园B区旁边的小区里,那个10多平米的出租屋里,闲来无事,我想找个事情做一做,好让我这漂浮的心静下来.大约在半年前就申请了微信的一个公众账号,一直 ...