updated README.md
Browse files
README.md
CHANGED
@@ -2,8 +2,88 @@
|
|
2 |
tags:
|
3 |
- pytorch_model_hub_mixin
|
4 |
- model_hub_mixin
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
---
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
tags:
|
3 |
- pytorch_model_hub_mixin
|
4 |
- model_hub_mixin
|
5 |
+
datasets:
|
6 |
+
- scikit-learn/iris
|
7 |
+
metrics:
|
8 |
+
- accuracy
|
9 |
+
library_name: pytorch
|
10 |
+
pipeline_tag: tabular-classification
|
11 |
---
|
12 |
|
13 |
+
# mlp-iris
|
14 |
+
|
15 |
+
A multi-layer perceptron (MLP) trained on the Iris dataset.
|
16 |
+
|
17 |
+
It takes four inputs: 'SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm' and 'PetalWidthCm'. It predicts whether the species is 'Iris-setosa' / 'Iris-versicolor' / 'Iris-virginica'.
|
18 |
+
|
19 |
+
It is a PyTorch adaptation of the scikit-learn model in Chapter 10 of Aurelien Geron's book 'Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow'. Find the scikit-learn model here: https://github.com/ageron/handson-ml3/blob/main/10_neural_nets_with_keras.ipynb
|
20 |
+
|
21 |
+
Code: https://github.com/sambitmukherjee/handson-ml3-pytorch/blob/main/chapter10/mlp_iris.ipynb
|
22 |
+
|
23 |
+
Experiment tracking: https://wandb.ai/sadhaklal/mlp-iris
|
24 |
+
|
25 |
+
## Usage
|
26 |
+
|
27 |
+
```
|
28 |
+
!pip install -q datasets
|
29 |
+
|
30 |
+
from datasets import load_dataset
|
31 |
+
|
32 |
+
iris = load_dataset("scikit-learn/iris")
|
33 |
+
iris.set_format("pandas")
|
34 |
+
iris_df = iris['train'][:]
|
35 |
+
|
36 |
+
label2id = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}
|
37 |
+
iris_df['Species'] = [label2id[species] for species in iris_df['Species']]
|
38 |
+
|
39 |
+
X = iris_df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']].values
|
40 |
+
y = iris_df['Species'].values
|
41 |
+
|
42 |
+
from sklearn.model_selection import train_test_split
|
43 |
+
|
44 |
+
X_train_full, X_test, y_train_full, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=42)
|
45 |
+
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, test_size=0.1, stratify=y_train_full, random_state=42)
|
46 |
+
|
47 |
+
X_means, X_stds = X_train.mean(axis=0), X_train.std(axis=0)
|
48 |
+
|
49 |
+
import torch
|
50 |
+
import torch.nn as nn
|
51 |
+
from huggingface_hub import PyTorchModelHubMixin
|
52 |
+
|
53 |
+
device = torch.device("cpu")
|
54 |
+
|
55 |
+
class MLP(nn.Module, PyTorchModelHubMixin):
|
56 |
+
def __init__(self):
|
57 |
+
super().__init__()
|
58 |
+
self.fc1 = nn.Linear(4, 5)
|
59 |
+
self.fc2 = nn.Linear(5, 3)
|
60 |
+
|
61 |
+
def forward(self, x):
|
62 |
+
act = torch.relu(self.fc1(x))
|
63 |
+
return self.fc2(act)
|
64 |
+
|
65 |
+
model = MLP.from_pretrained("sadhaklal/mlp-iris")
|
66 |
+
model.to(device)
|
67 |
+
|
68 |
+
X_new = X_test[:2] # Contains data on 2 new flowers from the test set.
|
69 |
+
X_new = ((X_new - X_means) / X_stds) # Normalize.
|
70 |
+
X_new = torch.tensor(X_new, dtype=torch.float32)
|
71 |
+
|
72 |
+
model.eval()
|
73 |
+
X_new = X_new.to(device)
|
74 |
+
with torch.no_grad():
|
75 |
+
logits = model(X_new)
|
76 |
+
probas = torch.softmax(logits, dim=-1)
|
77 |
+
confidences, preds = probas.max(dim=-1)
|
78 |
+
|
79 |
+
print(f"Predicted classes: {preds}")
|
80 |
+
print(f"Predicted confidences: {confidences}")
|
81 |
+
```
|
82 |
+
|
83 |
+
## Metric
|
84 |
+
|
85 |
+
Accuracy on the test set: 0.9333
|
86 |
+
|
87 |
+
---
|
88 |
+
|
89 |
+
This model has been pushed to the Hub using the [PyTorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration.
|