Пример #1
0
    def __init__(self,
                 num_samples=3000,
                 step_size=0.1,
                 num_steps_per_sample=1,
                 tau_out=1,
                 n_units_1=50,
                 n_units_2=50,
                 n_units_3=50,
                 keep_every=2,
                 normalize_input=True,
                 normalize_output=True,
                 seed=0,
                 RM=False,
                 burnin=500,
                 prior_tau=1.0):
        """
        This module performs HMC approximation for a Bayesian neural network.
        """

        # Set the random seeds
        self.seed = seed
        torch.manual_seed(seed)
        hamiltorch.set_random_seed(seed)
        # Check availability of GPU
        self.device = torch.device(
            "cuda: 0" if torch.cuda.is_available() else "cpu")

        self.X = None
        self.y = None
        # Network params
        self.network = None
        self.normalize_input = normalize_input
        self.normalize_output = normalize_output
        self.n_units_1 = n_units_1
        self.n_units_2 = n_units_2
        self.n_units_3 = n_units_3

        # HMC params
        self.num_steps_per_sample = num_steps_per_sample
        self.step_size = step_size
        self.num_samples = num_samples
        self.RM = RM
        self.burnin = burnin
        self.tau_out = tau_out
        self.prior_tau = prior_tau
        self.keep_every = keep_every
Пример #2
0
import torch
import hamiltorch
import matplotlib.pyplot as plt
import torch.nn as nn
import numpy as np
import util

# device

print(f'Is CUDA available?: {torch.cuda.is_available()}')
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = 'cpu'

# hyperparameters

hamiltorch.set_random_seed(123)
prior_std = 1
like_std = 0.1
step_size = 0.001
burn = 100
num_samples = 200
L = 100
layer_sizes = [1, 16, 16, 1]
activation = torch.tanh
pde = True
pinns = False
epochs = 10000
tau_priors = 1 / prior_std**2
tau_likes = 1 / like_std**2

lb = 0