You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"The ___Tensor___ class is probably the most important class in ___Torch___. Almost every package depends on this class. It is __the__ class for handling numeric data. \n",
18
+
"\n",
19
+
"As with pretty much anything in ___Torch___, tensors are serializable and deserializable. What that means is that you can convert a tensor to a string (and save it as a file to disk), and load it back.\n",
20
+
"\n",
21
+
"### Multi-dimensional matrix\n",
22
+
"A ___Tensor___ is a potentially multi-dimensional matrix. The number of dimensions is unlimited that can be created using ___LongStorage___ with more dimensions.\n",
23
+
"\n",
24
+
"Example: "
25
+
]
26
+
},
27
+
{
28
+
"cell_type": "code",
29
+
"collapsed": false,
30
+
"input": [
31
+
"--- creation of a 4D-tensor 4x5x6x2\n",
32
+
"z = torch.Tensor(4,5,6,2)\n",
33
+
"--- for more dimensions, (here a 6D tensor) one can do:\n",
"The number of dimensions of a Tensor can be queried by ___nDimension()___ or ___dim()___. Size of the i-th dimension is returned by ___size(i)___. A ___LongStorage___ containing all the dimensions can be returned by ___size()___."
48
+
]
49
+
},
50
+
{
51
+
"cell_type": "code",
52
+
"collapsed": false,
53
+
"input": [
54
+
"print(x:nDimension())"
55
+
],
56
+
"language": "python",
57
+
"metadata": {},
58
+
"outputs": [
59
+
{
60
+
"metadata": {},
61
+
"output_type": "pyout",
62
+
"prompt_number": 2,
63
+
"text": [
64
+
"6\t\n"
65
+
]
66
+
}
67
+
],
68
+
"prompt_number": 2
69
+
},
70
+
{
71
+
"cell_type": "code",
72
+
"collapsed": false,
73
+
"input": [
74
+
"print(x:size())"
75
+
],
76
+
"language": "python",
77
+
"metadata": {},
78
+
"outputs": [
79
+
{
80
+
"metadata": {},
81
+
"output_type": "pyout",
82
+
"prompt_number": 3,
83
+
"text": [
84
+
"\n",
85
+
" 4\n",
86
+
" 5\n",
87
+
" 6\n",
88
+
" 2\n",
89
+
" 7\n",
90
+
" 3\n",
91
+
"[torch.LongStorage of size 6]\n",
92
+
"\n"
93
+
]
94
+
}
95
+
],
96
+
"prompt_number": 3
97
+
},
98
+
{
99
+
"cell_type": "markdown",
100
+
"metadata": {},
101
+
"source": [
102
+
"### Internal data representation\n",
103
+
"The actual data of a ___Tensor___ is contained into a ___Storage___. It can be accessed using ___storage()___. While the memory of a ___Tensor___ has to be contained in this unique ___Storage___, it might not be __contiguous__: the first position used in the ___Storage___ is given by ___storageOffset()___ (starting at 1). And the jump needed to go from one element to another element in the i-th dimension is given by ___stride(i)___. In other words, given a 3D tensor"
104
+
]
105
+
},
106
+
{
107
+
"cell_type": "code",
108
+
"collapsed": false,
109
+
"input": [
110
+
"x = torch.Tensor(7,7,7)"
111
+
],
112
+
"language": "python",
113
+
"metadata": {},
114
+
"outputs": [],
115
+
"prompt_number": 4
116
+
},
117
+
{
118
+
"cell_type": "markdown",
119
+
"metadata": {},
120
+
"source": [
121
+
"accessing the element (3,4,5) can be done by"
122
+
]
123
+
},
124
+
{
125
+
"cell_type": "code",
126
+
"collapsed": false,
127
+
"input": [
128
+
"x[3][4][5]"
129
+
],
130
+
"language": "python",
131
+
"metadata": {},
132
+
"outputs": [
133
+
{
134
+
"metadata": {},
135
+
"output_type": "pyout",
136
+
"prompt_number": 5,
137
+
"text": [
138
+
"0\t\n"
139
+
]
140
+
}
141
+
],
142
+
"prompt_number": 5
143
+
},
144
+
{
145
+
"cell_type": "markdown",
146
+
"metadata": {},
147
+
"source": [
148
+
"or equivalently under the hood (but slowly!)"
149
+
]
150
+
},
151
+
{
152
+
"cell_type": "code",
153
+
"collapsed": false,
154
+
"input": [
155
+
"x:storage()[x:storageOffset()\n",
156
+
" +(3-1)*x:stride(1)\n",
157
+
" +(4-1)*x:stride(2)\n",
158
+
" +(5-1)*x:stride(3)\n",
159
+
" ]"
160
+
],
161
+
"language": "python",
162
+
"metadata": {},
163
+
"outputs": [
164
+
{
165
+
"metadata": {},
166
+
"output_type": "pyout",
167
+
"prompt_number": 6,
168
+
"text": [
169
+
"0\t\n"
170
+
]
171
+
}
172
+
],
173
+
"prompt_number": 6
174
+
},
175
+
{
176
+
"cell_type": "markdown",
177
+
"metadata": {},
178
+
"source": [
179
+
"One could say that a ___Tensor___ is a particular way of viewing a ___Storage___: a ___Storage___ only represents a chunk of memory, while the ___Tensor___ interprets this chunk of memory as having dimensions:"
180
+
]
181
+
},
182
+
{
183
+
"cell_type": "code",
184
+
"collapsed": false,
185
+
"input": [
186
+
"x = torch.Tensor(4,5)\n",
187
+
"s = x:storage()\n",
188
+
"for i=1,s:size() do -- fill up the Storage\n",
189
+
" s[i] = i\n",
190
+
"end\n",
191
+
"print(x)"
192
+
],
193
+
"language": "python",
194
+
"metadata": {},
195
+
"outputs": [
196
+
{
197
+
"metadata": {},
198
+
"output_type": "pyout",
199
+
"prompt_number": 7,
200
+
"text": [
201
+
" 1 2 3 4 5\n",
202
+
" 6 7 8 9 10\n",
203
+
" 11 12 13 14 15\n",
204
+
" 16 17 18 19 20\n",
205
+
"[torch.DoubleTensor of dimension 4x5]\n",
206
+
"\n"
207
+
]
208
+
}
209
+
],
210
+
"prompt_number": 7
211
+
},
212
+
{
213
+
"cell_type": "markdown",
214
+
"metadata": {},
215
+
"source": [
216
+
"Note also that in Torch elements in the same row [elements along the last dimension] are contiguous in memory for a Tensor:"
217
+
]
218
+
},
219
+
{
220
+
"cell_type": "code",
221
+
"collapsed": false,
222
+
"input": [
223
+
"x = torch.Tensor(4,5)\n",
224
+
"i = 0\n",
225
+
"x:apply(function()\n",
226
+
" i = i + 1\n",
227
+
" return i\n",
228
+
" end)\n",
229
+
"print(x)"
230
+
],
231
+
"language": "python",
232
+
"metadata": {},
233
+
"outputs": [
234
+
{
235
+
"metadata": {},
236
+
"output_type": "pyout",
237
+
"prompt_number": 8,
238
+
"text": [
239
+
" 1 2 3 4 5\n",
240
+
" 6 7 8 9 10\n",
241
+
" 11 12 13 14 15\n",
242
+
" 16 17 18 19 20\n",
243
+
"[torch.DoubleTensor of dimension 4x5]\n",
244
+
"\n"
245
+
]
246
+
}
247
+
],
248
+
"prompt_number": 8
249
+
},
250
+
{
251
+
"cell_type": "code",
252
+
"collapsed": false,
253
+
"input": [
254
+
"x:stride() -- element in the last dimension are contiguous!"
255
+
],
256
+
"language": "python",
257
+
"metadata": {},
258
+
"outputs": [],
259
+
"prompt_number": 9
260
+
},
261
+
{
262
+
"cell_type": "markdown",
263
+
"metadata": {},
264
+
"source": [
265
+
"This is exactly like in __C__ (and not __Fortran__)."
266
+
]
267
+
},
268
+
{
269
+
"cell_type": "markdown",
270
+
"metadata": {},
271
+
"source": [
272
+
"### Tensors of different types\n",
273
+
"Actually, several types of Tensor exist:\n",
274
+
"* ByteTensor -- contains unsigned chars\n",
275
+
"* CharTensor -- contains signed chars\n",
276
+
"* ShortTensor -- contains shorts\n",
277
+
"* IntTensor -- contains ints\n",
278
+
"* FloatTensor -- contains floats\n",
279
+
"* DoubleTensor -- contains doubles\n",
280
+
"\n",
281
+
"Most numeric operations are implemented only for ___FloatTensor___ and ___DoubleTensor___. Other ___Tensor___ types are useful if you want to save memory space."
282
+
]
283
+
},
284
+
{
285
+
"cell_type": "markdown",
286
+
"metadata": {},
287
+
"source": [
288
+
"### Default Tensor type\n",
289
+
"For convenience, an alias ___torch.Tensor___ is provided, which allows the user to write type-independent scripts, which can then ran after choosing the desired ___Tensor___ type with a call like"
290
+
]
291
+
},
292
+
{
293
+
"cell_type": "code",
294
+
"collapsed": false,
295
+
"input": [
296
+
"torch.setdefaulttensortype('torch.FloatTensor')"
297
+
],
298
+
"language": "python",
299
+
"metadata": {},
300
+
"outputs": [
301
+
{
302
+
"metadata": {},
303
+
"output_type": "pyout",
304
+
"prompt_number": 10,
305
+
"text": [
306
+
"\n"
307
+
]
308
+
}
309
+
],
310
+
"prompt_number": 10
311
+
},
312
+
{
313
+
"cell_type": "markdown",
314
+
"metadata": {},
315
+
"source": [
316
+
"See ___torch.setdefaulttensortype___ for more details. By default, the alias \"points\" to ___torch.DoubleTensor___."
0 commit comments