Access operations

Access operations

Accessing elements inside tensors

Suppose we have the following tensors:

> t = torch.tensor([
    [1,2,3],
    [4,5,6],
    [7,8,9]
], dtype=torch.float32)

> t.mean()
tensor(5.)

> t.mean().item()   # we use the item() tensor method for scalar valued tensors to get a number
5.0

Do it with multiple values:

> t.mean(dim=0).tolist()
[4.0, 5.0, 6.0]

> t.mean(dim=0).numpy()
array([4., 5., 6.], dtype=float32)
上一篇:keras_2-1


下一篇:《吴恩达深度学习》学习笔记002_神经网络的编程基础(Basics of Neural Network programming)