如何重塑文本数据以适合keras中的LSTM模型
how to reshape text data to be suitable for LSTM model in keras
更新1:
我所指的代码正是书中的代码,你可以找到它here。
唯一的问题是我不想在解码器部分有embed_size
。这就是为什么我认为我根本不需要嵌入层,因为如果我放置嵌入层,我需要在解码器部分有 embed_size
(如果我错了请纠正我)。
总的来说,我试图在不使用嵌入层的情况下采用相同的代码,因为我需要在解码器部分有 vocab_size
。
我认为评论中提供的建议可能是正确的 (using one_hot_encoding
) 我怎么遇到这个错误:
我做的时候 one_hot_encoding
:
tf.keras.backend.one_hot(indices=sent_wids, classes=vocab_size)
我收到这个错误:
in check_num_samples
you should specify the + steps_name + argument
ValueError: If your data is in the form of symbolic tensors, you should specify the steps_per_epoch argument (instead of the batch_size argument, because symbolic tensors are expected to produce batches of input data)
我准备数据的方式是这样的:
sent_lens
的形状是 (87716, 200)
,我想以一种可以将其输入 LSTM 的方式重塑它。
这里 200
代表 sequence_lenght
而 87716
是我拥有的样本数。
下面是 LSTM Autoencoder
:
的代码
inputs = Input(shape=(SEQUENCE_LEN,VOCAB_SIZE), name="input")
encoded = Bidirectional(LSTM(LATENT_SIZE), merge_mode="sum", name="encoder_lstm")(inputs)
decoded = RepeatVector(SEQUENCE_LEN, name="repeater")(encoded)
decoded = LSTM(VOCAB_SIZE, return_sequences=True)(decoded)
autoencoder = Model(inputs, decoded)
autoencoder.compile(optimizer="sgd", loss='mse')
autoencoder.summary()
history = autoencoder.fit(Xtrain, Xtrain,batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS)
我是否还需要做任何额外的事情,如果不需要,为什么我不能得到这个作品?
请让我知道哪一部分我会解释。
感谢您的帮助:)
您需要按以下方式重塑数据:
- 样本。一个序列就是一个样本。一个批次由一个或
更多样品。
- 时间步。一个时间步是一个观察点
在样本中。
- 特征。一个特征是一次一个观察
步骤.
(samples, time_steps, features)
那么您的模型应该如下所示(简化版):
visible = Input(shape=(time_steps, features))
encoder = LSTM(100, activation='relu')(visible)
# define reconstruct decoder
decoder = RepeatVector(time_steps)(encoder)
decoder = LSTM(100, activation='relu', return_sequences=True)(decoder)
decoder = TimeDistributed(Dense(features))(decoder)
model = Model(visible, decoder)
查看 this 很棒的教程。应该对你的情况有帮助。
但是,也就是说你可能只需要 expand the dimensions 个数组。
也检查一下 this 它可能会解决问题。
希望以上内容对您有所帮助。
正如评论中所说,事实证明我只需要做 one_hot_encoding
。
当我使用 tf.keras.backend 进行 one_hot 编码时,它会抛出我在问题中更新的错误。
然后我尝试了 to_categorical(sent_wids, num_classes=VOCAB_SIZE)
并且它修复了(但是面对 memory error
:D 这是不同的故事)!!!
我还应该提一下,我尝试了 sparse_categorical_crossentropy
而不是 one_hot_encoding
,但它没有用!
感谢大家的帮助:)
更新1:
我所指的代码正是书中的代码,你可以找到它here。
唯一的问题是我不想在解码器部分有embed_size
。这就是为什么我认为我根本不需要嵌入层,因为如果我放置嵌入层,我需要在解码器部分有 embed_size
(如果我错了请纠正我)。
总的来说,我试图在不使用嵌入层的情况下采用相同的代码,因为我需要在解码器部分有 vocab_size
。
我认为评论中提供的建议可能是正确的 (using one_hot_encoding
) 我怎么遇到这个错误:
我做的时候 one_hot_encoding
:
tf.keras.backend.one_hot(indices=sent_wids, classes=vocab_size)
我收到这个错误:
in check_num_samples
you should specify the + steps_name + argument
ValueError: If your data is in the form of symbolic tensors, you should specify the steps_per_epoch argument (instead of the batch_size argument, because symbolic tensors are expected to produce batches of input data)
我准备数据的方式是这样的:
sent_lens
的形状是 (87716, 200)
,我想以一种可以将其输入 LSTM 的方式重塑它。
这里 200
代表 sequence_lenght
而 87716
是我拥有的样本数。
下面是 LSTM Autoencoder
:
inputs = Input(shape=(SEQUENCE_LEN,VOCAB_SIZE), name="input")
encoded = Bidirectional(LSTM(LATENT_SIZE), merge_mode="sum", name="encoder_lstm")(inputs)
decoded = RepeatVector(SEQUENCE_LEN, name="repeater")(encoded)
decoded = LSTM(VOCAB_SIZE, return_sequences=True)(decoded)
autoencoder = Model(inputs, decoded)
autoencoder.compile(optimizer="sgd", loss='mse')
autoencoder.summary()
history = autoencoder.fit(Xtrain, Xtrain,batch_size=BATCH_SIZE,
epochs=NUM_EPOCHS)
我是否还需要做任何额外的事情,如果不需要,为什么我不能得到这个作品?
请让我知道哪一部分我会解释。
感谢您的帮助:)
您需要按以下方式重塑数据:
- 样本。一个序列就是一个样本。一个批次由一个或 更多样品。
- 时间步。一个时间步是一个观察点 在样本中。
- 特征。一个特征是一次一个观察 步骤.
(samples, time_steps, features)
那么您的模型应该如下所示(简化版):
visible = Input(shape=(time_steps, features))
encoder = LSTM(100, activation='relu')(visible)
# define reconstruct decoder
decoder = RepeatVector(time_steps)(encoder)
decoder = LSTM(100, activation='relu', return_sequences=True)(decoder)
decoder = TimeDistributed(Dense(features))(decoder)
model = Model(visible, decoder)
查看 this 很棒的教程。应该对你的情况有帮助。
但是,也就是说你可能只需要 expand the dimensions 个数组。
也检查一下 this 它可能会解决问题。
希望以上内容对您有所帮助。
正如评论中所说,事实证明我只需要做 one_hot_encoding
。
当我使用 tf.keras.backend 进行 one_hot 编码时,它会抛出我在问题中更新的错误。
然后我尝试了 to_categorical(sent_wids, num_classes=VOCAB_SIZE)
并且它修复了(但是面对 memory error
:D 这是不同的故事)!!!
我还应该提一下,我尝试了 sparse_categorical_crossentropy
而不是 one_hot_encoding
,但它没有用!
感谢大家的帮助:)