在Keras中使用Tensorflow层 [英] Using Tensorflow Layers in Keras

查看:213
本文介绍了在Keras中使用Tensorflow层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to build a sequential model in Keras using the pooling layer tf.nn.fractional_max_pool. I know I could try making my own custom layer in Keras, but I'm trying to see if I can use the layer already in Tensorflow. For the following code snippet:

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)))

我收到此错误.我已经尝试使用Input而不是InputLayer以及Keras Functional API进行其他操作,但是到目前为止还算不上运气.

I get this error. I've tried some other things with Input instead of InputLayer and also the Keras Functional API but so far no luck.

推荐答案

使其正常工作.供将来参考,这是您将需要实现的方式.由于tf.nn.fractional_max_pool返回3个张量,因此您只需获取第一个张量:

Got it to work. For future reference, this is how you would need to implement it. Since tf.nn.fractional_max_pool returns 3 tensors, you need to get the first one only:

model.add(InputLayer(input_tensor=tf.nn.fractional_max_pool(model.layers[3].output, p_ratio)[0]))

或使用Lambda层:

Or using Lambda layer:

def frac_max_pool(x):
    return tf.nn.fractional_max_pool(x,p_ratio)[0]

模型实现为:

model.add(Lambda(frac_max_pool))

这篇关于在Keras中使用Tensorflow层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆