(2) 张量

It starts with a tensor

NO.3 Tensors and storages
  • Values are allocated in contiguous chunks of memory, managed by <torch.Storage> instances.
  • A PyTorch Tensor is a view over such a Storage that’s capable of indexing into that storage by using an offset and per-dimension strides.(通过偏移量和每个维度的距离在内存中索引)
  • The underlying memory is allocated only once, however, so creating alternative tensor views on the data can be done quickly, regardless of the size of the data managed by
    the Storage instance.(无论如何改变位置,张量一旦创建,在内存中的位置不会变化)
    Storage
  • You can access the storage for a given tensor by using the .storage property:
import torch
point = torch.tensor([[1,2], [5,4]])
>>> 
tensor([[1, 2],
        [5, 4]])
point.storage()
>>> 1
	2
 	5
 	4
	[torch.LongStorage of size 4]
  • You can also index into a storage manually:
Point_storage = point.storage()
Point_storage[1]
>>> 2
1. You can’t index a storage of a 2D tensor by using two indices.
2. The layout of a storage is always one-dimensional, irrespective 
of the dimensionality of any tensors that may refer to it.
(在内存中只有一个维度)
  • At this point, it shouldn’t come as a surprise that changing the value of a storage changes the content of its referring tensor:
Point_storage[1] = 1000
Point_storage
>>>  1
 	 1000
 	 5
 	 4
	 [torch.LongStorage of size 4]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值