2017-08-01 2 views
0

ich zusammen auf mehrere Schichten der Lage sein möchte, aber vor dem Eingang, so etwas wie die folgenden Angabe:Wie Kette/compose Schichten in keras 2 funktionellem API ohne Eingabe zu spezifizieren (oder Eingabeform)

# conv is just a layer, no application 
conv = Conv2D(64, (3,3), activation='relu', padding='same', name='conv') 
# this doesn't work: 
bn = BatchNormalization()(conv) 

Beachten Sie, dass ich die Eingabe und ihre Form nicht angeben möchte, wenn sie vermieden werden kann. Ich möchte dies zu einem späteren Zeitpunkt als gemeinsame Ebene für mehrere Eingaben verwenden.

Gibt es eine Möglichkeit, das zu tun? Die obige Abbildung zeigt den folgenden Fehler:

>>> conv = Conv2D(64, (3,3), activation='relu', padding='same', name='conv') 
>>> bn = BatchNormalization()(conv) 
Traceback (most recent call last): 
    File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 419, in assert_input_compatibility 
    K.is_keras_tensor(x) 
    File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 393, in is_keras_tensor 
    raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. ' 
ValueError: Unexpectedly found an instance of type `<class 'keras.layers.convolutional.Conv2D'>`. Expected a symbolic tensor instance. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 552, in __call__ 
    self.assert_input_compatibility(inputs) 
    File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 425, in assert_input_compatibility 
    str(inputs) + '. All inputs to the layer ' 
ValueError: Layer batch_normalization_4 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.convolutional.Conv2D'>. Full input: [<keras.layers.convolutional.Conv2D object at 0x7f3f6e54b748>]. All inputs to the layer should be tensors. 

die Ausgabe der konv Schicht Grabbing nicht den Trick entweder:

>>> bn = BatchNormalization()(conv.output) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/mitchus/anaconda3/envs/tf/lib/python3.6/site-packages/keras/engine/topology.py", line 941, in output 
    ' has no inbound nodes.') 
AttributeError: Layer conv has no inbound nodes. 

Antwort

1

Try this:

def create_shared_layers(): 
    layers = [ 
     Conv2D(64, (3,3), activation='relu', padding='same', name='conv'), 
     BatchNormalization() 
    ] 
    def shared_layers(x): 
     for layer in layers: 
      x = layer(x) 
     return x 
    return shared_layers 

Später können Sie tun etwas wie:

shared_layers = create_shared_layers() 
... 
h1 = shared_layers(x1) 
h2 = shared_layers(x2) 
+0

Clever Problemumgehung! :) Ich frage mich, ob es eine "idiomatische" Art und Weise gibt, es scheint, als ob dies eine natürliche Funktionalität sein sollte. – mitchus

Verwandte Themen