我试图写一个基本上只是一个普通前馈层的层(激活(W x + b))。唯一的新颖之处在于,我希望该层包含一维参数矢量(输出尺寸的大小),并在调用时仅输出该一维矢量,而不是实际计算激活值(W x + b )。该向量应该是可训练的。
这是我想出的代码:
from keras import backend as K
from keras.layers import Layer
import keras
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)
self.out_estimate = self.add_weight(name='out_estimate',
shape=(self.output_dim,),
initializer='uniform',
trainable=True)
super(MyLayer, self).build(input_shape) # Be sure to call this at the end
def call(self, x):
return self.out_estimate
def compute_output_shape(self, input_shape):
return (self.output_dim,)
from keras.models import Model
from keras import layers
from keras import Input
input_tensor = layers.Input(shape=(784,))
output_tensor = MyLayer(10)(input_tensor)
model = Model(input_tensor, output_tensor)
model.summary()
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit(train_images, train_labels, epochs=1, batch_size=128)
以下是输出:
ValueError:检查目标时出错:预期my_layer_69具有1个维度,但数组的形状为(60000,10)
答案 0 :(得分:0)
MyLayer
类让__init__
寻找维度。但是您正在发送tesnor
。将tesnor
维度提取到self.output_dim