当前位置:网站首页>Pytorch usage and tricks
Pytorch usage and tricks
2022-08-05 00:32:00 【runny egg】
#Import the package firstimport torchimport numpy as np
1. 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 dictionary
1.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 tensor
1.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
边栏推荐
- 性能测试如何准备测试数据
- 找不到DiscoveryClient类型的Bean
- 国内网站用香港服务器会被封吗?
- Huggingface入门篇 II (QA)
- 元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
- uinty lua 关于异步函数的终极思想
- 软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
- 软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
- About I double-checked and reviewed the About staff page, returning an industry question
- E - Many Operations (按位考虑 + dp思想记录操作后的结果
猜你喜欢
Three tips for you to successfully get started with 3D modeling
[Cloud Native--Kubernetes] Pod Controller
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
【Valentine's Day special effects】--Canvas realizes full screen love
数据类型及输入输出初探(C语言)
QSunSync 七牛云文件同步工具,批量上传
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
2 用D435i运行VINS-fusion
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
随机推荐
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
软件测试面试题:测试用例通常包括那些内容?
TinyMCE disable escape
GO中sync包自由控制并发的方法
ARC129E Yet Another Minimization 题解 【网络流笔记】
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
Metasploit-域名上线隐藏IP
typeScript - Partially apply a function
leetcode:267. 回文排列 II
测试经理要不要做测试执行?
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
国内网站用香港服务器会被封吗?
2022牛客多校训练第二场 L题 Link with Level Editor I
oracle创建用户以后的权限问题
lua 如何 实现一个unity协程的工具
2022杭电多校第三场 K题 Taxi
About I double-checked and reviewed the About staff page, returning an industry question
leetcode:269. 火星词典
Inter-process communication and inter-thread communication
找不到DiscoveryClient类型的Bean