Torch7 Tensor slicing

----------------------------------------------------------------------
-- slicing.lua
-- 
-- This script demonstrates tensor slicing / manipulation.

-- To run this script, simply do:
-- $ th A_slicing.lua
-- and then press 'y' or 'return' at each step, to keep going.

-- little function to pause execution, and request user input
function next()
   local answer = nil
   while answer ~= '' and answer ~= 'y' and answer ~= 'Y' and neverstall ~= true do
      io.write("continue ([y]/n/!)? ")
      io.flush()
      answer=io.read()
      if answer == '!' then
         neverstall = true
      end
      if answer == 'n' then
         print('exiting...')
         os.exit()
      end
   end
   print ''
end

print '----------------------------------------------------------------------'
print 'creating a few tensors'

t1 = torch.range(1,75):resize(3,5,5)
print 't1 = torch.range(1,75):resize(3,5,5)'
print 't1 = '
print(t1)

t2 = torch.range(1,25):resize(5,5)
print 't2 = torch.range(1,25):resize(5,5)'
print 't2 = '
print(t2)

print 'done.'
print ''

next()
print '----------------------------------------------------------------------'
print 'the most basic slicing is done using the [] operator'
print ''

print 't1 ='
print( t1 )

print 't1[2] ='
print( t1[2] )

next()
print '----------------------------------------------------------------------'
print 't1_1 is a view in the existing t1 tensor: changing the values'
print 'in t1_1 directly affects t1:'
print ''

t1[2]:fill(7)
print 't1[2]:fill(7)'

print 't1[2] ='
print( t1[2] )

print 't1 ='
print( t1 )

next()
print '----------------------------------------------------------------------'
print 'more complex slicing can be done using the [{}] operator'
print 'this operator lets you specify one list/number per dimension'
print 'for example, t2 is a 2-dimensional tensor, therefore'
print 'we should pass 2 lists/numbers to the [{}] operator:'
print ''

t2_slice1 = t2[{ {},2 }]
t2_slice2 = t2[{ 2,{} }]      -- equivalent to t2[2]
t2_slice3 = t2[{ {2},{} }]
t2_slice4 = t2[{ {1,3},{3,4} }]
t2_slice5 = t2[{ {3},{4} }]
t2_slice6 = t2[{ 3,4 }]

print 't2 = '
print(t2)

print 't2[{ {},2 }] ='
print(t2_slice1)

print 't2[{ 2,{} }] ='
print(t2_slice2)

print 't2[{ {2},{} }] ='
print(t2_slice3)

print 't2[{ {1,3},{3,4} }] ='
print(t2_slice4)

print 't2[{ {3},{4} }] ='
print(t2_slice5)

print 't2[{ 3,4 }] ='
print(t2_slice6)

next()
print '----------------------------------------------------------------------'
print 'negative indexes can also be used:'
print ''

t2_slice7 = t2[{ {},{2,-2} }]
t2_slice8 = t2[{ -1,-1 }]

print 't2[{ {},{2,-2} }] ='
print(t2_slice7)

print 't2[{ -1,-1 }] ='
print(t2_slice8)

next()
print '----------------------------------------------------------------------'
print 'in basic Lua, the = operator cannot be overloaded (that speeds up the language parser'
print 'a lot...), but you can use the [{}] operator to copy tensors, and subtensors:'
print ''

print 't3 = torch.Tensor(5)'
print 't3[{}] = t2[{ {},1 }]'

t3 = torch.Tensor(5)
t3[{}] = t2[{ {},1 }]

print 't3 ='
print(t3)

next()
print '----------------------------------------------------------------------'
print 'if you need to slice arbitrary subtensors, you will need to do it in steps:'
print ''

t4 = torch.Tensor(5,2)
t4[{ {},1 }] = t2[{ {},2 }]
t4[{ {},2 }] = t2[{ {},5 }]

print [[
t4 = torch.Tensor(5,2)
t4[{ {},1 }] = t2[{ {},2 }]
t4[{ {},2 }] = t2[{ {},5 }]
]]

print 't4 ='
print(t4)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值