LR
- $y = \mathbf{w}^\top \mathbf{x} + b + \epsilon \text{ where } \epsilon \sim \mathcal{N}(0, \sigma^2).$
- $p(y|\mathbf{x}) = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left(-\frac{1}{2 \sigma^2} (y - \mathbf{w}^\top \mathbf{x} - b)^2\right).$
1 | %matplotlib inline |
1 | def synthetic_data(w, b, num_examples): |
1 | true_w = torch.tensor([2, -3.4]) |
1 | features[0], labels[0] |
(tensor([-0.9447, -1.9361]), tensor(9.1399))
1 | # Plot settings |
1 | set_figsize(figsize=(4, 4)) |
1 | # 特征的最后一维做可视化, |
<matplotlib.collections.PathCollection at 0x129ad8690>
1 | # golden example on batch-data access |
1 | batch_size = 10 |
tensor([[-0.0140, 0.4955],
[ 0.3740, 1.4491],
[ 1.3240, 0.6199],
[-0.0944, -0.1557],
[-0.2420, 0.2464],
[-0.6962, -1.4067],
[ 0.6464, -0.7040],
[-0.6650, 0.1285],
[ 0.6528, 0.4227],
[-1.4647, 0.0225]])
tensor([2.7209, 0.2724, 4.9975, 4.7869, 3.1399, 7.8283, 8.1079, 2.6803, 4.3185,
1.4501])
1 | #Model parameters |
1 | #Model |
1 | """ |
1 | # Traning |
epoch 1, loss 0.03302072733640671
epoch 2, loss 0.0073621841147542
epoch 3, loss 0.005785355810075998
epoch 4, loss 0.002183828502893448
epoch 5, loss 0.002760048722848296
框架自带的数据类构建LR
1 | from torch.utils import data |
1 | def load_array(data_array, batch_size, is_train=True): |
1 | next(iter(data_iter)) |
[tensor([[-0.6236, 0.8897],
[-0.3073, 0.9034],
[ 0.9847, -0.2340],
[-0.6958, 0.5574],
[-0.5059, -0.5852],
[-1.0757, -0.4725],
[-1.6130, -0.9148],
[-1.2579, 0.1319],
[-0.8099, -0.4857],
[-0.2726, 1.0848]]),
tensor([0.1864, 0.7726, 7.2163, 1.1565, 5.4282, 3.9169, 4.3345, 1.4896, 4.4729,
0.2074])]
1 | from torch import nn |
1 | net = nn.Sequential(nn.Linear(2, 1)) |
1 | num_epochs = 3 |
epoch 1, loss 16.28912353515625
epoch 2, loss 16.194189071655273
epoch 3, loss 16.209455490112305
MINIST图像多分类
1 | import torchvision |
1 | # By default pytorch torchvision datasets are of type PIL. |
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to fashion-mnist/data/FashionMNIST/raw/train-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting fashion-mnist/data/FashionMNIST/raw/train-images-idx3-ubyte.gz to fashion-mnist/data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to fashion-mnist/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting fashion-mnist/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to fashion-mnist/data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to fashion-mnist/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting fashion-mnist/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to fashion-mnist/data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to fashion-mnist/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting fashion-mnist/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to fashion-mnist/data/FashionMNIST/raw
Processing...
Done!
/Users/distiller/project/conda/conda-bld/pytorch_1587428061935/work/torch/csrc/utils/tensor_numpy.cpp:141: 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.
1 | def get_fashion_mnist_labels(labels): #@save |
1 | def show_images(imgs, num_rows, num_cols, title=None, scale=1.5): |
1 | X, y = next(iter(data.DataLoader(mnist_train, batch_size=5))) |
array([<matplotlib.axes._subplots.AxesSubplot object at 0x131bad250>,
<matplotlib.axes._subplots.AxesSubplot object at 0x131be9910>,
<matplotlib.axes._subplots.AxesSubplot object at 0x131c2a150>,
<matplotlib.axes._subplots.AxesSubplot object at 0x131c5e950>,
<matplotlib.axes._subplots.AxesSubplot object at 0x131c9e190>],
dtype=object)
1 | batch_size = 256 |
1 | def load_data_fashion_mnist(batch_size, resize=None): #@save |
1 |
|
Model
1 | num_inputs = 784 |
1 | X = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) |
(tensor([[5., 7., 9.]]),
tensor([[ 6.],
[15.]]))
1 | def softmax(x): |
1 | def Net(X): |
cross-entropy
- cross-entropy takes the negative log likelihood of the predicted probability assigned to the true label.
- $-\log P(y \mid x)$
1 | def crossEntropy(hat_Y, Y): |
1 | y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]]) |
tensor([0.1000, 0.5000])
1 | def accuracy(hat_Y, Y): |
1 | y = torch.tensor([0, 2]) |
0.5
1 | class Accumulator: |
1 | def evaluate_accuracy(net, data_iter): #@save |
1 | def net(X): |
1 |