在RNN的每个时间步之前添加完全连接的层

时间:2019-09-01 15:27:28

标签: keras tensorflow2.0

我有一个包含10个功能的多元时间序列,它每天运行14天。我想在LSTM的每个时间步之前添加一个完全连接的层。我想将尺寸减小到5,然后将其馈送到LSTM。

我环顾了stackoverflow,找不到任何明确的解决方案。我基本上是试图从本文http://hanj.cs.illinois.edu/pdf/kdd18_cyang.pdf重建相同的体系结构,其中指出解决方案2:具有活动嵌入的LSTM。

我提供了到目前为止的代码。

我曾经尝试过添加一个嵌入层,但是甚至不知道这是否有意义,因为我不确定该嵌入层是否在LSTM的每个时间步之前执行,并且我无法获得所需的尺寸。我看了有关嵌入层尺寸的文档,但我不太了解。

我之前也曾尝试添加一个Dense层,但是由于我的输入数据是3维的,所以无论如何这都行不通。

import tensorflow as tf
from tensorflow import keras

# Fake data
X = np.random.random(size=(500, 14, 10))
y = np.random.randint(0,2, size=(500))

m = X.shape[0]
timesteps = X.shape[1]
original_num_features = X.shape[2]
reduced_num_features = 5

model = keras.models.Sequential([
    #### not sure if I should be adding as embedding layer here. I want to reduce
    #### the feature dimensions from 10 to 5 with a fully connected layer.
    keras.layers.Embedding(input_dim = [timesteps,original_num_features],
        output_dim = [timesteps, reduced_num_features]),
    keras.layers.LSTM(64, input_shape=[timesteps, reduced_num_features]),
    keras.layers.Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentropy', 
          optimizer='adam', 
          metrics=['binary_accuracy'])


model.fit(X, y, epochs=10, batch_size=32)

我遇到了这个错误,但我什至不确定嵌入会像完全连接的图层一样减小尺寸。

TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ ListWrapper”

0 个答案:

没有答案