1.创建一个未初始化矩阵

from __future__ import print_function
import torch x = torch.empty(2,3)#uninitialized matrix
print(x)

2.均匀分布

x = torch.rand(2,3)
print(x)

3.创建一个零矩阵

x = torch.zeros(5,3,dtype = torch.long)
print(x)

4.自定义初始化矩阵,覆盖并改变dtype

x = torch.tensor([5.5,3])#初始化
x = torch.randn_like(x,dtype=torch.float)#override dtype
print(x)

5.产生单位矩阵

x = torch.eye(4,4,dtype = torch.float)
print(x)

6.将已有矩阵的元素都变成1

x = torch.eye(4,4,dtype = torch.float)
x= x.new_ones(5,3)
print(x)

7.加

from __future__ import print_function
import torch x = torch.eye(5,3,dtype = torch.float)
y= x.new_ones(5,3)
print(x+y)
print(torch.add(x,y)) result = torch.empty(5,3)
torch.add(x,y,out = result)
print(result) y.add_(x)
print(y)

8.打印第一列

print(x[:,0])

9.view

x = torch.randn(4,5)
y = x.view(20)
z = x.view(-1,10)
print(x.size(),y.size(),z.size())
print(x)
print(y)
print(z)

10.item (单元素矩阵转数字)

x = torch.randn(1)
print(x)
print(x.item())

11.tensor转numpy

a=torch.ones(5)
print(a)
b=a.numpy()
print(b)

12.numpy转tensor

import numpy as np
a = np.ones(5)
b=torch.from_numpy(a)
np.add(a,1,out=a)
print(a)
print(b)

13.numpy转PIL

Image.fromarray(np.uint8(img))

14.PIL转numpy

img = Image.open('1.png')
a = np.asarray(img)

  

pytorch基础的更多相关文章

  1. [人工智能]Pytorch基础

    PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...

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

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

  3. pytorch基础学习(二)

    在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...

  4. PyTorch基础——词向量(Word Vector)技术

    一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...

  5. pytorch 基础内容

    一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...

  6. Pytorch 基础

    Pytorch 1.0.0 学习笔记: Pytorch 的学习可以参考:Welcome to PyTorch Tutorials Pytorch 是什么? 快速上手 Pytorch! Tensors( ...

  7. pytorch基础教程1

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

  8. 【pytorch】pytorch基础学习

    目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...

  9. Pytorch基础(6)----参数初始化

    一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...

  10. pytorch基础学习(一)

    在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...

随机推荐

  1. 论文笔记: Mutual Learning to Adapt for Joint Human Parsing and Pose Estimation

    Mutual Learning to Adapt for Joint Human Parsing and Pose Estimation 2018-11-03 09:58:58 Paper: http ...

  2. struts2学习笔记(一)

    配置文件web.xml和struts.xml web.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  3. png转tif

    发国外的文章要求图片是tif,cmyk色彩空间的. 大小尺寸还有要求. 比如 网上大神多,找到了一段代码,感谢! https://www.jianshu.com/p/ec2af4311f56 http ...

  4. 综述 - 染色质可及性与调控表观基因组 | Chromatin accessibility and the regulatory epigenome

    RNA-seq这个工具该什么时候用?ATAC-seq该什么时候用?有相当一部分项目设计不行,导致花大钱测了一些没有意义的数据. 还是在中心法则这个框架下来解释,这是生物信息的核心.打开华大科技服务官网 ...

  5. 实现mypwd&mybash&myod&读者写者

    目录: 一.mypwd 二.mybash 三.myod 四.读者.写者 一.实现mypwd 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试m ...

  6. 使用Python访问微信

    itchat是一个开源的微信个人号接口,使用它我们可以很方便的访问我们个人微信号里的信息.itchat的github地址如下: https://github.com/littlecodersh/itc ...

  7. Matlab:五点差分方法求解椭圆方程非导数边值问题

    差分格式脚本文件: tic; clear clc M=32;%x的步数 N=16;%y的步数 h1=1/M;%x的步长 h2=1/N;%y的步长 x=0:h1:1; y=0:h2:1; u=zeros ...

  8. createDocumentFragment() 方法

    //createdocumentfragment()方法创建了一虚拟的节点对象,节点对象包含所有属性和方法. //当你想提取文档的一部分,改变,增加,或删除某些内容及插入到文档末尾可以使用create ...

  9. JJTree Tutorial for Advanced Java Parsing

    The Problem JJTree is a part of JavaCC is a parser/scanner generator for Java. JJTree is a preproces ...

  10. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example 1.4 Labeling the Map

    MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example 1.4 Labeling the Map 一.前言 MapServer拥有非常灵活的标签 ...