5

我想在 Keras 中实现时空全卷积网络(STFCN)。我需要将 3D 卷积输出的每个深度列(例如带有 shape 的张量(64, 16, 16))作为单独的 LSTM 的输入。

为了清楚起见,我有一个(64 x 16 x 16)张量尺寸(channels, height, width)。我需要将张量(显式或隐式)拆分为 16 * 16 = 256 张量的 shape (64 x 1 x 1)

这是 STFCN 论文中用于说明时空模块的图表。我上面描述的是“空间特征”和“时空模块”之间的箭头。

FCn 和时空模块之间的连接是图中的相关部分。

这个想法如何在 Keras 中得到最好的实施?

4

1 回答 1

6

您可以使用tf.splitKerasLambda层从 Tensorflow 使用

使用 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)
于 2017-11-08T16:36:49.927 回答