Seite 1 von 1

Input_shape eines ANN

Verfasst: Samstag 28. Mai 2022, 18:17
von nichtSoGuter
Ich habe folgendes Model geschrieben, welches im Anschluss mit KerasTuner optimiert habe.

Code: Alles auswählen

def build_model(hp):
    model = Sequential()
    
    #inputlayer
    model.add(Flatten(input_shape=(28,28)))      # HIER WIRD input_shape FESTGELEGT
    
    #hidden layer
    for i in range(hp.Int("layer_anzahl",1,5)):
        model.add(Dense(units = hp.Int("neuronen_anzahl",32,320,32), activation = "relu"))
        
    # outputlayer
    model.add(Dense(1))
    
    model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate = hp.Choice("learning_rate",[0.1,0.01,0.001,0.0001])), loss = "mse")
    
    return model

Das Problem ist, dass wenn ich predicten möchte übergebe ich der .predict() Methode einen Instanz mit dem shape (28,28). Also genau der shape den ich als input_shape festgelegt habe. Ich befürchte das mein Flatten layer nicht funktioniert. Aber wieso nicht?

Dennoch kommt folgende
Warnung:
WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 28).

und folgende Fehlermeldung:
ValueError: Exception encountered when calling layer "sequential" (type Sequential).

Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 784, but received input with shape (None, 28)

Call arguments received by layer "sequential" (type Sequential):
• inputs=tf.Tensor(shape=(None, 28), dtype=float32)
• training=False
• mask=None

Re: Input_shape eines ANN

Verfasst: Sonntag 29. Mai 2022, 09:47
von ThomasL
Lass dir mal mit model.summary() nach jedem build_model() Aufruf das Modell anzeigen.

Eventuell findest du hier Hilfen zum Verständnis der Anzeige und kannst damit deinen Dimensionsfehler erkennen.

https://stackoverflow.com/questions/369 ... parameters