解:
(1)Create a tensor a from list(range(9)). Predict and then check the size, offset, and stride.
创建列表a
将其转化为张量
a.size:The size (or shape, in NumPy parlance) is a tuple indicating how many elements across each dimension the tensor represents.
a.offset:The storage offset is the index in the storage corresponding to the first element in the tensor.
a.stride:The stride is the number of elements in the storage that need to be skipped over to obtain the next element along each dimension.
回顾:
(2)Create a new tensor using b = a.view(3, 3). What does view do? Check that a and b share the same storage.
A PyTorch Tensor instance is a view of such a Storage instance that is capable of indexing into that storage using an offset and per-dimension strides.
a.storage()与b.storage()均为
id(a.storage())==id(b.storage())
因此对b的修改也会影响a
例如:
(下文中a和b的值不修改,仍为0~8)
回顾:
(3)Create a tensor c = b[1:,1:]. Predict and then check the size, offset, and stride.
c取第1行及以后得行,和第1列及以后得列
c.size
c.storage()
c.offset
c的第一个元素4存储在内存地址4的位置
c.stride
因此从4到7需要3
从4到5需要1
注:
切片可以看成是指向内存地址的指针,对c的修改也会影响b
2.
解:
若出现报错可尝试将a整型改为浮点型,再进行数学运算
cos官网
sqrt官网