forked from torch/demos
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathslicing.lua
157 lines (117 loc) · 3.38 KB
/
slicing.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
----------------------------------------------------------------------
-- slicing.lua
--
-- This script demonstrates tensor slicing / manipulation.
-- To run this script, simply do:
-- torch 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)