当前位置:网站首页>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
边栏推荐
- Mysql_13 事务
- Redis visual management software Redis Desktop Manager2022
- 软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
- 《MySQL入门很轻松》第2章:MySQL管理工具介绍
- 怎样进行在不改变主线程执行的时候,进行日志的记录
- SV class virtual method of polymorphism
- 2022 Hangzhou Electric Multi-School 1004 Ball
- 软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
- Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
- [idea] idea configures sql formatting
猜你喜欢

图解 Canvas 入门

倒计时1天!8月2日—4日与你聊聊开源与就业那些事!

元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?

电赛必备技能___定时ADC+DMA+串口通信

node使用redis

Huggingface入门篇 II (QA)

性能测试如何准备测试数据

Inter-process communication and inter-thread communication

The master teaches you the 3D real-time character production process, the game modeling process sharing

SV 类的虚方法 多态
随机推荐
lua 如何 实现一个unity协程的工具
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
日志(logging模块)
2022多校第二场 K题 Link with Bracket Sequence I
tiup telemetry
Software testing interview questions: What are the strategies for system testing?
uinty lua 关于异步函数的终极思想
What is next-generation modeling (with learning materials)
图解 Canvas 入门
Mysql_14 存储引擎
软件测试面试题:软件测试类型都有哪些?
2 用D435i运行VINS-fusion
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
matlab中rcosdesign函数升余弦滚降成型滤波器
tiup update
D - I Hate Non-integer Number (选数的计数dp
软件测试面试题:软件都有多少种分类?
2022杭电多校第一场 1004 Ball
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?