🎓 A1Learner
🔍
0 of 79 lessons completed

PyTorch Copying Tensors

A slice is a view, a window onto the same values as the original tensor. clone() makes a true copy with its own values.

Edit a row, change the table

A row of a table is a view onto the table's values. Writing through the row writes into the table.

import torch

table = torch.tensor([[1, 2], [3, 4]])
row = table[0]
row[1] = 20
print(table)

A slice is a view too

The first two values form a view, not a copy. Changing the view changes the original row.

scores = torch.tensor([10, 20, 30, 40])
first_two = scores[:2]
first_two[0] = 99
print(scores)

Clone makes a true copy

clone() copies the values into a new tensor. Editing the clone leaves the original alone.

scores = torch.tensor([10, 20, 30])
copy = scores.clone()
copy[0] = 99
print(scores)
print(copy)

Example

import torch

table = torch.tensor([[1, 2],
                      [3, 4]])
row = table[0]
row[0] = 99
print("After editing the slice:")
print(table)

scores = torch.tensor([10, 20, 30])
safe = scores.clone()
safe[0] = 99
print("Original after editing the clone:")
print(scores)
print("Clone:")
print(safe)

Key points

  • View: a tensor that shares the same values as another tensor
  • Slices and rows are views, not copies
  • Editing a view edits the original tensor too
  • Use clone() to make a copy with its own values
  • Editing a clone leaves the original tensor unchanged
  • In short, a view shares values and a clone owns its values

In this example, table starts with four small numbers. row takes the first row of the table, and rows are views. Setting row[0] to 99 changes the table itself, which the print shows. scores then starts fresh with three values. clone() copies those values into safe. Setting safe[0] to 99 changes only the clone. The last prints show the original still starts with 10.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What happens when a value in a slice is changed?