在Keras中使用Tensorflow图层

Bar*_*t C 11 keras tensorflow keras-layer

我一直在尝试使用池化层在Keras中构建顺序模型tf.nn.fractional_max_pool.我知道我可以尝试在Keras中创建自己的自定义图层,但我正在尝试查看是否可以在Tensorflow中使用该图层.对于以下代码段:

p_ratio=[1.0, 1.44, 1.44, 1.0]

model = Sequential()
model.add(ZeroPadding2D((2,2), input_shape=(1, 48, 48)))
model.add(Conv2D(320, (3, 3), activation=PReLU()))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(320, (3, 3), activation=PReLU()))
model.add(InputLayer(input_tensor=tf.nn.fractional_max_pool(model.layers[3].output, p_ratio)))
Run Code Online (Sandbox Code Playgroud)

我收到这个错误.我尝试了其他一些东西Input而不是InputLayerKeras Functional API,但到目前为止还没有运气.

Bar*_*t C 19

得到它的工作.为了将来参考,您需要实现它.由于tf.nn.fractional_max_pool返回3个张量,因此您只需获得第一个:

model.add(InputLayer(input_tensor=tf.nn.fractional_max_pool(model.layers[3].output, p_ratio)[0]))
Run Code Online (Sandbox Code Playgroud)

或使用Lambda图层:

def frac_max_pool(x):
    return tf.nn.fractional_max_pool(x,p_ratio)[0]
Run Code Online (Sandbox Code Playgroud)

模型实现是:

model.add(Lambda(frac_max_pool))
Run Code Online (Sandbox Code Playgroud)

  • 你知道一种使用功能API的方法吗? (3认同)