Skip to content

Commit 134649d

Browse files
committed
Merge pull request torch#364 from Atcold/patch-1
Update utility.md
2 parents c55a062 + 842f90a commit 134649d

File tree

1 file changed

+87
-69
lines changed

1 file changed

+87
-69
lines changed

doc/utility.md

Lines changed: 87 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,107 @@
22
# Torch utility functions #
33

44
This functions are used in all Torch package for creating and handling classes.
5-
The most interesting function is probably [torch.class()](#torch.class) which allows
6-
the user to create easily new classes. [torch.typename()](#torch.typename) might
7-
also be interesting to check what is the class of a given Torch object.
5+
The most interesting function is probably [`torch.class()`](#torch.class) which allows
6+
the user to create easily new classes. [`torch.typename()`](#torch.typename) might
7+
also be interesting to check what is the class of a given *Torch7* object.
88

99
The other functions are more for advanced users.
1010

11+
1112
<a name="torch.class"></a>
1213
### [metatable] torch.class(name, [parentName]) ###
1314

1415
Creates a new `Torch` class called `name`. If `parentName` is provided, the class will inherit
1516
`parentName` methods. A class is a table which has a particular metatable.
1617

1718
If `name` is of the form `package.className` then the class `className` will be added to the specified `package`.
18-
In that case, `package` has to be a valid (and already loaded) package. If `name` does not contain any `"."`,
19+
In that case, `package` has to be a valid (and already loaded) package. If `name` does not contain any `.`,
1920
then the class will be defined in the global environment.
2021

2122
One \[or two\] (meta)tables are returned. These tables contain all the method
2223
provided by the class [and its parent class if it has been provided]. After
2324
a call to `torch.class()` you have to fill-up properly the metatable.
2425

25-
After the class definition is complete, constructing a new class _name_ will be achieved by a call to `_name_()`.
26-
This call will first call the method ```lua__init()``` if it exists, passing all arguments of `_name_()`.
26+
After the class definition is complete, constructing a new class `name` will be achieved by a call to `name()`.
27+
This call will first call the method ```lua__init()``` if it exists, passing all arguments of `name()`.
2728

2829
```lua
29-
require "torch"
30-
31-
-- for naming convenience
32-
do
30+
-- for naming convenience
31+
do
3332
--- creates a class "Foo"
3433
local Foo = torch.class('Foo')
3534

3635
--- the initializer
3736
function Foo:__init()
38-
self.contents = "this is some text"
37+
self.contents = 'this is some text'
3938
end
4039

4140
--- a method
4241
function Foo:print()
43-
print(self.contents)
42+
print(self.contents)
4443
end
4544

4645
--- another one
4746
function Foo:bip()
48-
print('bip')
47+
print('bip')
4948
end
5049

51-
end
50+
end
5251

53-
--- now create an instance of Foo
54-
foo = Foo()
52+
--- now create an instance of Foo
53+
foo = Foo()
5554

56-
--- try it out
57-
foo:print()
55+
--- try it out
56+
foo:print()
5857

59-
--- create a class torch.Bar which
60-
--- inherits from Foo
61-
do
58+
--- create a class torch.Bar which
59+
--- inherits from Foo
60+
do
6261
local Bar, parent = torch.class('torch.Bar', 'Foo')
6362

6463
--- the initializer
6564
function Bar:__init(stuff)
66-
--- call the parent initializer on ourself
67-
parent.__init(self)
65+
--- call the parent initializer on ourself
66+
parent.__init(self)
6867

69-
--- do some stuff
70-
self.stuff = stuff
68+
--- do some stuff
69+
self.stuff = stuff
7170
end
7271

7372
--- a new method
7473
function Bar:boing()
75-
print('boing!')
74+
print('boing!')
7675
end
7776

7877
--- override parent's method
7978
function Bar:print()
80-
print(self.contents)
81-
print(self.stuff)
79+
print(self.contents)
80+
print(self.stuff)
8281
end
83-
end
84-
85-
--- create a new instance and use it
86-
bar = torch.Bar("ha ha!")
87-
bar:print() -- overrided method
88-
bar:boing() -- child method
89-
bar:bip() -- parent's method
82+
end
9083

84+
--- create a new instance and use it
85+
bar = torch.Bar('ha ha!')
86+
bar:print() -- overrided method
87+
bar:boing() -- child method
88+
bar:bip() -- parent's method
9189
```
9290

9391
For advanced users, it is worth mentionning that `torch.class()` actually
94-
calls [torch.newmetatable()](#torch.newmetatable). with a particular
92+
calls [`torch.newmetatable()`](#torch.newmetatable). with a particular
9593
constructor. The constructor creates a Lua table and set the right
9694
metatable on it, and then calls ```lua__init()``` if it exists in the
9795
metatable. It also sets a [factory](#torch.factory) field ```lua__factory``` such that it
9896
is possible to create an empty object of this class.
9997

98+
10099
<a name="torch.type"></a>
101100
### [string] torch.type(object) ###
102101

103102
Checks if `object` has a metatable. If it does, and if it corresponds to a
104103
`Torch` class, then returns a string containing the name of the
105104
class. Otherwise, it returns the Lua `type(object)` of the object.
106-
Unlike [torch.typename()](#torch.typename), all outputs are strings:
105+
Unlike [`torch.typename()`](#torch.typename), all outputs are strings:
107106

108107
```lua
109108
> torch.type(torch.Tensor())
@@ -122,30 +121,33 @@ Checks if `object` has a metatable. If it does, and if it corresponds to a
122121
`Torch` class, then returns a string containing the name of the
123122
class. Returns `nil` in any other cases.
124123

125-
A Torch class is a class created with [torch.class()](#torch.class) or
126-
[torch.newmetatable()](#torch.newmetatable).
124+
A Torch class is a class created with [`torch.class()`](#torch.class) or
125+
[`torch.newmetatable()`](#torch.newmetatable).
126+
127127

128128
<a name="torch.typename2id"></a>
129129
### [userdata] torch.typename2id(string) ###
130130

131131
Given a Torch class name specified by `string`, returns a unique
132132
corresponding id (defined by a `lightuserdata` pointing on the internal
133-
structure of the class). This might be useful to do a _fast_ check of the
134-
class of an object (if used with [torch.id()](#torch.id)), avoiding string
133+
structure of the class). This might be useful to do a *fast* check of the
134+
class of an object (if used with [`torch.id()`](#torch.id)), avoiding string
135135
comparisons.
136136

137137
Returns `nil` if `string` does not specify a Torch object.
138138

139+
139140
<a name="torch.id"></a>
140141
### [userdata] torch.id(object) ###
141142

142-
Returns a unique id corresponding to the _class_ of the given Torch object.
143+
Returns a unique id corresponding to the `class` of the given *Torch7* object.
143144
The id is defined by a `lightuserdata` pointing on the internal structure
144145
of the class.
145146

146147
Returns `nil` if `object` is not a Torch object.
147148

148-
This is different from the _object_ id returned by [torch.pointer()](#torch.pointer).
149+
This is different from the `object` id returned by [`torch.pointer()`](#torch.pointer).
150+
149151

150152
<a name="torch.isTypeOf"></a>
151153
### [boolean] isTypeOf(object, typeSpec) ###
@@ -170,102 +172,115 @@ If the given `constructor` function is not `nil`, then assign to the variable `n
170172
The given `name` might be of the form `package.className`, in which case the `className` will be local to the
171173
specified `package`. In that case, `package` must be a valid and already loaded package.
172174

175+
173176
<a name="torch.factory"></a>
174177
### [function] torch.factory(name) ###
175178

176179
Returns the factory function of the Torch class `name`. If the class name is invalid or if the class
177180
has no factory, then returns `nil`.
178181

179-
A Torch class is a class created with [torch.class()](#torch.class) or
180-
[torch.newmetatable()](#torch.newmetatable).
182+
A Torch class is a class created with [`torch.class()`](#torch.class) or
183+
[`torch.newmetatable()`](#torch.newmetatable).
181184

182185
A factory function is able to return a new (empty) object of its corresponding class. This is helpful for
183186
[object serialization](file.md#torch.File.serialization).
184187

188+
185189
<a name="torch.getmetatable"></a>
186190
### [table] torch.getmetatable(string) ###
187191

188192
Given a `string`, returns a metatable corresponding to the Torch class described
189193
by `string`. Returns `nil` if the class does not exist.
190194

191-
A Torch class is a class created with [torch.class()](#torch.class) or
192-
[torch.newmetatable()](#torch.newmetatable).
195+
A Torch class is a class created with [`torch.class()`](#torch.class) or
196+
[`torch.newmetatable()`](#torch.newmetatable).
193197

194198
Example:
199+
195200
```lua
196-
> for k,v in pairs(torch.getmetatable("torch.CharStorage")) do print(k,v) end
201+
> for k, v in pairs(torch.getmetatable('torch.CharStorage')) do print(k, v) end
202+
197203
__index__ function: 0x1a4ba80
198204
__typename torch.CharStorage
199-
write function: 0x1a49cc0
205+
write function: 0x1a49cc0
200206
__tostring__ function: 0x1a586e0
201207
__newindex__ function: 0x1a4ba40
202-
string function: 0x1a4d860
208+
string function: 0x1a4d860
203209
__version 1
204-
copy function: 0x1a49c80
205-
read function: 0x1a4d840
206-
__len__ function: 0x1a37440
207-
fill function: 0x1a375c0
208-
resize function: 0x1a37580
209-
__index table: 0x1a4a080
210-
size function: 0x1a4ba20
210+
read function: 0x1a4d840
211+
copy function: 0x1a49c80
212+
__len__ function: 0x1a37440
213+
fill function: 0x1a375c0
214+
resize function: 0x1a37580
215+
__index table: 0x1a4a080
216+
size function: 0x1a4ba20
211217
```
212218

219+
213220
<a name="torch.isequal"></a>
214221
### [boolean] torch.isequal(object1, object2) ###
215222

216-
If the two objects given as arguments are `Lua` tables (or Torch objects), then returns `true` if and only if the
223+
If the two objects given as arguments are *Lua* tables (or *Torch7* objects), then returns `true` if and only if the
217224
tables (or Torch objects) have the same address in memory. Returns `false` in any other cases.
218225

219-
A Torch class is a class created with [torch.class()](#TorchClass) or
220-
[torch.newmetatable()](#torch.newmetatable).
226+
A Torch class is a class created with [`torch.class()`](#TorchClass) or
227+
[`torch.newmetatable()`](#torch.newmetatable).
228+
221229

222230
<a name="torch.getdefaulttensortype"></a>
223231
### [string] torch.getdefaulttensortype() ###
224232

225233
Returns a string representing the default tensor type currently in use
226-
by Torch7.
234+
by *Torch7*.
235+
227236

228237
<a name="torch.getenv"></a>
229238
### [table] torch.getenv(function or userdata) ###
230239

231240
Returns the Lua `table` environment of the given `function` or the given
232241
`userdata`. To know more about environments, please read the documentation
233-
of [lua_setfenv()](http://www.lua.org/manual/5.1/manual.html#lua_setfenv)
234-
and [lua_getfenv()](http://www.lua.org/manual/5.1/manual.html#lua_getfenv).
242+
of [`lua_setfenv()`](http://www.lua.org/manual/5.1/manual.html#lua_setfenv)
243+
and [`lua_getfenv()`](http://www.lua.org/manual/5.1/manual.html#lua_getfenv).
244+
235245

236246
<a name="torch.version"></a>
237247
### [number] torch.version(object) ###
238248

239249
Returns the field ```lua__version``` of a given object. This might
240250
be helpful to handle variations in a class over time.
241251

252+
242253
<a name="torch.pointer"></a>
243254
### [number] torch.pointer(object) ###
244255

245-
Returns a unique id (pointer) of the given `object`, which can be a Torch
256+
Returns a unique id (pointer) of the given `object`, which can be a *Torch7*
246257
object, a table, a thread or a function.
247258

248-
This is different from the _class_ id returned by [torch.id()](#torch.id).
259+
This is different from the `class` id returned by [`torch.id()`](#torch.id).
260+
249261

250262
<a name="torch.setdefaulttensortype"></a>
251263
### torch.setdefaulttensortype([typename]) ###
252264

253265
Sets the default tensor type for all the tensors allocated from this
254266
point on. Valid types are:
267+
255268
* `torch.ByteTensor`
256269
* `torch.CharTensor`
257270
* `torch.ShortTensor`
258271
* `torch.IntTensor`
259272
* `torch.FloatTensor`
260273
* `torch.DoubleTensor`
261274

275+
262276
<a name="torch.setenv"></a>
263277
### torch.setenv(function or userdata, table) ###
264278

265279
Assign `table` as the Lua environment of the given `function` or the given
266280
`userdata`. To know more about environments, please read the documentation
267-
of [lua_setfenv()](http://www.lua.org/manual/5.1/manual.html#lua_setfenv)
268-
and [lua_getfenv()](http://www.lua.org/manual/5.1/manual.html#lua_getfenv).
281+
of [`lua_setfenv()`](http://www.lua.org/manual/5.1/manual.html#lua_setfenv)
282+
and [`lua_getfenv()`](http://www.lua.org/manual/5.1/manual.html#lua_getfenv).
283+
269284

270285
<a name="torch.setmetatable"></a>
271286
### [object] torch.setmetatable(table, classname) ###
@@ -274,18 +289,21 @@ Set the metatable of the given `table` to the metatable of the Torch
274289
object named `classname`. This function has to be used with a lot
275290
of care.
276291

292+
277293
<a name="torch.getconstructortable"></a>
278294
### [table] torch.getconstructortable(string) ###
279295

280296
BUGGY
281-
Return the constructor table of the Torch class specified by 'string'.
297+
Return the constructor table of the Torch class specified by `string`.
298+
282299

283300
<a name="torch.totable"></a>
284301
### [table] torch.totable(object) ###
285302

286303
Converts a Tensor or a Storage to a lua table. Also available as methods: `tensor:totable()` and `storage:totable()`.
287304
Multidimensional Tensors are converted to a set of nested tables, matching the shape of the source Tensor.
288-
```
305+
306+
```lua
289307
t7> print(torch.totable(torch.Tensor({1, 2, 3})))
290308
{
291309
1 : 1

0 commit comments

Comments
 (0)