当前位置:网站首页>Pytorch usage and tricks
Pytorch usage and tricks
2022-08-05 00:32:00 【runny egg】
#Import the package firstimport torchimport numpy as np1. Creation of tensor
1.1 Method of initializing tensor
data = [[1, 2][3, 4]] # Initialize tensor with listtensor_data = torch.tensor(data)# create tensor from numpy arraydata = np.random.normal((2, 3))tensor_data = torch.tensor(data)# Create an all-one tensor of the same size as another tensordata_ones = torch.ones_like(data)# Create a random tensor of the same size as another tensordata_rand = torch.rand_like(data)# create random tensorrand_data = torch.rand([2, 3]) # where size can be an array, tuple, not a dictionary1.2 View tensor properties
data = torch.randn((2, 3))# Determine if it is a tensordata.is_tensor() # return True or Falsedata.is_complex(). # Returns True if the data is complex, otherwise returns Falsedata.is_floating_point() # Returns True if the data is a floating point type, otherwise Falsetorch.is_nonzero(data) # Does it contain a single element tensor, and returns False if it is 0, otherwise Truetorch.numel(data) # Determine the number of all elements in the tensor1.3 Tensor Operations
torch.zeros([H, W]) # Returns an all-zero tensortorch.range(start=0, end, step=1) # Generate continuous tensors, passing in a parameter defaults to endtorch.arange(start, end, step=1) # also generates continuous tensors# Note that the contiguous array generated by torch.range has one more element than torch.arangetorch.eye(n, m=None) # Create an n * m identity matrix, without m will create an n * n square matrixtorch.full([H, W], m) , create a H * W matrix with all m elementstorch.cat()# Concatenate tensors# E.g
I didn't think about this when I used tensorflow. I read the code today and found that torch has such an important statement, record it and learn it:
1. model.train()
Enable BatchNormalization and Dropout
2. model.eval()
Disable BatchNormalization and Dropout
After training the train samples, the generated model will beto test the sample.Before model(test), model.eval() needs to be added, otherwise as long as there is input data, even if there is no training, the model will change the weights.This is the property brought by the batch normalization layer contained in the model.
Reprinted from: https://zhuanlan.zhihu.com/p/208233193
边栏推荐
- Flask框架 根据源码分析可扩展点
- E - Distance Sequence (prefix and optimized dp
- [idea] idea configures sql formatting
- Zombie and orphan processes
- matlab 采用描点法进行数据模拟和仿真
- leetcode:267. 回文排列 II
- 软件测试面试题:一套完整的测试应该由哪些阶段组成?
- 刘润直播预告 | 顶级高手,如何创造财富
- Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
- tiup status
猜你喜欢

gorm联表查询-实战

Senior game modelers tell newbies, what are the necessary software for game scene modelers?

SV class virtual method of polymorphism

机器学习(公式推导与代码实现)--sklearn机器学习库

node使用redis

QSunSync Qiniu cloud file synchronization tool, batch upload

2022杭电多校第三场 K题 Taxi

oracle创建用户

【Valentine's Day special effects】--Canvas realizes full screen love
![[LeetCode] Summary of Matrix Simulation Related Topics](/img/80/bd71ca5211cce5805909015a642893.jpg)
[LeetCode] Summary of Matrix Simulation Related Topics
随机推荐
tiup telemetry
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
Redis visual management software Redis Desktop Manager2022
标识符、关键字、常量 和变量(C语言)
2022牛客多校训练第二场 H题 Take the Elevator
E - Distance Sequence (前缀和优化dp
软件测试面试题:测试用例通常包括那些内容?
2022牛客多校训练第二场 L题 Link with Level Editor I
【无标题】
Software Testing Interview Questions: What's the Key to a Good Test Plan?
10 种常见的BUG分类
2022 Hangzhou Electric Multi-School Training Session 3 1009 Package Delivery
软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
tiup status
【Unity编译器扩展之进度条】
软件测试面试题:LoadRunner 分为哪三个模块?
性能测试如何准备测试数据
软件测试面试题:软件测试类型都有哪些?
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
node使用redis