How to wrap a tensorflow object as Keras layer?

我是研究僧i 提交于 2019-12-22 14:01:32

问题


I would like to implement Hierarchical Multiscale LSTM as a Keras layer.
It was published here and implemented in tensorflow here.
My understanding is that there's a way to wrap such a tensorflow object in Keras as a layer. I'm not sure how complicated it is but I think it's feasible. Can you help me how to do it?


回答1:


This is usually done by implementing a custom Layer. To be more specific, you should inherit from keras.engine.topology.layer and provide a custom implementation for the following methods (and place the TensorFlow code within them):

  • build(input_shape): this is where you will define your weights. This method must set self.built = True, which can be done by calling super([Layer], self).build()
  • call(x): this is where the layer's logic lives. Unless you want your layer to support masking, you only have to care about the first argument passed to call: the input tensor.
  • compute_output_shape(input_shape): in case your layer modifies the shape of its input, you should specify here the shape transformation logic. This allows Keras to do automatic shape inference.

Since you're trying to implement a recurrent layer, it would also be convenient to inherit directly from keras.legacy.layers.recurrent. In this case, you probably do not need to redefine compute_output_shape(input_shape). If your layer needs additional arguments, you can pass them to the __init__ method of your custom layer.



来源:https://stackoverflow.com/questions/48054009/how-to-wrap-a-tensorflow-object-as-keras-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!