[FIXED] Tensorflow-Keras vector normalise output layer

Issue

My model outputs a direction vector in 3d space so I do not care about the magnitude of the vector.
How can I vector normalise the output layer so that the loss function doesn’t care about the magnitude either?

Model:

model = keras.Sequential(
    [
        keras.Input(shape=(17,), dtype=np.float64),
        layers.Dense(9, activation="relu"),
        layers.Dense(9, activation="relu"),
        layers.Dense(9, activation="relu"),
        layers.Dense(9, activation="relu"),
        layers.Dense(3) # output layer I want to vector normalise
    ]
)

Alternatively would it be possible to specify for the loss function to only consider the angle between the vectors as loss?

Thank you.

Solution

I found what I was looking for:

tf.keras.losses.CosineSimilarity

Answered By – Wock

Answer Checked By – Laura B. (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published