Jmeeks29ig
Jmeeks29ig

Reputation: 219

How can I directly modify the weight values in the Julia library Flux?

In the Julia library Flux, we have the ability to take a neural network, let's call it network m and extract the weights of network m with the following code:

params(m)

This returns a Zygote.Params type of object, of the form:

Params([Float32[0.20391908 -0.101616435 0.09610984 -0.1013181 -0.13325627 -0.034813307 -0.13811183 0.27022845 ...]...)

If I wanted to alter each of the weights slightly, how would I be able to access them?

Edit: As requested, here is the structure for m:

Chain(LSTM(8,10),Dense(10,1))

Upvotes: 2

Views: 1129

Answers (1)

You can iterate on a Params object to access each set of parameters as an array, which you can manipulate in place.

Supposing you want to change every parameter by 1‰, you could do something like the following:

julia> using Flux

julia> m = Dense(10, 5, σ)
Dense(10, 5, σ)

julia> params(m)
Params([Float32[-0.026854342 -0.57200056 … 0.36827534 -0.39761665; -0.47952518 0.594778 … 0.32624483 0.29363066; … ; -0.22681071 -0.0059174187 … -0.59344876 -0.02679312; 
-0.4910349 0.60780525 … 0.114975974 0.036513895], Float32[0.0, 0.0, 0.0, 0.0, 0.0]])                                                                                     

julia> for p in params(m)
           p .*= 1.001
       end

julia> params(m)
Params([Float32[-0.026881196 -0.5725726 … 0.3686436 -0.39801428; -0.4800047 0.5953728 … 0.32657108 0.2939243; … ; -0.22703752 -0.0059233364 … -0.5940422 -0.026819913; -0.
49152592 0.60841304 … 0.11509095 0.03655041], Float32[0.0, 0.0, 0.0, 0.0, 0.0]])                                                                                         

Upvotes: 5

Related Questions