File size: 2,728 Bytes
a2fef2b
 
 
 
681f9b2
 
 
 
 
 
a2fef2b
 
681f9b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
tags:
- model_hub_mixin
- pytorch_model_hub_mixin
pipeline_tag: tabular-regression
library_name: pytorch
datasets:
- gvlassis/california_housing
metrics:
- rmse
---

# wide-and-deep-net-california-housing

A wide & deep neural network trained on the California Housing dataset.

It takes eight inputs: `'MedInc'`, `'HouseAge'`, `'AveRooms'`, `'AveBedrms'`, `'Population'`, `'AveOccup'`, `'Latitude'` and `'Longitude'`. It predicts `'MedHouseVal'`.

It is a PyTorch adaptation of the TensorFlow model in Chapter 10 of Aurelien Geron's book 'Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow'.

![](https://raw.githubusercontent.com/sambitmukherjee/handson-ml3-pytorch/main/chapter10/Figure_10-13.png)

Code: https://github.com/sambitmukherjee/handson-ml3-pytorch/blob/main/chapter10/wide_and_deep_net_california_housing.ipynb

Experiment tracking: https://wandb.ai/sadhaklal/wide-and-deep-net-california-housing

## Usage

```
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing(as_frame=True)

from sklearn.model_selection import train_test_split

X_train_full, X_test, y_train_full, y_test = train_test_split(housing['data'], housing['target'], test_size=0.25, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, test_size=0.25, random_state=42)

X_means, X_stds = X_train.mean(axis=0), X_train.std(axis=0)
X_train = (X_train - X_means) / X_stds
X_valid = (X_valid - X_means) / X_stds
X_test = (X_test - X_means) / X_stds

import torch

device = torch.device("cpu")

import torch.nn as nn
from huggingface_hub import PyTorchModelHubMixin

class WideAndDeepNet(nn.Module, PyTorchModelHubMixin):
    def __init__(self):
        super().__init__()
        self.hidden1 = nn.Linear(8, 30)
        self.hidden2 = nn.Linear(30, 30)
        self.output = nn.Linear(38, 1)

    def forward(self, x):
        act = torch.relu(self.hidden1(x))
        act = torch.relu(self.hidden2(act))
        concat = torch.cat([x, act], axis=1)
        return self.output(concat)

model = WideAndDeepNet.from_pretrained("sadhaklal/wide-and-deep-net-california-housing")
model.to(device)
model.eval()

# Let's predict on 3 unseen examples from the test set:
print(f"Ground truth housing prices: {y_test.values[:3]}")
x_new = torch.tensor(X_test.values[:3], dtype=torch.float32)
x_new = x_new.to(device)
with torch.no_grad():
    preds = model(x_new)
print(f"Predicted housing prices: {preds.squeeze()}")
```

## Metric

RMSE on the test set: 0.546

---

This model has been pushed to the Hub using the [PyTorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration.