Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyTorch create_tensor

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use PyTorch create_tensor". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use PyTorch create_tensor.

Course Code 1. Create_tensorimport torchimport numpy as np a = np.ones ((3,3)) print (a, id (a)) b = torch.tensor (a) print (b, id (b), b.device) # b_gpu = torch.tensor (a, device = 'cuda') b_gpu = torch.tensor (a, device =' cpu') print (b_gpu, id (b_gpu), b_gpu.device) c = torch.from_numpy (a) print (c, id (c) a [0 0] = 2print (a, c) c [0,1] = 3print (a, c) d = torch.zeros ((3,3,3)) print (d, d.dtype, d.shape) dd = torch.zeros_like (d) print (d, d.type, d.shape) e = torch.full ((2,2,233) print (e, e.dtype) ee = torch.full ((2,2,233) print (ee, ee.dtype) f = torch.arange (1,5) print (f) F.dtype) ff = torch.arange (1.5.1) print (ff, ff.dtype) g = torch.linspace (1,6,6) print (g, g.dtype) h = torch.normal (0,1,( 3,3) print (h, h.dtype) hh = torch.randn ((3,3) print (hh, hh.dtype) I = torch.rand ((2,2) print (I) ii = torch.randint (1,5, (2)) 2) print (ii) j = torch.randperm (20) print (j, j.dtype) 2. Reshape_tensorimport torch import numpy as np a = torch.arange (0,10, dtype = torch.int64) b = torch.reshape (a, (2,5) print (b) baut = torch.t (b) print (baut, b_T.shape) c = torch.reshape (torch.arange (0,24, dtype = torch.int64), (2,3,4) print (c) d = torch.transpose (c) 0,1) print (d) e = torch.tensor ([1]) print (e, e.shape) f = torch.squeeze (e) print (f, f.shape) f = f * 2print (f, e) ee = torch.unsqueeze (f, dim = 0) print (ee) 3. Concat_split_tensorimport torch import numpy as np T1 = torch.ones ((2,2)) T2 = torch.zeros ((2,2)) a = torch.cat ([T1, T2], dim = 0) print (a) A.shape) b = torch.stack ([T1, T2], dim = 0) print (b, b.shape) print (b [0], b [1]) x = torch.split (b, [1,1], dim = 0) print (type (x)) c, d = xprint (c, d) e = torch.index_select (a, dim = 0, index = torch.tensor ([0,2])) print (e) mask = a.ge (1) f = torch.masked_select (a, mask) print (mask F) 4. Tensor_operator# is familiar with and shows the commonly used tensor operations import torch import numpy as nptorch.manual_seed (10) # datax = torch.rand ((20,1)) * 10y = 2 * x + 5 + torch.randn (20,1) # modelw = torch.tensor (np.asarray ([0.3]), requires_grad=True) b = torch.tensor (np.asarray ([0.]), requires_grad=True) print (w) by univariate linear regression. B) # iterationfor _ in range (1000): # flow y_pre = w * x + b loss = (0.5 * (y_pre-y) * * 2). Mean () # backwords loss.backward (). Data.sub.05 * w.grad) b.data.sub.sub.0.05 * b.grad. Grad.zero.b.grad.zero.b.grad.zero) # show if = 0: print (str (_) + 'loss is') Loss.data.numpy () if loss.data.numpy () < 0.47: breakprint ('finish...') Work

1. Install anaconda,pycharm, CUDA+CuDNN (optional), virtual environment, pytorch, and implement hello pytorch to view the version of pytorch

two。 What is the relationship between tensor and matrix, vector and scalar?

3. What function does Variable "give" to the tensor?

4. Use torch.from_numpy to create tensors and print addresses to view ndarray and tensor data

5. Implement four modes for torch.normal () to create a tensor.

1. Loading environment

Conda create-n torch_p36 python=3.6.5

Conda activate torch_p36

Pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2-f https://download.pytorch.org/whl/torch_stable.html

two。 Conceptual interpretation

Scalar (scalar)

A scalar represents a single number, which is different from most other objects studied in linear algebra.

Vector (vector)

A vector represents an ordered set of numbers. Through the indexes in the order, we can determine each individual number

Matrix (matrix)

A matrix is a collection of objects with the same characteristics and latitudes, which is represented as a two-dimensional data table. Its meaning is that an object is represented as a row in a matrix, a feature is represented as a column in a matrix, and each feature has a numerical value.

Tensor (tensor)

In some cases, we will discuss arrays with more than two-dimensional coordinates. In general, the elements in an array are distributed in a regular grid of several dimensional coordinates, which we call a tensor.

3. Variable "endows" tensor function.

Variable is a data type in torch.autograd, which is mainly used to encapsulate Tensor, so that tensor can derive automatically.

There are five main attributes:

1.data: packaged Tensor

Gradient of 2.grad:data

3.grad_fn: creating the Function of Tensor (the method used to create tensors, such as addition or multiplication) is the key to automatic derivation.

4.requires.grad: indicates whether the tensor needs a gradient. The tensor that does not need a gradient can be set to false.

5.is_leaf: indicates whether the tensor is a leaf node in the calculation graph.

Now that variable doesn't need to be in the code, it's incorporated into tensor.

Tensor

Dtype

Shape

Device

4. Create the tensor import torchimport numpy as np a = np.ones ((3,3)) print (a, id (a)) b = torch.tensor (a) print (b, id (b), b.device) # b_gpu = torch.tensor (a, device = 'cuda') b_gpu = torch.tensor (a, device =' cpu') print (b_gpu, id (b_gpu), b_gpu.device) c = torch.from_numpy (a) print (c, id (c) a [0) 0] = 2print (a, c) c [0,1] = 3print (a, c) d = torch.zeros ((3,3,3)) print (d, d.dtype, d.shape) dd = torch.zeros_like (d) print (d, d.type, d.shape) e = torch.full ((2,2,233) print (e, e.dtype) ee = torch.full ((2,2,233) print (ee, ee.dtype) f = torch.arange (1,5) print (f) F.dtype) ff = torch.arange (1.5.1) print (ff, ff.dtype) g = torch.linspace (1,6,6) print (g, g.dtype) h = torch.normal (0,1,( 3,3) print (h, h.dtype) hh = torch.randn ((3,3) print (hh, hh.dtype) I = torch.rand ((2,2) print (I) ii = torch.randint (1,5, (2)) 2) print (ii) j = torch.randperm (20) print (j, j.dtype) so far I believe you have a deeper understanding of "how to use PyTorch create_tensor". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report