From ca57e665c94557176981f10fedfcced9a3629cfc Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Mon, 4 Nov 2024 09:11:57 -0800 Subject: [PATCH 1/5] Redirect tensor tutorial in blitz to QS --- .jenkins/validate_tutorials_built.py | 1 + beginner_source/blitz/tensor_tutorial.py | 197 +---------------------- 2 files changed, 4 insertions(+), 194 deletions(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 2aaa5d6ef71..da2ae37a040 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -11,6 +11,7 @@ NOT_RUN = [ "beginner_source/basics/intro", # no code "beginner_source/introyt/introyt_index", # no code + "beginner_source/blitz/tensor_tutorial", # no code, redirects "beginner_source/onnx/intro_onnx", "beginner_source/profiler", "beginner_source/saving_loading_models", diff --git a/beginner_source/blitz/tensor_tutorial.py b/beginner_source/blitz/tensor_tutorial.py index ac54945bc3a..084c2008c39 100644 --- a/beginner_source/blitz/tensor_tutorial.py +++ b/beginner_source/blitz/tensor_tutorial.py @@ -2,200 +2,9 @@ Tensors ======== -Tensors are a specialized data structure that are very similar to arrays -and matrices. In PyTorch, we use tensors to encode the inputs and -outputs of a model, as well as the model’s parameters. +This page has been moved. -Tensors are similar to NumPy’s ndarrays, except that tensors can run on -GPUs or other specialized hardware to accelerate computing. If you’re familiar with ndarrays, you’ll -be right at home with the Tensor API. If not, follow along in this quick -API walkthrough. +.. raw:: html + """ - -import torch -import numpy as np - - -###################################################################### -# Tensor Initialization -# ~~~~~~~~~~~~~~~~~~~~~ -# -# Tensors can be initialized in various ways. Take a look at the following examples: -# -# **Directly from data** -# -# Tensors can be created directly from data. The data type is automatically inferred. - -data = [[1, 2], [3, 4]] -x_data = torch.tensor(data) - -###################################################################### -# **From a NumPy array** -# -# Tensors can be created from NumPy arrays (and vice versa - see :ref:`bridge-to-np-label`). -np_array = np.array(data) -x_np = torch.from_numpy(np_array) - - -############################################################### -# **From another tensor:** -# -# The new tensor retains the properties (shape, datatype) of the argument tensor, unless explicitly overridden. - -x_ones = torch.ones_like(x_data) # retains the properties of x_data -print(f"Ones Tensor: \n {x_ones} \n") - -x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data -print(f"Random Tensor: \n {x_rand} \n") - - -###################################################################### -# **With random or constant values:** -# -# ``shape`` is a tuple of tensor dimensions. In the functions below, it determines the dimensionality of the output tensor. - -shape = (2, 3,) -rand_tensor = torch.rand(shape) -ones_tensor = torch.ones(shape) -zeros_tensor = torch.zeros(shape) - -print(f"Random Tensor: \n {rand_tensor} \n") -print(f"Ones Tensor: \n {ones_tensor} \n") -print(f"Zeros Tensor: \n {zeros_tensor}") - - - - -###################################################################### -# -------------- -# - - -###################################################################### -# Tensor Attributes -# ~~~~~~~~~~~~~~~~~ -# -# Tensor attributes describe their shape, datatype, and the device on which they are stored. - -tensor = torch.rand(3, 4) - -print(f"Shape of tensor: {tensor.shape}") -print(f"Datatype of tensor: {tensor.dtype}") -print(f"Device tensor is stored on: {tensor.device}") - - -###################################################################### -# -------------- -# - - -###################################################################### -# Tensor Operations -# ~~~~~~~~~~~~~~~~~ -# -# Over 100 tensor operations, including transposing, indexing, slicing, -# mathematical operations, linear algebra, random sampling, and more are -# comprehensively described -# `here `__. -# -# Each of them can be run on the GPU (at typically higher speeds than on a -# CPU). If you’re using Colab, allocate a GPU by going to Edit > Notebook -# Settings. -# - -# We move our tensor to the GPU if available -if torch.cuda.is_available(): - tensor = tensor.to('cuda') - print(f"Device tensor is stored on: {tensor.device}") - - -###################################################################### -# Try out some of the operations from the list. -# If you're familiar with the NumPy API, you'll find the Tensor API a breeze to use. -# - -############################################################### -# **Standard numpy-like indexing and slicing:** - -tensor = torch.ones(4, 4) -tensor[:,1] = 0 -print(tensor) - -###################################################################### -# **Joining tensors** You can use ``torch.cat`` to concatenate a sequence of tensors along a given dimension. -# See also `torch.stack `__, -# another tensor joining op that is subtly different from ``torch.cat``. -t1 = torch.cat([tensor, tensor, tensor], dim=1) -print(t1) - -###################################################################### -# **Multiplying tensors** - -# This computes the element-wise product -print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n") -# Alternative syntax: -print(f"tensor * tensor \n {tensor * tensor}") - -###################################################################### -# -# This computes the matrix multiplication between two tensors -print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n") -# Alternative syntax: -print(f"tensor @ tensor.T \n {tensor @ tensor.T}") - - -###################################################################### -# **In-place operations** -# Operations that have a ``_`` suffix are in-place. For example: ``x.copy_(y)``, ``x.t_()``, will change ``x``. - -print(tensor, "\n") -tensor.add_(5) -print(tensor) - -###################################################################### -# .. note:: -# In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss -# of history. Hence, their use is discouraged. - -###################################################################### -# -------------- -# - - -###################################################################### -# .. _bridge-to-np-label: -# -# Bridge with NumPy -# ~~~~~~~~~~~~~~~~~ -# Tensors on the CPU and NumPy arrays can share their underlying memory -# locations, and changing one will change the other. - - -###################################################################### -# Tensor to NumPy array -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -t = torch.ones(5) -print(f"t: {t}") -n = t.numpy() -print(f"n: {n}") - -###################################################################### -# A change in the tensor reflects in the NumPy array. - -t.add_(1) -print(f"t: {t}") -print(f"n: {n}") - - -###################################################################### -# NumPy array to Tensor -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -n = np.ones(5) -t = torch.from_numpy(n) - -###################################################################### -# Changes in the NumPy array reflects in the tensor. -np.add(n, 1, out=n) -print(f"t: {t}") -print(f"n: {n}") From 258bbc2aa1798d1fd39e1da322ed562b268097ad Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Mon, 4 Nov 2024 12:29:22 -0800 Subject: [PATCH 2/5] Use include --- beginner_source/blitz/README.txt | 2 +- beginner_source/blitz/tensor_tutorial.py | 10 ---------- beginner_source/blitz/tensor_tutorial.rst | 1 + 3 files changed, 2 insertions(+), 11 deletions(-) delete mode 100644 beginner_source/blitz/tensor_tutorial.py create mode 100644 beginner_source/blitz/tensor_tutorial.rst diff --git a/beginner_source/blitz/README.txt b/beginner_source/blitz/README.txt index f529d42e028..d2b918260fb 100644 --- a/beginner_source/blitz/README.txt +++ b/beginner_source/blitz/README.txt @@ -1,7 +1,7 @@ Deep Learning with PyTorch: A 60 Minute Blitz --------------------------------------------- -1. tensor_tutorial.py +1. tensor_tutorial.rst What is PyTorch? https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html diff --git a/beginner_source/blitz/tensor_tutorial.py b/beginner_source/blitz/tensor_tutorial.py deleted file mode 100644 index 084c2008c39..00000000000 --- a/beginner_source/blitz/tensor_tutorial.py +++ /dev/null @@ -1,10 +0,0 @@ -""" -Tensors -======== - -This page has been moved. - -.. raw:: html - - -""" diff --git a/beginner_source/blitz/tensor_tutorial.rst b/beginner_source/blitz/tensor_tutorial.rst new file mode 100644 index 00000000000..738bfd3b493 --- /dev/null +++ b/beginner_source/blitz/tensor_tutorial.rst @@ -0,0 +1 @@ +.. literalinclude:: ../../basics/tensorqs_tutorial.py From 3f0c9b883ab7317a5276d5bf9e473c6ce20ac9bb Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Mon, 4 Nov 2024 16:29:16 -0800 Subject: [PATCH 3/5] Try again --- beginner_source/blitz/tensor_tutorial.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/beginner_source/blitz/tensor_tutorial.rst b/beginner_source/blitz/tensor_tutorial.rst index 738bfd3b493..01e5be31246 100644 --- a/beginner_source/blitz/tensor_tutorial.rst +++ b/beginner_source/blitz/tensor_tutorial.rst @@ -1 +1,5 @@ +Tensors +======= + .. literalinclude:: ../../basics/tensorqs_tutorial.py + :language: python From 4c6eecdea3243992de0efd13bfc8efb98727cdb6 Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Tue, 5 Nov 2024 08:07:07 -0800 Subject: [PATCH 4/5] oh maybe i wasn't finding the file --- beginner_source/blitz/tensor_tutorial.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beginner_source/blitz/tensor_tutorial.rst b/beginner_source/blitz/tensor_tutorial.rst index 01e5be31246..503feb6736e 100644 --- a/beginner_source/blitz/tensor_tutorial.rst +++ b/beginner_source/blitz/tensor_tutorial.rst @@ -1,5 +1,4 @@ Tensors ======= -.. literalinclude:: ../../basics/tensorqs_tutorial.py - :language: python +.. literalinclude:: ../basics/tensorqs_tutorial.py From 6ec8e255c1a154b4b8d568830d93b09d593284f3 Mon Sep 17 00:00:00 2001 From: Jane Xu Date: Tue, 5 Nov 2024 11:43:51 -0800 Subject: [PATCH 5/5] use include and not literalinclude --- beginner_source/blitz/tensor_tutorial.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beginner_source/blitz/tensor_tutorial.rst b/beginner_source/blitz/tensor_tutorial.rst index 503feb6736e..41ec7d0da8c 100644 --- a/beginner_source/blitz/tensor_tutorial.rst +++ b/beginner_source/blitz/tensor_tutorial.rst @@ -1,4 +1,4 @@ Tensors ======= -.. literalinclude:: ../basics/tensorqs_tutorial.py +.. include:: ../basics/tensorqs_tutorial.py