Ich denke, es gibt zwei Möglichkeiten, je nachdem, was Sie brauchen:
(a) predictions_1 und predictions_2 Angelegenheit für Sie. In diesem Fall können Sie ein Netzwerk mit 2 Ausgängen trainieren. Hier ein Beispiel aus Ihrem Beitrag abgeleitet:
input_shape = [3, 20]
inputs = Input(shape=input_shape)
x = Flatten()(inputs)
predictions_1 = Dense(4, name='predictions_1')(x)
# here the predictions_1 just corresponds to your next layer's input
y = Dense(5)(predictions_1)
y = Dense(2)(y)
predictions_2 = Dense(29, name='predictions_2')(y)
# you specify here that you have 2 outputs
model = Model(input=inputs, output=[predictions_1, predictions_2])
Für die .fit und .predict, können Sie eine Menge von Details in https://keras.io/getting-started/functional-api-guide/ Abschnitt finden: Multi-Input und Multi-Output-Modelle.
(b) Sie sind nur an Vorhersagen_2 interessiert. In diesem Fall können Sie einfach tun:
input_shape = [3, 20]
inputs = Input(shape=input_shape)
x = Flatten()(inputs)
predictions_1 = Dense(4, name='predictions_1')(x)
# here the predictions_1 just corresponds to your next layer's input
y = Dense(5)(predictions_1)
y = Dense(2)(y)
predictions_2 = Dense(29, name='predictions_2')(y)
# you specify here that your only output is predictions_2
model = Model(input=inputs, output=predictions_2)
nun in Bezug auf inception_v3. Sie können selbst die Architektur definieren und die tiefen Schichten nach Ihren Bedürfnissen modifizieren (indem Sie diesen Layern spezifische Namen geben, um zu vermeiden, dass sie automatisch benannt werden).
Danach kompilieren Sie Ihr Modell und Lasten Gewichte (wie in https://keras.io/models/about-keras-models/ siehe Funktion load_weights (..., by_name = True))
# you can load weights for only the part that corresponds to the true
# inception_v3 architecture. The other part will be initialized
# randomly
model.load_weights("inception_v3.hdf5", by_name=True)
Dieses Ihr Problem lösen sollte. Übrigens können Sie zusätzliche Informationen hier finden: https://www.gradientzoo.com. Der Arzt.erklärt mehrere Speichern/Laden/Feinabstimmung Routinen;)
aktualisieren: Wenn Sie von Grund auf Ihr Modell nicht neu definieren möchten Sie Folgendes tun können:
input_shape = [3, 20]
# define model1 and model2 as you want
inputs1 = Input(shape=input_shape)
x = Flatten()(inputs1)
predictions_1 = Dense(4, name='predictions_1')(x)
model1 = Model(input=inputs1, output=predictions_1)
inputs2 = Input(shape=(4,))
y = Dense(5)(inputs2)
y = Dense(2)(y)
predictions_2 = Dense(29, name='predictions_2')(y)
model2 = Model(input=inputs2, output=predictions_2)
# then define functions returning the image of an input through model1 or model2
def give_model1():
def f(x):
return model1(x)
return f
def give_model2():
def g(x):
return model2(x)
return g
# now you can create a global model as follows:
inputs = Input(shape=input_shape)
x = model1(inputs)
predictions = model2(x)
model = Model(input=inputs, output=predictions)
vielen Dank. diese Arbeit, wenn ich das ganze Anfangsmodell neu definieren will. Was aber, wenn ich bereits ein Model-Objekt habe? Ich habe mich gefragt, ob es eine Möglichkeit gibt, es direkt an das neue Modell anzuhängen, ohne es von Grund auf neu definieren zu müssen (wenn man bedenkt, dass es sich um ein sehr großes Modell handelt). Danke auch für den Link – meto
@meto ok ich bekomme es. Ich habe gerade meine Antwort basierend auf Ihrem Kommentar und Omids Vorschlag aktualisiert. Funktioniert es für Sie? – bn2nkm