包含时间序列的Keras CategoryEncoding图层

ipakzgxi  于 2023-02-08  发布在  Go
关注(0)|答案(1)|浏览(93)

对于LSTM,我使用tensorflow.keras.utils.timeseries_dataset_from_array()创建时间序列。对于某些特性,我希望使用Keras预处理层进行one-hot编码。
下面的代码:

n_timesteps = 20
n_categorical_features = 1

from tensorflow.keras.layers import Input, IntegerLookup, CategoryEncoding

cat_inp = keras.layers.Input(shape=(n_timesteps, n_categorical_features), name = "categorical_input")
index = IntegerLookup()
index.adapt(X["br"])
encoder = CategoryEncoding(num_tokens=index.vocabulary_size(), output_mode = "one_hot")(cat_inp)

然而,最后一行给了我错误ValueError: Exception encountered when calling layer "category_encoding_22" (type CategoryEncoding). When output_mode is not 'int' , maximum supported output rank is 2. Received output_mode one_hot and input shape (None, 20, 1), which would result in output rank 3.问题似乎是CategoryEncoding不支持输入Tensor的形状(None,n_timesteps,n_categorical_features)。
如何对timeseries_dataset_from_array()产生的输入Tensor进行one-hot编码?

rdlzhqv9

rdlzhqv91#

请尝试使用TimeDistributed图层:

encoder = tf.keras.layers.TimeDistributed(CategoryEncoding(num_tokens=index.vocabulary_size(), output_mode = "one_hot"))(cat_inp)

它会将CategoryEncoding应用到时间序列中的每个项目。请参阅https://keras.io/api/layers/recurrent_layers/time_distributed/了解更多信息。

相关问题