自定义层不训练但不出错

时间:2020-03-27 13:49:56

标签: tensorflow machine-learning keras deep-learning neural-network

我正在尝试实现特定的自定义层。但是,当我运行它时,一切都正常,没有错误,但是在编译和拟合之后,我没有得到“培训”。即,我得到的输出与输入的输入相同...

  class Reconfiguration_unit(K.layers.Layer):
    def __init__(self, *args, **kwargs):
        super(Reconfiguration_unit, self).__init__(*args, **kwargs)
  def build(self, input_shape):
    self.weight = self.add_weight(shape=[input_shape[1],input_shape[1]], 
                                initializer='zeros',
                                trainable=True)
    self.bias = self.add_weight(shape=[input_shape[1],input_shape[1]], 
                                initializer='zeros',
                                trainable=True)
    self.location = self.add_weight(shape=input_shape[1:],
                                initializer='zeros',
                                trainable=True)
    self.scale = self.add_weight(shape=input_shape[1:],
                                initializer='zeros',
                                trainable=True)



def call(self, x):
    # 1. Shift and scale data
    x_shift = x - self.location

    # 2. Rescale componentwise
    x_mod = tf.math.multiply(x_shift,self.scale)

    # 3. Apply bumpy function Component-wise
    x_in_abs = tf.math.abs(x_mod)
    Logic_x_leq1 = tf.math.sign(tf.keras.activations.relu(1-x_in_abs)) # Takes value 1 iff |x|<=1 else 0: since probability of |x|=1 is 0 we should be ok
    x_thresheld = Logic_x_leq1*tf.math.exp(-1/(1-tf.math.pow(x_in_abs,-1))) # Computes bump function at thresholds with previous logic

    # 4+5. Apply Shift (In Tangent Space) and diagonalize
    x_out = tf.linalg.diag(x_thresheld) + self.bias 

    # 6. Multiply by weight matrix (in Tangent Space)
    x_out = tf.matmul(x_out,self.weight) 

    # 7. Apply Matrix Exponential
    x_out = tf.linalg.expm(x_out)

    # 8. Muliply by output of (1)
    x_out = tf.linalg.matvec(x_out,x_shift)

    # 9. Recenter Transformed Data
    x_out = x_out + self.location

    # Return Ouput
    return x_out

1 个答案:

答案 0 :(得分:1)

绝对不要(我是说,永远 ...)将权重初始化为零!您可以使用偏见来做到这一点,但不能使用权重。

看看可用的initializers-我强烈建议使用GlorotUniform(这是Keras core layers的默认初始值设定项)或GlorotNormal

顺便说一句,尚不清楚locationscale参数在做什么-您可能还想用不同于零的值初始化它们。