【发布时间】: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