当前位置:网站首页>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
边栏推荐
- Software test interview questions: BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
- matlab 采用描点法进行数据模拟和仿真
- 翁恺C语言程序设计网课笔记合集
- 【idea】idea配置sql格式化
- STC89C52RC的P4口的应用问题
- 2022杭电多校 第三场 B题 Boss Rush
- node uses redis
- node使用redis
- Getting started with 3D modeling for games, what modeling software can I choose?
- 【unity编译器扩展之模型动画拷贝】
猜你喜欢

What is next-generation modeling (with learning materials)

典型相关分析CCA计算过程

2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi

oracle创建用户

标识符、关键字、常量 和变量(C语言)
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

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

数据类型-整型(C语言)

Three tips for you to successfully get started with 3D modeling

元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
随机推荐
oracle创建用户以后的权限问题
tiup update
软件开发工具的技术要素
Software testing interview questions: How many types of software are there?
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
软件测试面试题:系统测试的策略有?
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?
Metasploit-域名上线隐藏IP
lua 如何 实现一个unity协程的工具
导入JankStats检测卡帧库遇到问题记录
leetcode: 267. Palindromic permutations II
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
typeScript - Partially apply a function
软件质量评估的通用模型
[idea] idea configures sql formatting
数据类型-整型(C语言)
Software testing interview questions: What are the seven-layer network protocols?
2022杭电多校训练第三场 1009 Package Delivery
matlab中rcosdesign函数升余弦滚降成型滤波器
2022多校第二场 K题 Link with Bracket Sequence I