Tensor
tensor 是torch里最重要的一种数据结构,专门为数值数据设计的
Multi-dimensional matrix
一个tensor 可以认为是一个多维矩阵,我们处理数据的时候经常把数据存储在一个四维矩阵中[N,C,H,W] batchsize大小=N,通道数=C,或者说特征图数目,图像的长宽H*W。不同于C的数组,tensor的下标是从1开始的,而不是C的0
tensor的类型包括了:
–ByteTensor – contains unsigned chars
–CharTensor – contains signed chars
–ShortTensor – contains shorts
–IntTensor – contains ints
–LongTensor – contains longs
–FloatTensor – contains floats
–DoubleTensor – contains doubles
如何定义一个tensor:
x=torch.Tensor(n1,n2,n3,..,nk)
x:size()返回x这个tensor的大小
x:size(n)返回x第n维的大小
x:dim()返回k,x是几维的张量
如何访问一个tensor里的元素
x[n][k],或者采用x:storage()[x:storageOffset()+(n-1)*x:stride(1)+(k-1)*x:stride(2)(更慢)
tensor其实是sotrage的一种特殊的视角,is a particular way of viewing a strogage:a storage only represents a chunk of memory ,while the Tensor interprets this chunk of memory as having dimensions
tensor 赋初值:
x = torch.Tensor(4,5)
s = x:storage()
for i=1,s:size() do -- fill up the Storage
s[i] = i
end
> x -- s is interpreted by x as a 2D matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
[torch.DoubleTensor of dimension 4x5]
x = torch.Tensor(4,5)
i = 0
x:apply(function()
i = i + 1
return i
end)
> x
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
[torch.DoubleTensor of dimension 4x5]
> x:stride()
5
1 -- element in the last dimension are contiguous!
[torch.LongStorage of size 2]
Most numeric operations are implemented only for FloatTensor and DoubleTensor. Other Tensor types are useful if you want to save memory space
使用如下命令设置缺省的tensor类型:
torch.setdefaulttensortype(‘torch.FloatTensor’)
tensor赋值问题
x=torch.Tensor(5):zero()
x:zero()
x:narrow(1,2,3):fill(1)
y = torch.Tensor(x:size()):copy(x)
y = x:clone()
torch.Tensor() create new tensor object,分配新的内存
torch.Tensor(tensor) 类似指针,新创建的tensor和原来的tensor是共享同一块内存的。
x=torch.Tensor(3,4)
y=torch.Tensor(x)
y[3][2]=1 [0.0001s]
x[3][2]
1
x = torch.Tensor(torch.LongStorage({4,4,3,2}))
x:size()
4
4
3
2
torch.Tensor(storage,[storageOffset,sizes,[strides]])
s=torch.Storage(10)
x=torch.Tensor(s,1,torch.LongStorage{2,5}),x是s的另一种排列方式
x,s共享
torch.Tensor(table)
> torch.Tensor({{1,2,3,4}, {5,6,7,8}})
1 2 3 4
5 6 7 8
[torch.DoubleTensor of dimension 2x4]
tensor的操作/manipulate
clone()
y=x:clone(), x和y不共享地址contiguous():如果原来的存储是连续的地址,那么不拷贝,否则拷贝
If the given Tensor contents are contiguous in memory, returns the exact same Tensor (no memory copy).
Otherwise (not contiguous in memory), returns a clone (memory copy).- t()转置
x = torch.Tensor(2,3):fill(1)
> x
1 1 1
1 1 1
[torch.DoubleTensor of dimension 2x3]
-- x is contiguous, so y points to the same thing
y = x:contiguous():fill(2)
> y
2 2 2
2 2 2
[torch.DoubleTensor of dimension 2x3]
-- contents of x have been changed
> x
2 2 2
2 2 2
[torch.DoubleTensor of dimension 2x3]
-- x:t() is not contiguous, so z is a clone
z = x:t():contiguous():fill(3.14)
> z
3.1400 3.1400
3.1400 3.1400
3.1400 3.1400
[torch.DoubleTensor of dimension 3x2]
-- contents of x have not been changed
> x
2 2 2
2 2 2
[torch.DoubleTensor of dimension 2x3]
- type() x:type() torch.type(x) x = torch.Tensor(3):fill(3.14)
- typeAs(tensor)
x = torch.Tensor(3):fill(3.14)
> x
3.1400
3.1400
3.1400
[torch.DoubleTensor of dimension 3]
y = x:type('torch.DoubleTensor')
> y
3.1400
3.1400
3.1400
[torch.DoubleTensor of dimension 3]
-- zero y contents
y:zero()
-- contents of x have been changed
> x
0
0
0
[torch.DoubleTensor of dimension 3]
- isTensor(obj)
tensor大小
- nDimension() dim()
- size(dim) size()
- #self x=torch.Tensor(2,3) #x—>2 ,3
- stride(dim) stride()
- storage()
- isContiguous()
- isSize(storage) tensor跟storage的元素数目是否匹配
- isSameSizeAs(tensor)
- nElement() tensor的元素数目
- storageOffset():
Return the first index (starting at 1) used in the tensor’s storage.
Referencing a tensor to an existing tensor or chunk of memory
浅拷贝,共享同一块内存,no memory copy
y=torch.Storage(10)
x=torch.Tensor()
x:set(y,1,10)
x=torch.Tensor(y,1,10)
–set(Tensor)
x=torch.Tensor(2,5):fill(3.14)
y=torch.Tensor():set(x)
y:zero()
x也是全零
–isSetTo(tensor)
当且仅当,当两个tensor的size,stride,内存地址和offset是一样的
x=torch.Tesor(2,5)
y=torch.Tensor()
y:isSetTo(x)
y:set(x)
y:isSetTo(x)
y:t():isSetTo(x)
-[self] set(storage,[storageOffset,sizes,[strides]])
s=torch.Storage(10):fill(1)
sz=torch.LongStorage({2,5})
x=torch.Tensor()
x:set(s,1,sz)
–深拷贝copy和初始化
[self]copy(tensor)
x=torch.Tensor(4):fill(1)
y=torch.Tensor(2,2):copy(x)
[self]fill(value)
resizeAs()
resize(sizes)
–Extracting sub-Tensors 提取tensor的子tensor
[self]narrow(dim,index,size),浅拷贝
x = torch.Tensor(5,6):zero()
y=x:narrow(1,2,3)
[Tensor]sub(dims,dimle…[,dim4s[,dim4e]])浅拷贝
[Tensor]select(dim,index)
x=torch.Tensor(5,6):zero()
y=x:select(1,2):fill(2)
[Tensor] [{ dim1,dim2,… }] or [{ {dim1s,dim1e}, {dim2s,dim2e} }]
x[{{},{4,5}}
index 深拷贝
x=torch.rand(5,5)
y=x:index(1,torch.LongTensor{3,1})
indexCopy(dim,index,tensor)
indexAdd(dim,index,tensor)
indexFill(dim,index,val)
gather(dim,index)