python - 使用 Keras 中的 Lambda 函数拆分 LSTM 输出

标签 python tensorflow deep-learning keras keras-layer

我正在使用带有 tensorflow 后端的 Keras。我在 Keras 中使用功能层。我想要实现的是我在某个层具有以下架构。

(20,200)的张量----> LSTM---->分成两个大小各为(20,100)的张量

然后使用这两个张量作为进一步网络的两个分支。 (我们可以认为这与合并操作相反)

据我了解,目前实现这一目标的唯一方法是使用 Lambda 层,因为 Keras 中没有“拆分”功能。

然而,查看 Lambda 函数的文档,似乎只有当我们使用 Keras 时,output_shape 功能才相关。

任何人都可以就如何实现这一目标提供任何建议吗?这是我想要实现的粗略伪代码。

#Other code before this 
lstm_1st_layer = LSTM(nos_hidden_neurons, return_sequences=True)(lstm_0th_layer)
#this layer outputs a tensor of size(20,200)
#Split it into two tensors of size (20,100) each (call them left and right)
left_lstm = LSTM(200, return_sequences=True)(left)
right_lstm = LSTM(200, return_sequences=True)(right)
#Other code after this 

最佳答案

在你的地方,我会简单地使用两个 LSTM 层,单元数量减半。

然后你就可以准备两个输出了:

left = LSTM(half_nos_hidden_neurons,.....)(lstm_0th_layer)   
right = LSTM(half_nos_hidden_neurons,.....)(lstm_0th_layer)

效果是一样的。

关于python - 使用 Keras 中的 Lambda 函数拆分 LSTM 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44914769/

相关文章:

python - uwsgi : why two processes are loaded per each app?

python - linux anaconda环境不安装模块

python - 使用 tensorflow 和 keras 运行 .py 时出错

Tensorflow-js : what is the equivalent of `tf.placeholder` ?

machine-learning - 如何训练纯文本段落并返回关键短语?这可能吗?

Python:查找二维数组中元素半径内点的平均值

python - 将字典与其他字典进行比较,并仅选择其他字典中存在的那些键

hadoop - Tensorflow 和 Hadoop 部署

python - 训练时在图像批数据上添加白噪声

python - 记录 TensorBoard 2.1 正则化损失的推荐方法是什么