【问题标题】:convert keras.engine.Layer to tensorflow将 keras.engine.Layer 转换为 tensorflow
【发布时间】:2018-04-19 00:27:09
【问题描述】:

大家好,我想将使用 keras 编写的自定义层转换为 tensorflow。

我知道如何编写自定义层 keras,这是 keras 网站的示例:

from keras import backend as K
from keras.engine.topology import Layer
import numpy as np

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
       self.output_dim = output_dim
       super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                  shape=(input_shape[1],   
                                  self.output_dim),
                                  initializer='uniform',
                                  trainable=True)
        super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!

    def call(self, x):
       return K.dot(x, self.kernel)

    def compute_output_shape(self, input_shape):
       return (input_shape[0], self.output_dim)

我想知道是否应该使用类构建我的 tensorflow 自定义层? 以及是否可以举个例子。

【问题讨论】:

    标签: python tensorflow keras layer


    【解决方案1】:

    如果有人遇到同样的问题,我会使用 tf.layers.Layer 和相同的 body。

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 2021-02-06
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多