Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize functions #2

Open
HLWeil opened this issue Jun 7, 2021 · 0 comments
Open

Modularize functions #2

HLWeil opened this issue Jun 7, 2021 · 0 comments

Comments

@HLWeil
Copy link

HLWeil commented Jun 7, 2021

Many of the gcn and also the train functions might be generalized to allow for a more modular and broader use.

E.g. the GCNModel.create function might be generalized to a compose function:

Instead of

let create nfeat nhid nclass dropout adj =
    let gc1 = gcnLayer nfeat nhid true adj
    let gc2 = gcnLayer nhid nclass true adj        
    let drp = Dropout(dropout) |> M

    F [gc1;gc2;drp] (fun t ->
        use t = gc1.forward(t)
        use t = Functions.ReLU(t)
        use t = drp.forward(t)
        use t = gc2.forward(t)
        let t = Functions.LogSoftmax(t, dimension=1L)
        t)   

there could be a general layer composition function

let composeWithDropOut (dropOut : #IModel) (activationFunc : TorchTensor -> TorchTensor) (outFunc : TorchTensor -> TorchTensor) (layers : FuncModel list) =
    let rec composeF (f : TorchTensor -> TorchTensor) (remainingLayers : #IModel list) =
        match remainingLayers with
        | [] -> failwith "no layers were given"
        | l :: [] -> f >> l.forward >> outFunc
        | l :: ls ->  composeF (f >> l.forward >> dropOut.forward >> activationFunc) ls
    let forward = composeF id layers
    let models : IModel list =  List.append (layers |> List.map (fun x -> x :> IModel)) [dropOut]
    F models forward

with your specific case resulting in

///Create two layer GCN model with dropout
let create nfeat nhid nclass dropout adj =
    let gc1 = gcnLayer nfeat nhid true adj
    let gc2 = gcnLayer nhid nclass true adj        
    let drp = Dropout(dropout) |> M

    composeWithDropOut drp (fun t -> Functions.ReLU(t)) (fun t -> Functions.LogSoftmax(t,dimension=1L)) [gc1;gc2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant