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
use crate::model::{SubModel, SubModel2};
use tch::{nn, nn::Module, Device, Tensor};
use super::{MLPConfig, mlp};

#[allow(clippy::clippy::upper_case_acronyms)]
/// 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 seq = mlp("cl", var_store, config).add(nn::linear(
            p / format!("cl{}", config.units.len() + 1),
            *config.units.last().unwrap(),
            config.out_dim,
            Default::default(),
        ));

        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,
        }
    }
}