嵌套在自定义Keras图层内部的Keras模型

时间:2019-02-18 18:01:38

标签: keras nested layer

我想建立一个Keras模型,该模型使用类似SPICE的数值方法进行正向传播。由于SPICE问题无法解析解决,因此我建立了以下课程。该类很好地实现了预测(数字正向代理)和确定梯度(解析地)。

班级:

# "..." notes places where code is ommited for conciseness

class SPICE_solver():
    def __init__(self, num_inputs, num_outputs, ...):
    ...
        self.net = build_model_SPICE_solver(num_inputs, num_outputs, ...)

    def predict(self, activations, weights, ...):
        '''
        :param activations:         shape: (?, num_inputs)
        :param weights:             shape: (1, num_inputs, num_outputs)
        :return: vout               shape: (?, num_outputs)
        '''
        ...
        out = np.zeros([activations.shape[0], weights.shape[-1]])
        self.net.fit(x=[activations, weights],
                     y=[out],
                     epochs=200,
                     callbacks=[EarlyStoppingByLossVal(monitor='loss', value=self.acc, verbose=0)],
                     verbose=0,
                     steps_per_epoch=64)
        self.vout = self.net.get_weights()
        return self.vout    # weights incidate the output of the 'layer'

    def gradients(self, activations, weights, ...):
        '''
        :param activations:         shape: (?, num_inputs)
        :param weights:             shape: (?, num_inputs, num_outputs)
        :return: gradient:          list of gradients for: activations, weights (w.r.t. vout)
        '''
        ...
        outputTensor = self.net.output
        listOfVariableTensors = self.net.input
        gradients = K.gradients(outputTensor, listOfVariableTensors)
        sess = tf.InteractiveSession()
        sess.run(tf.initialize_all_variables())
        self.grad = sess.run(gradients, feed_dict={self.net.input[0]:activations, self.net.input[1]:weights})
        return self.grad

我想使用此类在自定义更高级别的Keras层中完成正向传播(SPICE_solver.predict)和向后传播(SPICE_solver.gradients)。

自定义Keras图层:

class mac_nonLin_SPICE(Layer):

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

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel',
                                      shape=(1, int(input_shape[1]), self.output_dim),
                                      initializer='glorot_uniform',
                                      # constraint='UnitNorm',
                                      trainable=True)
        self.slvr = SPICE_solver(int(input_shape[1]), self.output_dim)
        super(mac_nonLin_SPICE, self).build(input_shape)  # Be sure to call this at the end

    def call(self, x):
        return self.slvr.predict(x, self.kernel)

    # def reutrn gradient():????
    # pass

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

我遇到了很多问题,以嵌套方式调用Keras模型。有没有在自定义Keras层中实现此类对象的实用方法?

编辑:我的直觉告诉我,使用底层TensorFlow API重建整个设计是最实用的方法,尽管不方便。仍在寻找一种简便的Keras解决方案。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

简而言之,我无法使用Keras完成此操作。这是我发现的最佳解决方案:

我使用Tensorflow低级API重新创建了网络,并定义了两个损失函数:

  • 损失1:前馈路径的平均误差平方(换句话说,如果loss1高,则SPICE解决方案不好)
  • 损失2:均方误差(预测-training_data)

然后,我将优化器设置为最小化: 损失=损失1 +损失2 *(1-高斯(a *损失1))

位置:

  • Gaus()是高斯函数,归一化为振幅1
  • “ a”是一个因素

这样,仅当Loss1小(SPICE解决方案良好)时,Loss2才会最小化。

希望这对某人有帮助。