Skip to content

030_FirstModel_Regression - squeeze() führt zu Shape-Mismatch zwischen y_predict und y_tensor in der Trainingsschleife #2

@joophe

Description

@joophe

Beschreibung
Im Code zu Kapitel 3 (030_FirstModel_Regression) wird in DataPrep.py die Variable y mit doppelten eckigen Klammern erstellt, was ein 2D-Array der Form [n_samples, 1] erzeugt:

y = np.array(anxiety_dummies[['Anxiety Level (1-10)']], dtype=np.float32)

Dadurch hat y_tensor die Form [11000, 1].

In der Trainingsschleife in 10_ModelClass.py wird nun y_predict gesqueezed:

loss = loss_fn(y_predict.squeeze(), y_tensor)

Das führt zu einem Shape Mismatch: y_predict.squeeze() hat die Form [11000], während y_tensor die Form [11000,1] beibehält. PyTorchs Boradcasting berechnet dann eine [11000, 11000]-Matrix statt 11.000 elementweiser Paare. Der MSE-Loss ist damit semantisch falsch.

Lösung - Option A: Beide Tensoren squeezen:

loss = loss_fn(y_predict.squeeze(), y_tensor.squeeze())

Lösung - Option B: y direkt als 1D-Array in DataPrep.py anlegen

y = np.array(anxiety_dummies['Anxiety Level (1-10)'], dtype=np.float32)

Lösung - Option C: squeeze() bei y_predict entfernen

loss = loss_fn(y_predict, y_tensor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions