当前位置:网站首页>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
边栏推荐
- 2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
- SV class virtual method of polymorphism
- 2022 Nioke Multi-School Training Session H Question H Take the Elevator
- 软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
- 软件测试面试题:一套完整的测试应该由哪些阶段组成?
- 软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
- 2022 Hangzhou Electric Multi-School 1004 Ball
- 软件测试面试题:什么是软件测试?软件测试的目的与原则?
- 软件开发工具的技术要素
- 软件测试面试题:做好测试计划的关键是什么?
猜你喜欢
随机推荐
NMS原理及其代码实现
MongoDB搭建及基础操作
Mysql_12 多表查询
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
国内网站用香港服务器会被封吗?
oracle创建表空间
matlab 采用描点法进行数据模拟和仿真
[idea] idea configures sql formatting
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
进程间通信和线程间通信
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?
uinty lua 关于异步函数的终极思想
怎样进行在不改变主线程执行的时候,进行日志的记录
TinyMCE disable escape
软件测试面试题:网络七层协仪具体?
Software Testing Interview Questions: About Automated Testing Tools?
gorm联表查询-实战
电赛必备技能___定时ADC+DMA+串口通信
Inter-process communication and inter-thread communication







![[Cloud Native--Kubernetes] Pod Controller](/img/e1/1a8cc82223f9a9be79ebbf1211e9a4.png)

