Skip to content

Commit 7d6dcf9

Browse files
committed
fix RNN LSTM GRU
1 parent 7e3c77c commit 7d6dcf9

File tree

5 files changed

+25
-82
lines changed

5 files changed

+25
-82
lines changed

examples/basic_tutorials/imdb_LSTM_simple.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# The same set of code can switch the backend with one line
55
import os
66
# os.environ['TL_BACKEND'] = 'tensorflow'
7-
# os.environ['TL_BACKEND'] = 'mindspore'
7+
os.environ['TL_BACKEND'] = 'mindspore'
88
# os.environ['TL_BACKEND'] = 'paddle'
9-
os.environ['TL_BACKEND'] = 'torch'
9+
# os.environ['TL_BACKEND'] = 'torch'
1010
import tensorlayerx as tlx
1111
from tensorlayerx.nn import Module
1212
from tensorlayerx.nn import Linear, LSTM, Embedding
@@ -42,7 +42,7 @@ class ImdbNet(Module):
4242
def __init__(self):
4343
super(ImdbNet, self).__init__()
4444
self.embedding = Embedding(num_embeddings=vocab_size, embedding_dim=64)
45-
self.lstm = LSTM(input_size=64, hidden_size=64, num_layers=2)
45+
self.lstm = LSTM(input_size=64, hidden_size=64)
4646
self.linear1 = Linear(in_features=64, out_features=64, act=tlx.nn.ReLU)
4747
self.linear2 = Linear(in_features=64, out_features=2)
4848

tensorlayerx/backend/ops/paddle_nn.py

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
import paddle as pd
5+
import paddle.nn
56
from paddle import framework
67
import paddle.nn.functional as F
78
import numpy as np
@@ -13,7 +14,7 @@
1314
from paddle.nn.layer.rnn import RNNCellBase
1415
import warnings
1516
import math
16-
17+
from paddle import _C_ops
1718

1819
def padding_format(padding):
1920
"""
@@ -1503,7 +1504,6 @@ def concat_states(states, bidirectional=False, state_components=1):
15031504
componnets.append(states[i::state_components])
15041505
return tuple([pd.stack(item) for item in componnets])
15051506

1506-
15071507
class rnnbase(LayerList):
15081508

15091509
def __init__(
@@ -1539,7 +1539,6 @@ def __init__(
15391539
self.bias = bias
15401540
RNN = pd.nn.RNN
15411541
BiRNN = pd.nn.BiRNN
1542-
15431542
kwargs = {"weight_ih_attr": None, "weight_hh_attr": None, "bias_ih_attr": self.bias, "bias_hh_attr": self.bias}
15441543
act = None
15451544
rnn_cls = None
@@ -1618,16 +1617,11 @@ def __init__(
16181617

16191618
def flatten_parameters(self):
16201619
if self.could_use_cudnn:
1621-
params = self.parameters(include_sublayers=False)
1622-
shape = [np.prod(param.shape) for param in params]
1623-
self._all_weights = [None] * len(params)
1624-
for i, param in enumerate(params):
1625-
offset = 0 if i % 4 < 2 else (2 * self.num_layers * self.bidirect)
1626-
layer_idx = i // 4
1627-
self._all_weights[offset + layer_idx * 2 + i % 2] = param
1620+
self._all_weights = self.parameters(include_sublayers=False)
1621+
shape = [np.prod(param.shape) for param in self._all_weights]
16281622
self._flat_weight = [
16291623
self.create_parameter(
1630-
shape=[np.sum(shape)], dtype=params[0].dtype, default_initializer=I.Constant(0.0)
1624+
shape=[np.sum(shape)], dtype=self._all_weights[0].dtype, default_initializer=I.Constant(0.0)
16311625
)
16321626
]
16331627
self._dropout_state = self.create_variable(dtype=fluid.core.VarDesc.VarType.UINT8)
@@ -1640,42 +1634,20 @@ def flatten_parameters(self):
16401634
}, attrs={
16411635
"copy_data": True,
16421636
"use_align": False,
1643-
"dtype": params[0].dtype
1637+
"dtype": self._all_weights[0].dtype
16441638
}
16451639
)
16461640

16471641
def _cudnn_impl(self, inputs, initial_states, sequence_length):
16481642
if not self.time_major:
16491643
inputs = pd.tensor.transpose(inputs, [1, 0, 2])
1650-
out = self._helper.create_variable_for_type_inference(inputs.dtype)
1651-
state = [self._helper.create_variable_for_type_inference(inputs.dtype) for i in range(self.state_components)]
1652-
reserve = self._helper.create_variable_for_type_inference(
1653-
dtype=fluid.core.VarDesc.VarType.UINT8, stop_gradient=True
1654-
)
1655-
1656-
inputs = {
1657-
'Input': inputs,
1658-
'WeightList': self._all_weights,
1659-
'PreState': initial_states,
1660-
'SequenceLength': sequence_length
1661-
}
1662-
attrs = {
1663-
'dropout_prob': self.dropout,
1664-
'is_bidirec': self.bidirect == 2,
1665-
'input_size': self.input_size,
1666-
'hidden_size': self.hidden_size,
1667-
'num_layers': self.num_layers,
1668-
'mode': self.mode,
1669-
'is_test': not self.training
1670-
}
1671-
1672-
outputs = {
1673-
'Out': out,
1674-
'State': state,
1675-
'Reserve': reserve,
1676-
'DropoutState': self._dropout_state,
1677-
}
1678-
self._helper.append_op(type="rnn", inputs=inputs, outputs=outputs, attrs=attrs)
1644+
_, _, out, state = _C_ops.rnn(
1645+
inputs, initial_states, self._all_weights, sequence_length,
1646+
self._dropout_state, self.state_components, 'dropout_prob',
1647+
self.dropout, 'is_bidirec', self.bidirect == 2,
1648+
'input_size', self.input_size, 'hidden_size', self.hidden_size,
1649+
'num_layers', self.num_layers, 'mode', self.mode, 'is_test',
1650+
not self.training)
16791651
out = pd.tensor.transpose(out, [1, 0, 2]) if not self.time_major else out
16801652
return out, tuple(state) if len(state) > 1 else state[0]
16811653

tensorlayerx/backend/ops/tensorflow_nn.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,6 @@ def __init__(
21832183
self.w_hh = w_hh
21842184
self.b_ih = b_ih
21852185
self.b_hh = b_hh
2186-
21872186
self.act_fn = None
21882187
if mode == 'LSTM':
21892188
# gate_size = 4 * hidden_size

tensorlayerx/nn/core/core_tensorflow.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ def __setattr__(self, name, value):
146146
raise TypeError("Expected type is Module, but got Parameter.")
147147
self.insert_param_to_layer(name, value)
148148

149-
elif isinstance(value, ParameterTuple):
150-
self.set_attr_for_parameter_tuple(name, value)
151-
152149
elif isinstance(value, Module):
153150
if layers is None:
154151
raise AttributeError("Can not assign layers before Module.__init__() call.")
@@ -297,27 +294,6 @@ def _compute_shape(tensors):
297294
shape_mem = tlx.get_tensor_shape(tensors)
298295
return shape_mem
299296

300-
def set_attr_for_parameter_tuple(self, name, value):
301-
"""Set attr for parameter in ParameterTuple."""
302-
params = self.__dict__.get('_params')
303-
params_tuple = self.__dict__.get('_params_tuple')
304-
if params is None:
305-
raise AttributeError("For 'Module', can not assign params before Module.__init__() is called.")
306-
exist_names = set("")
307-
308-
for item in value:
309-
self.insert_param_to_layer(item.name, item, check_name=False)
310-
if item.name in exist_names:
311-
raise ValueError("The value {} , its name '{}' already exists.".
312-
format(value, item.name))
313-
exist_names.add(item.name)
314-
315-
if name in self.__dict__:
316-
del self.__dict__[name]
317-
if name in params:
318-
del params[name]
319-
params_tuple[name] = value
320-
321297
def insert_param_to_layer(self, param_name, param, check_name=True):
322298
"""
323299
Adds a parameter to the current layer.

tensorlayerx/nn/layers/recurrent.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,10 @@ def __repr__(self):
448448

449449
def build(self, inputs_shape):
450450
bidirect = 2 if self.bidirectional else 1
451-
self.w_ih = []
452-
self.w_hh = []
453-
self.b_ih = []
454-
self.b_hh = []
451+
self.weight_ih = ParameterList()
452+
self.weight_hh = ParameterList()
453+
self.bias_ih = ParameterList()
454+
self.bias_hh =ParameterList()
455455
stdv = 1.0 / np.sqrt(self.hidden_size)
456456
_init = tlx.nn.initializers.RandomUniform(minval=-stdv, maxval=stdv)
457457
if self.mode == 'LSTM':
@@ -465,37 +465,33 @@ def build(self, inputs_shape):
465465
layer_input_size = self.input_size if layer == 0 else self.hidden_size * bidirect
466466
suffix = '_reverse' if direction == 1 else ''
467467

468-
self.w_ih.append(
468+
self.weight_ih.append(
469469
self._get_weights(
470470
var_name='weight_ih_l{}{}'.format(layer, suffix), shape=(gate_size, layer_input_size),
471471
init=_init
472472
)
473473
)
474-
self.w_hh.append(
474+
self.weight_hh.append(
475475
self._get_weights(
476476
var_name='weight_hh_l{}{}'.format(layer, suffix), shape=(gate_size, self.hidden_size),
477477
init=_init
478478
)
479479
)
480480
if self.bias:
481-
self.b_ih.append(
481+
self.bias_ih.append(
482482
self._get_weights(
483483
var_name='bias_ih_l{}{}'.format(layer, suffix), shape=(gate_size, ), init=_init
484484
)
485485
)
486-
self.b_hh.append(
486+
self.bias_hh.append(
487487
self._get_weights(
488488
var_name='bias_hh_l{}{}'.format(layer, suffix), shape=(gate_size, ), init=_init
489489
)
490490
)
491-
self.weight_ih = ParameterList(self.w_ih)
492-
self.weight_hh = ParameterList(self.w_hh)
493-
self.bias_ih = ParameterList(self.b_ih)
494-
self.bias_hh =ParameterList(self.b_hh)
495491
self.rnn = tlx.ops.rnnbase(
496492
mode=self.mode, input_size=self.input_size, hidden_size=self.hidden_size, num_layers=self.num_layers,
497493
bias=self.bias, batch_first=self.batch_first, dropout=self.dropout, bidirectional=self.bidirectional,
498-
is_train=self.is_train, w_ih=self.w_ih, w_hh=self.w_hh, b_ih=self.b_ih, b_hh=self.b_hh
494+
is_train=self.is_train, w_ih=self.weight_ih, w_hh=self.weight_hh, b_ih=self.bias_ih, b_hh=self.bias_hh
499495
)
500496

501497
def forward(self, input, states=None):

0 commit comments

Comments
 (0)