当前位置:网站首页>Pytorch for former Torch users - Tensors
Pytorch for former Torch users - Tensors
2022-06-30 09:14:00 【The man of Jike will never admit defeat】
Pytorch for former Torch users - Tensors
Tensors
Pytorch Medium Tensor and Torch Medium Tensor Have almost the same usage
Create a (5*7) Uninitialized... Of size tensor
import torch
a = torch.empty(5, 7, dtype=torch.floar)
Initializes a double precision floating-point number randomly obtained from the standard normal distribution tensor
b = torch.randn(5, 7, dtype=torch.double)
print(b.size())
# torch.Size([5, 7])
Note: torch.Size It's actually a tuple (tuple)
Inplace / Out-of-place
tensor All local operations on the are suffixed _
for example ,add It's an ex situ version , add_ It is an in place version
Some operations , for example narrow There is no in place version , And like fill_ There is no easy version for the operation of
Zero Indexing
Pytorch in tensor The index is from 0 At the beginning a = b[0, 3] # from b Select the first 1 That's ok , The first 4 Column
Tensors You can also use Python Slice index for b = a[:, 3:5] # Select all lines , The first 4 And the 5 Column
No Camel Casing Humpless spelling
All function names are no longer spelled hump . for example indexAdd Now rewrite to index_add_
x = torch.ones(5, 5);
# tensor([[ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.],
# [ 1., 1., 1., 1., 1.]])
z = torch.empty(5, 2)
z[:, 0] = 10
z[:, 1] = 100
# tensor([[ 10., 100.],
# [ 10., 100.],
# [ 10., 100.],
# [ 10., 100.],
# [ 10., 100.]])
x.index_add_(1, torch.tensor([4, 0]), z)
# stay x The first dimensional direction of ( horizontal direction ), x The fifth column of ( The index for 4) Add z The first column ,x The first column ( The index for 0) Add z The second column of
# tensor([[ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.],
# [ 101., 1., 1., 1., 11.]])
Numpy Bridge
Numpy array and torch Tensor The transition between them is very simple , Memory addresses are shared between the two , That is, changing one will change the other .
hold torch Tensor convert to numpy Array
a = torch.ones(5)
# tensor([ 1., 1., 1., 1., 1.])
b = a.numpy()
# [1. 1. 1. 1. 1.]
a.add_(1)
# tensor([ 2., 2., 2., 2., 2.])
# [2. 2. 2. 2. 2.]
hold numpy Array convert to torch Tensor
a = np.ones(5)
b = torch.from_numpy(a)
# [1. 1. 1. 1. 1.]
# tensor([ 1., 1., 1., 1., 1.], dtype=torch.float64)
CUDA Tensors
stay Pytorch in CUDA Tensors Simple to use , from CPU Change one CUDA Tensors To GPU Will save its original type
# Let us run this cell only cuda is available
if torch.cuda.is_available():
# create a LongTensor and transfer it
# to GPU as torch.cuda.LongTensor
a = torch.full((10, ), 2, device=torch.device("cuda"))
print(a)
b = a.to(torch.device("cpu"))
# transfer it to CPU, back to
# being a torch.LongTensor
边栏推荐
- Talk about the kotlin cooperation process and the difference between job and supervisorjob
- Coredata acquisition in swift sorting, ascending, descending
- RPC understanding
- Alcohol tester scheme: what principle does the alcohol tester measure alcohol solubility based on?
- 关于Lombok的@Data注解
- Bind threads to run on a specific CPU logical kernel
- Pit encountered by fastjason
- [untitled]
- Explanation on the use of password profiteering cracking tool Hydra
- Find the number that appears only once in the array
猜你喜欢
Baidu map JS browsing terminal
QT connection to Shentong database
Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)
Metasploit practice - SSH brute force cracking process
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
[untitled]
Detailed explanation of pytoch's scatter function
Pytorch BERT
Evaluation standard for audio signal quality of intelligent speakers
Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development
随机推荐
Splice and slice functions of JS
2020-11-02
Talk about the kotlin cooperation process and the difference between job and supervisorjob
Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development
Esp32 (6): Bluetooth and WiFi functions for function development
Interviewer: do you understand the principle of recyclerview layout animation?
Metasploit practice - SSH brute force cracking process
Detectron2 source code reading 3-- encapsulating dataset with mapper
Talking about the difference between kotlin collaboration and thread
icon资源
Introduction to MySQL basics day4 power node [Lao Du] class notes
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
快应用中实现自定义抽屉组件
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Implementing custom drawer component in quick application
Application of hongruan face recognition
Using appbarlayout to realize secondary ceiling function
C#访问MongoDB并执行CRUD操作
Esp32 things (V): analysis of common API of esp32 of Swiss Army knife
Flink Exception -- No ExecutorFactory found to execute the application