chainer.links.StatelessLSTM¶
-
class
chainer.links.
StatelessLSTM
(in_size, out_size=None, lateral_init=None, upward_init=None, bias_init=None, forget_bias_init=None)[source]¶ Stateless LSTM layer.
This is a fully-connected LSTM layer as a chain. Unlike the
lstm()
function, this chain holds upward and lateral connections as child links. This link doesn’t keep cell and hidden states.Parameters: Variables: - upward (chainer.links.Linear) – Linear layer of upward connections.
- lateral (chainer.links.Linear) – Linear layer of lateral connections.
Example
There are several ways to make a StatelessLSTM link.
Let a two-dimensional input array \(x\), a cell state array \(h\), and the output array of the previous step \(h\) be:
>>> x = np.zeros((1, 10), dtype='f') >>> c = np.zeros((1, 20), dtype='f') >>> h = np.zeros((1, 20), dtype='f')
Give both
in_size
andout_size
arguments:>>> l = L.StatelessLSTM(10, 20) >>> c_new, h_new = l(c, h, x) >>> c_new.shape (1, 20) >>> h_new.shape (1, 20)
Omit
in_size
argument or fill it withNone
:The below two cases are the same.
>>> l = L.StatelessLSTM(20) >>> c_new, h_new = l(c, h, x) >>> c_new.shape (1, 20) >>> h_new.shape (1, 20)
>>> l = L.StatelessLSTM(None, 20) >>> c_new, h_new = l(c, h, x) >>> c_new.shape (1, 20) >>> h_new.shape (1, 20)
Methods
-
__call__
(c, h, x)[source]¶ Returns new cell state and updated output of LSTM.
Parameters: Returns: Returns
(c_new, h_new)
, wherec_new
represents new cell state, andh_new
is updated output of LSTM units.Return type: tuple of ~chainer.Variable
-
add_link
(name, link)[source]¶ Registers a child link to this chain.
Deprecated since version v2.0.0: Assign the child link directly to an attribute within
init_scope()
instead. For example, the following codechain.add_link('l1', L.Linear(3, 5))
can be replaced by the following line.
with chain.init_scope(): chain.l1 = L.Linear(3, 5)
The latter is easier for IDEs to keep track of the attribute’s type.
Parameters:
-
add_param
(name, shape=None, dtype=<class 'numpy.float32'>, initializer=None)[source]¶ Registers a parameter to the link.
Deprecated since version v2.0.0: Assign a
Parameter
object directly to an attribute withininit_scope()
instead. For example, the following codelink.add_param('W', shape=(5, 3))
can be replaced by the following assignment.
with link.init_scope(): link.W = chainer.Parameter(None, (5, 3))
The latter is easier for IDEs to keep track of the attribute’s type.
Parameters: - name (str) – Name of the parameter. This name is also used as the attribute name.
- shape (int or tuple of ints) – Shape of the parameter array. If it is omitted, the parameter variable is left uninitialized.
- dtype – Data type of the parameter array.
- initializer – If it is not
None
, the data is initialized with the given initializer. If it is an array, the data is directly initialized by it. If it is callable, it is used as a weight initializer. Note that in these cases,dtype
argument is ignored.
-
add_persistent
(name, value)[source]¶ Registers a persistent value to the link.
The registered value is saved and loaded on serialization and deserialization. The value is set to an attribute of the link.
Parameters: - name (str) – Name of the persistent value. This name is also used for the attribute name.
- value – Value to be registered.
-
cleargrads
()[source]¶ Clears all gradient arrays.
This method should be called before the backward computation at every iteration of the optimization.
-
disable_update
()[source]¶ Disables update rules of all parameters under the link hierarchy.
This method sets the
enabled
flag of the update rule of each parameter variable toFalse
.
-
enable_update
()[source]¶ Enables update rules of all parameters under the link hierarchy.
This method sets the
enabled
flag of the update rule of each parameter variable toTrue
.
-
init_scope
()[source]¶ Creates an initialization scope.
This method returns a context manager object that enables registration of parameters (and links for
Chain
) by an assignment. AParameter
object can be automatically registered by assigning it to an attribute under this context manager.Example
In most cases, the parameter registration is done in the initializer method. Using the
init_scope
method, we can simply assign aParameter
object to register it to the link.class MyLink(chainer.Link): def __init__(self): super().__init__() with self.init_scope(): self.W = chainer.Parameter(0, (10, 5)) self.b = chainer.Parameter(0, (5,))
-
register_persistent
(name)[source]¶ Registers an attribute of a given name as a persistent value.
This is a convenient method to register an existing attribute as a persistent value. If
name
has been already registered as a parameter, this method removes it from the list of parameter names and re-registers it as a persistent value.Parameters: name (str) – Name of the attribute to be registered.
-
zerograds
()[source]¶ Initializes all gradient arrays by zero.
This method can be used for the same purpose of cleargrads, but less efficient. This method is left for backward compatibility.
Deprecated since version v1.15: Use
cleargrads()
instead.