SickerDude43
SickerDude43

Reputation: 196

How do I denormalize L2-Normalized Data in MXNet?

I normalized my Data with the built in L2Normalization from MXNet ndarray. Since I want to know the actual value of the prediction I have to denormalize the data to analyze it properly. For normalization I used:

mx.nd.L2Normalization(x, mode='instance')

It computed me the correct values and I also understand how the calculation works. Now I want to reverse it. Is there a built in method?

My idea would be to swap x and y in the function and to solve for x. However I don't know the sum of the instance nor anything else. So I can't simply do it. Is there a function to denormalize? Or do I have to normalize all by myself? That would sadly make the L2Normalization function useless in many cases.

Upvotes: 0

Views: 198

Answers (3)

SickerDude43
SickerDude43

Reputation: 196

The answers given here are correct. There is no built in function for L2 Denormalization in MXNet. However it is easy to simply compute and save these values. Here is my approach in Python:

saved_norm_vals = []
def l2_normalize(row):
    norm = np.linalg.norm(row)
    saved_norm_vals.append(norm)
    if norm == 0:
        return row
    return row / norm


def l2_denormalize(row):
    val = saved_norm_vals.pop(0)
    return row*val

Both function take in the row of a pandas dataframe. You can use this function by using the .apply method. Keep in mind that you have to strictly contain the order for that to work. Or you always need to clear out the list if there are values you don't want to denormalize.

Upvotes: 0

Luca Anzalone
Luca Anzalone

Reputation: 749

Since l2-normalization divides each component x_i in a vector x by the l2-norm of x, that is norm = sqrt(sum(square(x_i))), you should pre-compute that value, and then multiply by it to get the actual predictions.

Say your normalized vector is x', you can recover x by multiplying x' by the previously computed norm.

Upvotes: 1

lejlot
lejlot

Reputation: 66805

You can't "denormalize" if you used a function to normalize. Normalization removes information, norm of your data is completely lost. Since you are doing per-instance normalization this means you have to store norm of every single instance manually to reverse the process.

Upvotes: 1

Related Questions