Last active
April 18, 2019 16:02
-
-
Save trcook/ac50c4076372b17a71b176b0492878a3 to your computer and use it in GitHub Desktop.
create a layer in keras that dynaically adjusts shape according to size of input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# problem: sometimes we want a network that will start with a layer equal to the number of input neurons with fewer and fewer neurons in each subsequent neuron | |
class DenseLayer(k.layers.Layer): | |
def __init__(self): | |
super().__init__() | |
def build(self,input_shape): | |
self.layer_list=[] | |
size=input_shape[-1] | |
print(size) | |
for i in range(3): | |
setattr(self,'%s'%i,k.layers.Dense(size,activation=k.activations.sigmoid, | |
kernel_initializer=k.initializers.glorot_normal)) | |
# using setattr here is key -- when keras builds the layer, it scans for attributes set on the class instance. | |
# You can't just stash a bunch of layers in a list. | |
size=size//2 if size//2>0 else 1 | |
self.layer_list.append("%s"%i) | |
self.out=k.layers.Dense(1) | |
def call(self,Input): | |
x=Input | |
for i in self.layer_list: | |
x=self.__dict__[i](x) | |
x=self.out(x) | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment