2016-09-27 3 views
3

Ich baue ein konvolutionelles neurales Netzwerk mit Keras und möchte einen einzelnen Knoten mit der Standardabweichung meiner Daten vor der letzten vollständig verbundenen Schicht hinzufügen.Variablen in Keras verschmelzen

Hier ist ein Mindest Code, um den Fehler zu reproduzieren:

from keras.layers import merge, Input, Dense 
from keras.layers import Convolution1D, Flatten 
from keras import backend as K 

input_img = Input(shape=(64, 4)) 

x = Convolution1D(48, 3, activation='relu', init='he_normal')(input_img) 
x = Flatten()(x) 

std = K.std(input_img, axis=1) 
x = merge([x, std], mode='concat', concat_axis=1) 

output = Dense(100, activation='softmax', init='he_normal')(x) 

Daraus ergibt sich die folgende TypeError:

----------------------------------------------------------------- 
TypeError      Traceback (most recent call last) 
<ipython-input-117-c1289ebe610e> in <module>() 
     6 x = merge([x, std], mode='concat', concat_axis=1) 
     7 
----> 8 output = Dense(100, activation='softmax', init='he_normal')(x) 

/home/ubuntu/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask) 
    486          '`layer.build(batch_input_shape)`') 
    487    if len(input_shapes) == 1: 
--> 488     self.build(input_shapes[0]) 
    489    else: 
    490     self.build(input_shapes) 

/home/ubuntu/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/layers/core.pyc in build(self, input_shape) 
    701 
    702   self.W = self.init((input_dim, self.output_dim), 
--> 703       name='{}_W'.format(self.name)) 
    704   if self.bias: 
    705    self.b = K.zeros((self.output_dim,), 

/home/ubuntu/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/initializations.pyc in he_normal(shape, name, dim_ordering) 
    64  ''' 
    65  fan_in, fan_out = get_fans(shape, dim_ordering=dim_ordering) 
---> 66  s = np.sqrt(2./fan_in) 
    67  return normal(shape, s, name=name) 
    68 

TypeError: unsupported operand type(s) for /: 'float' and 'NoneType' 

Jede Idee, warum?

+1

Welche Version von Keras sind Sie? Wenn ich versuche, Ihren Code auszuführen, erhalte ich den Fehler: "Sie haben versucht, die Schicht" dose_output "aufzurufen. Diese Ebene hat keine Informationen über ihre erwartete Eingabeform, wobei 'Dichte_Ausgabe' die letzte Ebene ist, da K.std keine Ausgabeforminformationen hat, wie Keras in seine Ebenen einbettet. – 7VoltCrayon

Antwort

1

std ist keine Keras-Schicht, so dass es nicht die Layer-Input/Output-Shape-Schnittstelle erfüllt. Die Lösung hierfür ist eine Lambda Schicht K.std Verpackung zu verwenden:

from keras.layers import merge, Input, Dense, Lambda 
from keras.layers import Convolution1D, Flatten 
from keras import backend as K 

input_img = Input(shape=(64, 4)) 

x = Convolution1D(48, 3, activation='relu', init='he_normal')(input_img) 
x = Flatten()(x) 

std = Lambda(lambda x: K.std(x, axis=1))(input_img) 
x = merge([x, std], mode='concat', concat_axis=1) 

output = Dense(100, activation='softmax', init='he_normal')(x)