Path: blob/master/notebooks/book1/09/naive_bayes_mnist_torch.ipynb
1192 views
Kernel: Python 3
Please find jax implementation of this notebook here: https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book1/09/naive_bayes_mnist_jax.ipynb
Naive Bayes classifiers
We show how to implement Naive Bayes classifiers from scratch. We use binary features, and 2 classes. Based on sec 18.9 of http://d2l.ai/chapter_appendix-mathematics-for-deep-learning/naive-bayes.html.
In [1]:
Get data
We use a binarized version of MNIST.
In [2]:
Out[2]:
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Failed to download (trying next):
HTTP Error 503: Service Unavailable
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz to ./temp/MNIST/raw/train-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=0.0, max=9912422.0), HTML(value='')))
Extracting ./temp/MNIST/raw/train-images-idx3-ubyte.gz to ./temp/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Failed to download (trying next):
HTTP Error 503: Service Unavailable
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-labels-idx1-ubyte.gz
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-labels-idx1-ubyte.gz to ./temp/MNIST/raw/train-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=0.0, max=28881.0), HTML(value='')))
Extracting ./temp/MNIST/raw/train-labels-idx1-ubyte.gz to ./temp/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Failed to download (trying next):
HTTP Error 503: Service Unavailable
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz to ./temp/MNIST/raw/t10k-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=0.0, max=1648877.0), HTML(value='')))
Extracting ./temp/MNIST/raw/t10k-images-idx3-ubyte.gz to ./temp/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Failed to download (trying next):
HTTP Error 503: Service Unavailable
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-labels-idx1-ubyte.gz
Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-labels-idx1-ubyte.gz to ./temp/MNIST/raw/t10k-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=0.0, max=4542.0), HTML(value='')))
Extracting ./temp/MNIST/raw/t10k-labels-idx1-ubyte.gz to ./temp/MNIST/raw
/usr/local/lib/python3.7/dist-packages/torchvision/datasets/mnist.py:498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:180.)
return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
In [3]:
Out[3]:
Dataset MNIST
Number of datapoints: 60000
Root location: ./temp
Split: Train
StandardTransform
Transform: Compose(
ToTensor()
)
In [4]:
Out[4]:
<class 'torch.Tensor'>
torch.Size([1, 28, 28])
<class 'int'>
4
In [5]:
Out[5]:
tensor([[0.3569, 0.1098, 0.0196, 0.9137, 0.9804],
[0.0000, 0.0000, 0.4000, 0.9961, 0.8627],
[0.0000, 0.0000, 0.6627, 0.9961, 0.5373],
[0.0000, 0.0000, 0.6627, 0.9961, 0.2235],
[0.0000, 0.0000, 0.6627, 0.9961, 0.2235]])
In [6]:
Out[6]:
[tensor(0.), tensor(1.)]
In [7]:
Out[7]:
torch.Size([1, 28, 28])
torch.Size([2, 1, 28, 28])
torch.Size([1, 2, 28, 28])
torch.Size([2, 28, 28])
In [8]:
In [10]:
Out[10]:
[(10, 28, 28), (10,)]
[[0.7294118 0.99215686 0.99215686 0.5882353 0.10588235]
[0.0627451 0.3647059 0.9882353 0.99215686 0.73333335]
[0. 0. 0.9764706 0.99215686 0.9764706 ]
[0.50980395 0.7176471 0.99215686 0.99215686 0.8117647 ]
[0.99215686 0.99215686 0.99215686 0.98039216 0.7137255 ]]
In [11]:
Out[11]:
[(10, 28, 28), (10,)]
[[ True True True True False]
[False False True True True]
[False False True True True]
[ True True True True True]
[ True True True True True]]
In [12]:
In [13]:
Out[13]:
(60000, 28, 28)
<class 'numpy.ndarray'>
[[ True True True True False]
[False False True True True]
[False False True True True]
[ True True True True True]
[ True True True True True]]
Training
In [14]:
Out[14]:
array([0.09871667, 0.11236667, 0.0993 , 0.10218333, 0.09736667,
0.09035 , 0.09863333, 0.10441667, 0.09751667, 0.09915 ])
In [15]:
Out[15]:
[0 1 2 3 4 5 6 7 8 9]
dict_keys([5, 0, 4, 1, 9, 2, 3, 6, 7, 8])
dict_values([5421, 5923, 5842, 6742, 5949, 5958, 6131, 5918, 6265, 5851])
We use add-one smoothing for class conditional Bernoulli distributions.
In [16]:
Out[16]:
(10, 28, 28)
<class 'numpy.ndarray'>
In [17]:
Out[17]:
Testing
In [18]:
Out[18]:
[-268.97252516 -301.70441241 -245.19514063 -218.87383792 -193.45703309
-206.09087174 -292.52263925 -114.62566181 -220.33134563 -163.17842631]
ytrue 7 yhat 7
[7]
In [ ]:
In [20]:
Out[20]:
In [21]:
Out[21]:
In [22]:
Out[22]:
0.8427