[Solved]Flux.jl: can it be used to train networks with multiple distinct inputs?


#1

Similar network in Keras: https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models

I’m getting errors when running the following basic example code trying to train a network with two inputs and 1 output. The network is made up out of a left branch and a right branch

using Flux


nINPUTS=1024
nHIDDEN=353
nOUTPUTS=54

W1 = param(rand(nHIDDEN, nINPUTS))
b1 = param(rand(nHIDDEN))
layer1(x) = W1 * x .+ b1

W2 = param(rand(nOUTPUTS, nHIDDEN))
b2 = param(rand(nOUTPUTS))
layer2(x) = W2 * x .+ b2

left_branch(x) = layer2(relu.(layer1(x)))

W3 = param(ones(nOUTPUTS,nOUTPUTS))
b3 = param(zeros(nOUTPUTS))
layer3(y) = W3 * y .+ b3

right_branch(y)=layer3(y)

model(A,B)=left_branch(A) .+ right_branch(B)

da,db,dc = rand(nINPUTS),ones(nOUTPUTS),rand(nOUTPUTS)

Zipped_Data = zip(da,db,dc)

loss(x,y,z) = Flux.mse(model(x,y),z)

optSGD=Flux.Optimise.SGD([W1,W2,W3,b1,b2,b3], η = 0.01)

Flux.train!(loss,Zipped_Data,optSGD)

#2

SOLVED:

  1. by changing the line in the optimizer:
    Flux.Optimise.SGD([W1,W2,W3,b1,b2,b3]
    to
    using Flux.Tracker
    Flux.Optimise.SGD(Params([W1,W2,W3,b1,b2,b3]),…

  2. abandon Zipped_data and writing custom function to obtain the Array(Tuple(Array( structure required for Flux.Train!