def __init__(self,
              n_links,
              hidden_size=20,
              prior_pi=1.0,
              prior_sigma_1=1.0,
              prior_sigma_2=0.01):
     super(BRNN, self).__init__()
     self.hidden_size = hidden_size
     self.lstm_1 = BayesianLSTM(n_links,
                                hidden_size,
                                prior_pi=prior_pi,
                                prior_sigma_1=prior_sigma_1,
                                prior_sigma_2=prior_sigma_2,
                                posterior_rho_init=1.0,
                                peephole=False)
     self.lstm_2 = BayesianLSTM(hidden_size,
                                hidden_size,
                                prior_pi=prior_pi,
                                prior_sigma_1=prior_sigma_1,
                                prior_sigma_2=prior_sigma_2,
                                posterior_rho_init=1.0,
                                peephole=False)
     self.linear = BayesianLinear(hidden_size,
                                  n_links,
                                  prior_pi=prior_pi,
                                  prior_sigma_1=prior_sigma_1,
                                  prior_sigma_2=prior_sigma_2,
                                  posterior_rho_init=3.0)
示例#2
0
    def test_sequential_cuda(self):
        #check if we can create sequential models chaning our Bayesian Linear layers
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        b_lstm = BayesianLSTM(1, 10)
        to_feed = torch.ones((10, 10, 1))

        predicted = b_lstm(to_feed)
示例#3
0
    def test_brnn_sharpen_posterior_on_forward_lstm(self):
        b_module = BayesianLSTM(3, 5, sharpen=True)
        in_tensor = torch.ones(5, 4, 3)
        out_tensor = b_module(in_tensor)[0][:, -1, :]

        loss = nn.MSELoss()(out_tensor.clone().detach().normal_(), out_tensor)
        b_module.forward(in_tensor, sharpen_loss=loss)
示例#4
0
    def __init__(self, n_features, n_hidden, seq_len, y_length):
        super(B_LSTM, self).__init__()

        self.n_hidden = n_hidden
        self.seq_len = seq_len
        self.y_len = y_length

        self.lstm = BayesianLSTM(n_features, n_hidden)
        self.linear = nn.Linear(in_features=n_hidden, out_features=y_length)
示例#5
0
 def __init__(self):
     super().__init__()
     self.blinear1 = BayesianLinear(10, 512)
     self.bconv = BayesianConv2d(3,
                                 3,
                                 kernel_size=(3, 3),
                                 padding=1,
                                 bias=True)
     self.blstm = BayesianLSTM(10, 2)
示例#6
0
    def test_frozen_inference(self):
        b_lstm = BayesianLSTM(1, 10)
        b_lstm.freeze = True

        in_data = torch.ones((10, 10, 1))
        b_inference_1 = b_lstm(in_data, hidden_states=None)
        b_inference_2 = b_lstm(in_data, hidden_states=None)

        self.assertEqual((b_inference_1[0] == b_inference_2[0]).all(),
                         torch.tensor(True))
示例#7
0
    def test_infer_shape_1_sample(self):
        deterministic_lstm = nn.LSTM(1, 10, 1, batch_first=True)
        in_data = torch.ones((10, 10, 1))
        det_inference = deterministic_lstm(in_data)

        b_lstm = BayesianLSTM(1, 10)
        b_inference = b_lstm(in_data, hidden_states=None)

        self.assertEqual(det_inference[0].shape, b_inference[0].shape)
        pass
示例#8
0
    def test_kl_divergence(self):
        #create model, sample weights
        #check if kl divergence between apriori and a posteriori is working
        b_lstm = BayesianLSTM(1, 10)
        to_feed = torch.ones((10, 10, 1))

        predicted = b_lstm(to_feed)
        complexity_cost = b_lstm.log_variational_posterior - b_lstm.log_prior

        self.assertEqual((complexity_cost == complexity_cost).all(),
                         torch.tensor(True))
示例#9
0
    def test_variational_inference(self):
        #create module, check if inference is variating
        deterministic_lstm = nn.LSTM(1, 10, 1, batch_first=True)
        in_data = torch.ones((10, 10, 1))
        det_inference = deterministic_lstm(in_data)

        b_lstm = BayesianLSTM(1, 10)
        b_inference_1 = b_lstm(in_data, hidden_states=None)
        b_inference_2 = b_lstm(in_data, hidden_states=None)

        self.assertEqual((b_inference_1[0] != b_inference_2[0]).any(),
                         torch.tensor(True))
        self.assertEqual((det_inference[0] == det_inference[0]).all(),
                         torch.tensor(True))
        pass
示例#10
0
    def __init__(self, input_dim, hidden_dim, linear_dim, sequence_length,
                 output_dim):
        '''
			input_dim: input dimension (number of stops + dimension of temporal features)
			hidden_dim: hidden dimension
			linear_dim: linear layer dimension
			sequence_length: sequence_length (default: 24*14 hours i.e. two weeks)
			output_dim: output dimension (number of stops, default: 10)
		'''

        super(LSTM_Net, self).__init__()

        # define network constants
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.linear_dim = linear_dim
        self.sequence_length = sequence_length
        self.output_dim = output_dim

        # bayesian LSTM layer
        self.lstm1 = BayesianLSTM(in_features=input_dim,
                                  out_features=hidden_dim,
                                  bias=True,
                                  prior_sigma_1=1.0,
                                  prior_sigma_2=4.0,
                                  prior_pi=0.5,
                                  posterior_mu_init=0,
                                  posterior_rho_init=-0.5)

        # bayesian linear layer
        self.blinear1 = BayesianLinear(
            in_features=hidden_dim,
            out_features=linear_dim,
            bias=True,
            prior_sigma_1=1.0,
            prior_sigma_2=0.5,
            prior_pi=0.5,
            posterior_mu_init=0,
            posterior_rho_init=-0.5,
        )

        # linear layer
        self.linear1 = nn.Linear(in_features=linear_dim,
                                 out_features=output_dim,
                                 bias=True)

        # dropout function
        self.dropout = nn.Dropout(p=0.5)
示例#11
0
 def test_init_bayesian_lstm(self):
     b_lstm = BayesianLSTM(10, 10)
     self.assertEqual(isinstance(b_lstm, (nn.Module)), True)
     self.assertEqual(isinstance(b_lstm, (BayesianModule)), True)
     pass
示例#12
0
 def test_peephole_inference(self):
     b_lstm = BayesianLSTM(1, 10, peephole=True)
     in_data = torch.ones((10, 10, 1))
     b_lstm(in_data)
     b_lstm.freeze = True
     b_lstm(in_data)
示例#13
0
 def __init__(self):
     super().__init__()
     self.lstm1 = BayesianLSTM(3, 5, sharpen=True)
     self.gru1 = BayesianGRU(5, 3, sharpen=True)