DingKe / nn_playground

Experimental keras implementation of novel neural network structures
MIT License
430 stars 152 forks source link

use_bias = True Error in binary_layers.py #13

Open miibotree opened 6 years ago

miibotree commented 6 years ago

If I set use_bias = True in binarynet or xnornet, these vars are not defined: self.output_dim self.bias_initializer self.bias_regularizer self.bias_constraint

according the source code in keras layers/convolutional.py, I modify code as follows:

from keras import regularizers
class BinaryDense(Dense):
    def __init__(self, units, H=1., kernel_lr_multiplier='Glorot', bias_lr_multiplier=None,
                 bias_initializer='zeros', bias_regularizer=None, bias_constraint=None,
                 **kwargs):
        super(BinaryDense, self).__init__(units, **kwargs)
        self.H = H
        self.kernel_lr_multiplier = kernel_lr_multiplier
        self.bias_lr_multiplier = bias_lr_multiplier
        self.bias_initializer = initializers.get(bias_initializer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.bias_constraint = constraints.get(bias_constraint)
        ......
    def build(self, input_shape):
        assert len(input_shape) >= 2
        input_dim = input_shape[1]
        self.output_dim = self.units
       ......
    def get_config(self):
        config = {'H': self.H,
                  'kernel_lr_multiplier': self.kernel_lr_multiplier,
                  'bias_lr_multiplier': self.bias_lr_multiplier,
                  'bias_initializer': initializers.serialize(self.bias_initializer),
                  'bias_regularizer': regularizers.serialize(self.bias_regularizer),
                  'bias_constraint': constraints.serialize(self.bias_constraint)
                  }

so as class BinaryConv2D but set self.output_dim = self.filters