added "Usage" section to README.md
Browse files
README.md
CHANGED
@@ -20,14 +20,64 @@ Code: https://github.com/sambitmukherjee/handson-ml3-pytorch/blob/main/chapter10
|
|
20 |
|
21 |
Experiment tracking: https://wandb.ai/sadhaklal/logistic-regression-iris
|
22 |
|
23 |
-
##
|
24 |
-
|
25 |
-
The validation set contains 30% of the examples (selected at random using stratification on the target variable):
|
26 |
|
27 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
from sklearn.model_selection import train_test_split
|
29 |
|
30 |
X_train, X_val, y_train, y_val = train_test_split(X.values, y.values, test_size=0.3, stratify=y, random_state=42)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
```
|
32 |
|
|
|
|
|
|
|
|
|
33 |
Accuracy on the validation set: 1.0
|
|
|
20 |
|
21 |
Experiment tracking: https://wandb.ai/sadhaklal/logistic-regression-iris
|
22 |
|
23 |
+
## Usage
|
|
|
|
|
24 |
|
25 |
```
|
26 |
+
!pip install -q datasets
|
27 |
+
|
28 |
+
from datasets import load_dataset
|
29 |
+
|
30 |
+
iris = load_dataset("scikit-learn/iris")
|
31 |
+
iris.set_format("pandas")
|
32 |
+
iris_df = iris['train'][:]
|
33 |
+
X = iris_df[['PetalLengthCm', 'PetalWidthCm']]
|
34 |
+
y = (iris_df['Species'] == "Iris-setosa").astype(int)
|
35 |
+
|
36 |
+
class_names = ["Not Iris-setosa", "Iris-setosa"]
|
37 |
+
|
38 |
from sklearn.model_selection import train_test_split
|
39 |
|
40 |
X_train, X_val, y_train, y_val = train_test_split(X.values, y.values, test_size=0.3, stratify=y, random_state=42)
|
41 |
+
X_means, X_stds = X_train.mean(axis=0), X_train.std(axis=0)
|
42 |
+
|
43 |
+
import torch
|
44 |
+
import torch.nn as nn
|
45 |
+
from huggingface_hub import PyTorchModelHubMixin
|
46 |
+
|
47 |
+
device = torch.device("cpu")
|
48 |
+
|
49 |
+
class LinearModel(nn.Module, PyTorchModelHubMixin):
|
50 |
+
def __init__(self):
|
51 |
+
super().__init__()
|
52 |
+
self.fc = nn.Linear(2, 1)
|
53 |
+
|
54 |
+
def forward(self, x):
|
55 |
+
out = self.fc(x)
|
56 |
+
return out
|
57 |
+
|
58 |
+
model = LinearModel.from_pretrained("sadhaklal/logistic-regression-iris")
|
59 |
+
model.to(device)
|
60 |
+
|
61 |
+
# Inference on new data:
|
62 |
+
import numpy as np
|
63 |
+
|
64 |
+
X_new = np.array([[2.0, 0.5], [3.0, 1.0]]) # Contains data on 2 new flowers.
|
65 |
+
X_new = ((X_new - X_means) / X_stds) # Normalize.
|
66 |
+
X_new = torch.from_numpy(X_new).float()
|
67 |
+
|
68 |
+
model.eval()
|
69 |
+
X_new = X_new.to(device)
|
70 |
+
with torch.no_grad():
|
71 |
+
logits = model(X_new)
|
72 |
+
proba = torch.sigmoid(logits.squeeze())
|
73 |
+
preds = (proba > 0.5).long()
|
74 |
+
|
75 |
+
print(f"Predicted classes: {preds}")
|
76 |
+
print(f"Predicted probabilities of being Iris-setosa: {proba}")
|
77 |
```
|
78 |
|
79 |
+
## Metric
|
80 |
+
|
81 |
+
As shown above, the validation set contains 30% of the examples (selected at random in a stratified fashion).
|
82 |
+
|
83 |
Accuracy on the validation set: 1.0
|