【问题标题】:Variable length sequences for RNN, using functional keras APIRNN 的可变长度序列,使用功能性 keras API
【发布时间】:2020-11-15 10:53:23
【问题描述】:

我目前正在使用这个实现(在我的自动编码器 RNN 网络中,1 个输入 = 1 个编码器,2 个输出 = 2 个解码器):

input_tensor = Input(shape=(None, features))

enc = LSTM(timesteps, activation = 'tanh', return_sequences = True)(input_tensor)
enc = LSTM(timesteps, activation = 'tanh', return_sequences = False)(enc)
    
decode1 = RepeatVector(timesteps * 2)(enc)
decode1 = LSTM(200, activation = 'tanh', return_sequences = True)(decode1)
decode1 = LSTM(timesteps, activation = 'tanh', return_sequences = True)(decode1)
decode1 = TimeDistributed(Dense(8, activation="softmax"), name="dec1")(decode1)

decode2 = RepeatVector(timesteps)(enc)
decode2 = LSTM(int(timesteps), activation = 'tanh', return_sequences = True)(decode2)
decode2 = TimeDistributed(Dense(2, activation = "tanh"), name="dec2")(decode2)
    
new_model = Model(inputs=input_tensor, outputs = [decode1, decode2])

但这不起作用。我知道在顺序模型中可以有可变的序列长度,但在功能性 keras API 中不可能吗?因为我真的需要 2 个输出解码器……谁能帮忙?

【问题讨论】:

  • 请澄清问题。我重新创建了您的模型,没有任何错误。顺序 API 与 RNN 一起使用 - tensorflow.org/guide/keras/rnn
  • @Andrey 您提供哪些数据类型作为输入?因为我收到错误消息:“无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)”。我知道它适用于 Sequential 模型,但是不可能有 2 个并行输出解码器(或者有吗?)。
  • 我只收到此错误,因为序列的长度可变...

标签: tensorflow machine-learning keras lstm autoencoder


【解决方案1】:

你的模型有效:

import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, RepeatVector, TimeDistributed, Dense
from tensorflow.keras import Model
features = 10
timesteps = 10
input_tensor = Input(shape=(None, features))

enc = LSTM(timesteps, activation = 'tanh', return_sequences = True)(input_tensor)
enc = LSTM(timesteps, activation = 'tanh', return_sequences = False)(enc)
    
decode1 = RepeatVector(timesteps * 2)(enc)
decode1 = LSTM(200, activation = 'tanh', return_sequences = True)(decode1)
decode1 = LSTM(timesteps, activation = 'tanh', return_sequences = True)(decode1)
decode1 = TimeDistributed(Dense(8, activation="softmax"), name="dec1")(decode1)

decode2 = RepeatVector(timesteps)(enc)
decode2 = LSTM(int(timesteps), activation = 'tanh', return_sequences = True)(decode2)
decode2 = TimeDistributed(Dense(2, activation = "tanh"), name="dec2")(decode2)
    
new_model = Model(inputs=input_tensor, outputs = [decode1, decode2])

import numpy as np

#input = tf.random.uniform([100, 10, features], 0, 10, dtype=tf.int32)
input = np.random.uniform(0, 10, [100, 10, features])
output = new_model(input)

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 2016-11-27
    • 2019-04-17
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多