Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python和Keras创建两层简单RNN模型_Python_Machine Learning_Keras - Fatal编程技术网

使用Python和Keras创建两层简单RNN模型

使用Python和Keras创建两层简单RNN模型,python,machine-learning,keras,Python,Machine Learning,Keras,我正在尝试使用Python中的Keras库为两层SimpleRN模型创建一个模型。我真的不明白keras模型层是如何工作的,所以我不太明白如何得到我想要的结果,或者如何修复我收到的错误 第一个SimpleRN层需要64维并输出状态序列,而第二个层必须具有64维并输出最终状态 我目前有: import tensorflow.keras.models as models import tensorflow.keras.layers as layers model_simple_rnn

我正在尝试使用Python中的Keras库为两层SimpleRN模型创建一个模型。我真的不明白keras模型层是如何工作的,所以我不太明白如何得到我想要的结果,或者如何修复我收到的错误

第一个SimpleRN层需要64维并输出状态序列,而第二个层必须具有64维并输出最终状态

我目前有:

 import tensorflow.keras.models as models
 import tensorflow.keras.layers as layers
    
 model_simple_rnn = models.Sequential([
        layers.Embedding(100,64, name='embedding'),
        layers.SimpleRNN(64, name='simpleRNN1'),
        layers.SimpleRNN(64, name='simpleRNN2'),
        layers.Dense(1, activation='sigmoid', name='dense'),
        ], name='SIMPLERNN')
我得到以下错误:

ValueError: Input 0 of layer simple_rnn_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 64)
我需要有以下输出:

Model: "SIMPLERNN"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, None, 100)         1000000   
_________________________________________________________________
simpleRNN1 (SimpleRNN)       (None, None, 64)          10560     
_________________________________________________________________
simpleRNN2 (SimpleRNN)     (None, 64)                8256      
_________________________________________________________________
dense (Dense)                (None, 1)                 65        
=================================================================
Total params: 1,018,881
Trainable params: 1,018,881
Non-trainable params: 0

在SimpleRN1层中设置return_sequences=True