<jupyter_start><jupyter_text>Demo - Bayesian Neural Network Classification<jupyter_code>import numpy as np
from sklearn import datasets

import torch
import torch.nn as nn
import torch.optim as optim

import torchbnn as bnn
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1. Load Iris Data<jupyter_code>iris = datasets.load_iris()
X = iris.data
Y = iris.target 
x, y = torch.from_numpy(X).float(), torch.from_numpy(Y).long()
x.shape, y.shape<jupyter_output><empty_output><jupyter_text>2. Define Model<jupyter_code>model = nn.Sequential(
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=4, out_features=100),
    nn.ReLU(),
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=100, out_features=3),
)
ce_loss = nn.CrossEntropyLoss()
kl_loss = bnn.BKLLoss(reduction='mean', last_layer_only=False)
kl_weight = 0.01

optimizer = optim.Adam(model.parameters(), lr=0.01)<jupyter_output><empty_output><jupyter_text>3. Train Model<jupyter_code>kl_weight = 0.1
for step in range(3000):
    pre = model(x)
    ce = ce_loss(pre, y)
    kl = kl_loss(model)
    cost = ce + kl_weight*kl
    
    optimizer.zero_grad()
    cost.backward()
    optimizer.step()
    
_, predicted = torch.max(pre.data, 1)
total = y.size(0)
correct = (predicted == y).sum()
print('- Accuracy: %f %%' % (100 * float(correct) / total))
print('- CE : %2.2f, KL : %2.2f' % (ce.item(), kl.item()))<jupyter_output>- Accuracy: 96.666667 %
- CE : 0.11, KL : 1.31<jupyter_text>4. Test Model<jupyter_code>def draw_plot(predicted) :
    fig = plt.figure(figsize = (16, 5))

    ax1 = fig.add_subplot(1, 2, 1)
    ax2 = fig.add_subplot(1, 2, 2)

    z1_plot = ax1.scatter(X[:, 0], X[:, 1], c = Y)
    z2_plot = ax2.scatter(X[:, 0], X[:, 1], c = predicted)

    plt.colorbar(z1_plot,ax=ax1)
    plt.colorbar(z2_plot,ax=ax2)

    ax1.set_title("REAL")
    ax2.set_title("PREDICT")

    plt.show()
pre = model(x)
_, predicted = torch.max(pre.data, 1)
draw_plot(predicted)
# Bayesian Neural Network will return different outputs even if inputs are same.
# In other words, different plots will be shown every time forward method is called.
pre = model(x)
_, predicted = torch.max(pre.data, 1)
draw_plot(predicted)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14658990256864482, 'fraction_numerical': 0.0354295837023915, 'mean_word_length': 4.633416458852868, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '531866', 'license': 'MIT', 'n_tokens_mistral': 946, 'n_tokens_neox': 888, 'n_words': 187, 'path': 'bnn_classification.ipynb', 'repo_name': 'Robin-ML/bnn', 'size': '2258'}