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)
posted @ 2019-06-22 12:43  虔诚的树  阅读(122)  评论(0编辑  收藏  举报