作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Keras 中实现时空全卷积网络(STFCN)。我需要输入 3D 卷积输出的每个深度列,例如形状为 (64, 16, 16)
的张量,作为单独 LSTM 的输入。
为了清楚起见,我有一个 (64 x 16 x 16)
维度张量 (channels, height, width)
.我需要将张量(显式或隐式)拆分为 16 * 16 = 256 个形状的张量 (64 x 1 x 1)
.
这是 STFCN 论文中用于说明时空模块的图表。我上面描述的是“空间特征”和“时空模块”之间的箭头。
这个想法如何在 Keras 中得到最好的实现?
最佳答案
您可以使用 tf.split
来自使用 Keras 的 Tensorflow Lambda
层
使用 Lambda 分割形状为 (64,16,16)
的张量进入 (64,1,1,256)
然后子集您需要的任何索引。
import numpy as np
import tensorflow as tf
import keras.backend as K
from keras.models import Model
from keras.layers import Input, Lambda
# input data
data = np.ones((3,64,16,16))
# define lambda function to split
def lambda_fun(x) :
x = K.expand_dims(x, 4)
split1 = tf.split(x, 16, 2)
x = K.concatenate(split1, 4)
split2 = tf.split(x, 16, 3)
x = K.concatenate(split2, 4)
return x
## check thet splitting works fine
input = Input(shape= (64,16,16))
ll = Lambda(lambda_fun)(input)
model = Model(inputs=input, outputs=ll)
res = model.predict(data)
print(np.shape(res)) #(3, 64, 1, 1, 256)
关于deep-learning - 如何在 Keras 中按列拆分张量以实现 STFCN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484413/
我是一名优秀的程序员,十分优秀!