Views
No views yet
MedHouseVal). Mô hình được xây dựng bằng PyTorch, dựa trên kiến trúc trong cuốn Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow của Aurélien Géron.[batch_size, 8]torch.FloatTensor'MedInc' – Thu nhập trung vị'HouseAge' – Tuổi trung bình của căn nhà'AveRooms' – Số phòng trung bình'AveBedrms' – Số phòng ngủ trung bình'Population' – Dân số'AveOccup' – Số người trung bình trên mỗi hộ'Latitude' – Vĩ độ'Longitude' – Kinh độtorch.FloatTensor có shape [batch_size, 1]1import torch
2import torch.nn as nn
3from huggingface_hub import PyTorchModelHubMixin
4
5# Tạo dữ liệu đầu vào giả lập (batch 1, 8 features)
6x_input = torch.randn(1, 8)
7print("Mock input:")
8print(x_input)
9
10# Định nghĩa mô hình Wide & Deep Neural Network
11class WideAndDeepNet(nn.Module, PyTorchModelHubMixin):
12 def __init__(self):
13 super().__init__()
14 self.hidden1 = nn.Linear(6, 30)
15 self.hidden2 = nn.Linear(30, 30)
16 self.main_head = nn.Linear(35, 1)
17 self.aux_head = nn.Linear(30, 1)
18 self.main_loss_fn = nn.MSELoss(reduction='sum')
19 self.aux_loss_fn = nn.MSELoss(reduction='sum')
20
21 def forward(self, input_wide, input_deep, label=None):
22 act = torch.relu(self.hidden1(input_deep))
23 act = torch.relu(self.hidden2(act))
24 concat = torch.cat([input_wide, act], dim=1)
25 main_output = self.main_head(concat)
26 aux_output = self.aux_head(act)
27 if label is not None:
28 main_loss = self.main_loss_fn(main_output.squeeze(), label)
29 aux_loss = self.aux_loss_fn(aux_output.squeeze(), label)
30 return WideAndDeepNetOutput(main_output=main_output, aux_output=aux_output)
31
32# Tải mô hình từ Hugging Face Hub
33model = WideAndDeepNet.from_pretrained("sadhaklal/wide-and-deep-net-california-housing-v3")
34model.eval()
35
36# Dự đoán với mô hình
37with torch.no_grad():
38 prediction = model(x_input)
39
40print(f"Giá nhà dự đoán (mock input): {prediction.item():.3f}")