This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import torch.optim as optim | |
import numpy as np | |
from blitz.modules import BayesianLinear | |
from blitz.utils import variational_estimator | |
from sklearn.datasets import load_boston |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X, y = load_boston(return_X_y=True) | |
X = StandardScaler().fit_transform(X) | |
y = StandardScaler().fit_transform(np.expand_dims(y, -1)) | |
X_train, X_test, y_train, y_test = train_test_split(X, | |
y, | |
test_size=.25, | |
random_state=42) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X, y = load_boston(return_X_y=True) | |
X = StandardScaler().fit_transform(X) | |
y = StandardScaler().fit_transform(np.expand_dims(y, -1)) | |
X_train, X_test, y_train, y_test = train_test_split(X, | |
y, | |
test_size=.25, | |
random_state=42) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X, y = load_boston(return_X_y=True) | |
X = StandardScaler().fit_transform(X) | |
y = StandardScaler().fit_transform(np.expand_dims(y, -1)) | |
X_train, X_test, y_train, y_test = train_test_split(X, | |
y, | |
test_size=.25, | |
random_state=42) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X, y = load_boston(return_X_y=True) | |
X = StandardScaler().fit_transform(X) | |
y = StandardScaler().fit_transform(np.expand_dims(y, -1)) | |
X_train, X_test, y_train, y_test = train_test_split(X, | |
y, | |
test_size=.25, | |
random_state=42) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@variational_estimator | |
class BayesianRegressor(nn.Module): | |
def __init__(self, input_dim, output_dim): | |
super().__init__() | |
#self.linear = nn.Linear(input_dim, output_dim) | |
self.blinear1 = BayesianLinear(input_dim, 512) | |
self.blinear2 = BayesianLinear(512, output_dim) | |
def forward(self, x): | |
x_ = self.blinear1(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate_regression(regressor, | |
X, | |
y, | |
samples = 100, | |
std_multiplier = 2): | |
preds = [regressor(X) for i in range(samples)] | |
preds = torch.stack(preds) | |
means = preds.mean(axis=0) | |
stds = preds.std(axis=0) | |
ci_upper = means + (std_multiplier * stds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regressor = BayesianRegressor(13, 1) | |
optimizer = optim.SGD(regressor.parameters(), lr=0.001) | |
criterion = torch.nn.MSELoss() | |
ds_train = torch.utils.data.TensorDataset(X_train, y_train) | |
dataloader_train = torch.utils.data.DataLoader(ds_train, batch_size=16, shuffle=True) | |
ds_test = torch.utils.data.TensorDataset(X_test, y_test) | |
dataloader_test = torch.utils.data.DataLoader(ds_test, batch_size=16, shuffle=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import torch | |
import torch.nn as nn | |
import torch.optim as optim | |
import torch.nn.functional as F | |
from blitz.modules import BayesianLSTM | |
from blitz.utils import variational_estimator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#importing the dataset | |
amazon="data/AMZN_2006-01-01_to_2018-01-01.csv" | |
ibm="data/IBM_2006-01-01_to_2018-01-01.csv" | |
df = pd.read_csv(ibm) | |
#scaling and selecting data | |
close_prices = df["Close"] | |
scaler = StandardScaler() | |
close_prices_arr = np.array(close_prices).reshape(-1, 1) |