# Is it possible to unnormalise data in Flux.jl?

Hi,

I’ve seen a convenient normalisation function in Flux.jl, Flux.normalise.
However, what if I want to unnormalise data (recover the original data)? Is there such functionality in Flux.jl?

Thanks.

you will have to store the mean and variance by yourself to perform the inverse transform, otherwise that information is lost

Nice thing about Flux is that it works with all julia packages.
I am surprised it actually has a `normalize` of it’s own.

Something like StatsBase can do this.

``````julia> using StatsBase

julia> x = randn(10) .+ 5
10-element Vector{Float64}:
5.964172883616336
4.116442184696866
6.605584680364782
3.519368666428235
6.201987836586334
4.446148516233912
6.684526916570591
4.791433884353377
3.683098936166946
4.955188515988089

julia> t = fit(ZScoreTransform, x)
ZScoreTransform{Float64, Vector{Float64}}(1, 1, [5.0967953021005465], [1.1905029391356516])

julia> px = StatsBase.transform(t, x)
10-element Vector{Float64}:
0.7285807980831507
-0.823478115993105
1.267354601711165
-1.3250086025134726
0.9283408701940692
-0.5465310201913723
1.3336645902132718
-0.2564978276902644
-1.1874782660847485
-0.11894702772869165

julia> StatsBase.reconstruct
reconstruct  reconstruct!
julia> StatsBase.reconstruct(t, px)
10-element Vector{Float64}:
5.964172883616336
4.116442184696866
6.605584680364782
3.5193686664282353
6.201987836586334
4.446148516233912
6.684526916570591
4.791433884353377
3.683098936166946
4.955188515988089
``````

Another option is FeatureTransforms.jl

``````julia> using FeatureTransforms

julia> t = MeanStdScaling(x)
MeanStdScaling(5.0967953021005465, 1.1905029391356516)

julia> xp = FeatureTransforms.apply(x, t)
10-element Vector{Float64}:
0.7285807980831507
-0.823478115993105
1.267354601711165
-1.3250086025134726
0.9283408701940692
-0.5465310201913723
1.3336645902132718
-0.2564978276902644
-1.1874782660847485
-0.11894702772869165

julia> FeatureTransforms.apply(x, t; inverse=true)
10-element Vector{Float64}:
12.197160649558949
9.997431821764149
12.960763278784253
9.286614043385278
12.48028005004014
10.389948178510636
13.05474424300921
10.801011424097393
9.481535410734686
10.995961794355594
``````

I haven’t actually tested these with Flux.
They should work, though they might give mutation errors.

4 Likes

I’ve tested StatsBase.jl with Flux.jl as you suggested but it does not work properly.
It seems that StatsBase.jl mutates arrays, which is not supported by Zygote.jl for auto-differentiation.

Did you try the second package @oxinabox mentioned? FeatureTransforms should be relatively AD-safe since it doesn’t use mutation at all.

Not yet.
Since the package is relatively young, I thought there is a risk to use it.
But it would be worth to try it. Thanks

NB I work for Invenia, so I am in no way unbiased.
But you can trust most things we make.
We do this as a job, we have higher standards than anyone else for continued maintainance.
That one is being used in like 6+ components of our production system.

2 Likes