看过keras的人会发现有一个GRUCell,LSTMCell这样的东西,其实就是一个RNN单元,然后可能会有人去用它,但是发现按照常规的方法用的话会报错,这里我提供了一个示例代码,这个东西其实可以和RNN来配合使用:
直接上代码:
from keras.models import Model
from keras.layers import *
encoder_inputs=Input(shape=(20,2048))
cells=[GRUCell(100) for _ in range(20)]
rnn_layer=RNN(cells,return_state=True)(encoder_inputs)
model = Model(inputs=encoder_inputs, outputs=rnn_layer)
print(model.summary())
运行的输出为:
/home/eric/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 20, 2048) 0
_________________________________________________________________
rnn_1 (RNN) [(None, 100), (None, 100) 1790400
=================================================================
Total params: 1,790,400
Trainable params: 1,790,400
Non-trainable params: 0
_________________________________________________________________
None
没有报错啦
参考文献
[1].attention is all you need with keras. https://www.kaggle.com/shujunge/attention-is-all-you-need-with-keras