当前位置:网站首页>[pytorch learning notes] tensor
[pytorch learning notes] tensor
2022-07-02 19:20:00 【liiiiiiiiiiiiike】
Plate purpose :
Systematization Pytorch The learning process , Only record dry goods
tensor
Tensor and ndarrays similar , Both can usually share the same underlying memory , This eliminates the need to replicate data .
import torch
import numpy as np
tensor initialization
import torch
import numpy as np
## Initialization tensor
# Directly from data
data = [[1,2], [3,4]]
x_data = torch.tensor(data)
# numpy turn torch
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
# From the other tensor
x_ones = torch.ones_like(x_data)
x_rand = torch.rand_like(x_data,dtype=torch.float)
# Use random or constant values
shape = (2,3)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
# tensor attribute
tensor = torch.rand(3,4)
print(tensor.shape) # dimension
print(tensor.dtype) # data type
print(tensor.device) # tensor Operation unit
tensor operation
# section
tensor = torch.ones(4,4)
print(tensor[0]) # first line
print(tensor[:,0]) # First column
print(tensor[...,-1]) # The last line
tensor[:,1] = 0
print(tensor)
''' tensor([1., 1., 1., 1.]) tensor([1., 1., 1., 1.]) tensor([1., 1., 1., 1.]) tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]]) '''
# Splicing
t1 = torch.cat([tensor,tensor,tensor],dim=1) # Stack by column
print(t1)
# Arithmetic operations
y1 = tensor @ tensor.T # @ Point multiplication .T Transposition
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T,out=y3) # tensor Point multiplication Output y3
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor,tensor,out=z3)
# tensor Aggregate into a value
agg = tensor.sum()# <class 'torch.Tensor'>
agg_item = agg.item()#<class 'float'>
print(type(agg_item))
# Local operation
tensor.add_(5) # _ Indicates in situ operation , Directly modifying tensor, Can save memory , But there will be problems when calculating the derivative , Loss of raw data
print(tensor)
# cpu and numpy Upper tensor Can share their underlying memory location
t = torch.ones(5) # torch.tensor
n = t.numpy()# ndarray
t.add_(1)
print(t)# tensor([2., 2., 2., 2., 2.])
print(n)# [2. 2. 2. 2. 2.]
边栏推荐
- 预处理和预处理宏
- "Patient's family, please come here" reading notes
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- Codeworks 5 questions per day (1700 average) - day 4
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- Kubernetes three open interfaces first sight
- 2022编译原理期末考试 回忆版
- [100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
- Talk about the design of red envelope activities in e-commerce system
- The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
猜你喜欢

Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式

医院在线问诊源码 医院视频问诊源码 医院小程序源码

聊聊电商系统中红包活动设计

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

高频面试题

机器学习笔记 - 时间序列预测研究:法国香槟的月销量

The difference between interceptor and filter

How performance testing creates business value
随机推荐
论文导读 | 机器学习在数据库基数估计中的应用
数字滚动带动画
How performance testing creates business value
数据降维——主成分分析
Novice must see, click two buttons to switch to different content
How to print mybats log plug-in using XML file
PHP-Parser羽毛球预约小程序开发require线上系统
codeforces每日5题(均1700)-第四天
Thread application instance
2022 software engineering final exam recall Edition
Compile oglpg-9th-edition source code with clion
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
The difference between interceptor and filter
SIFT特征点提取「建议收藏」
Emmet基础语法
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
PHP parser badminton reservation applet development requires online system
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比