1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use super::{mlp, MlpConfig};
use crate::model::{SubModel, SubModel2};
use tch::{nn, nn::Module, Device, Tensor};

/// Multilayer perceptron.
pub struct Mlp {
    config: MlpConfig,
    device: Device,
    seq: nn::Sequential,
}

impl Mlp {
    fn create_net(var_store: &nn::VarStore, config: &MlpConfig) -> nn::Sequential {
        let p = &var_store.root();
        let mut seq = nn::seq();
        let mut in_dim = config.in_dim;

        for (i, &out_dim) in config.units.iter().enumerate() {
            seq = seq.add(nn::linear(
                p / format!("{}{}", "cl", i + 1),
                in_dim,
                out_dim,
                Default::default(),
            ));
            seq = seq.add_fn(|x| x.relu());
            in_dim = out_dim;
        }

        seq = seq.add(nn::linear(
            p / format!("{}{}", "cl", config.units.len() + 1),
            in_dim,
            config.out_dim,
            Default::default(),
        ));

        if !config.activation_out {
            seq = seq.add_fn(|x| x.relu());
        }

        seq
    }
}

impl SubModel for Mlp {
    type Config = MlpConfig;
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, x: &Self::Input) -> Tensor {
        self.seq.forward(&x.to(self.device))
    }

    fn build(var_store: &nn::VarStore, config: Self::Config) -> Self {
        let device = var_store.device();
        let seq = Self::create_net(var_store, &config);

        Self {
            config,
            device,
            seq,
        }
    }

    fn clone_with_var_store(&self, var_store: &nn::VarStore) -> Self {
        let config = self.config.clone();
        let device = var_store.device();
        let seq = Self::create_net(&var_store, &config);

        Self {
            config,
            device,
            seq,
        }
    }
}

impl SubModel2 for Mlp {
    type Config = MlpConfig;
    type Input1 = Tensor;
    type Input2 = Tensor;
    type Output = Tensor;

    fn forward(&self, input1: &Self::Input1, input2: &Self::Input2) -> Self::Output {
        let input1: Tensor = input1.to(self.device);
        let input2: Tensor = input2.to(self.device);
        let input = Tensor::cat(&[input1, input2], -1);
        self.seq.forward(&input.to(self.device))
    }

    fn build(var_store: &nn::VarStore, config: Self::Config) -> Self {
        let units = &config.units;
        let in_dim = *units.last().unwrap_or(&config.in_dim);
        let out_dim = config.out_dim;
        let p = &var_store.root();
        let seq = mlp("cl", var_store, &config).add(nn::linear(
            p / format!("cl{}", units.len() + 1),
            in_dim,
            out_dim,
            Default::default(),
        ));

        Self {
            config,
            device: var_store.device(),
            seq,
        }
    }

    fn clone_with_var_store(&self, var_store: &nn::VarStore) -> Self {
        let config = self.config.clone();
        let device = var_store.device();
        let seq = Self::create_net(&var_store, &config);

        Self {
            config,
            device,
            seq,
        }
    }
}