Skip to content

Commit

Permalink
feat: Add CNN model
Browse files Browse the repository at this point in the history
  • Loading branch information
james.chan committed Oct 17, 2023
1 parent 1e335dd commit b61b64f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
6 changes: 4 additions & 2 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from model.model import HousePriceModel, TransformerRegressor
from model.model_v2 import HousePriceModel_CNN
from dataloader import HousePriceTestDataset, min_max_denormalize, z_score_denormalize
import platform

Expand Down Expand Up @@ -41,8 +42,9 @@ def inference():
# Load Model
model_path = 'model.pth' # Update with the path of your trained model file
input_dim = len(normalize_columns.keys())
model = HousePriceModel(input_dim)
model = TransformerRegressor(input_dim, 4, 6)
# model = HousePriceModel(input_dim)
# model = TransformerRegressor(input_dim, 4, 6)
model = HousePriceModel_CNN(input_dim)
if gpu:
model = model.cuda()
else:
Expand Down
52 changes: 52 additions & 0 deletions model/model_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import torch
# from torch.utils.data import DataLoader
import torch.nn as nn
# from torch.autograd import Variable
# import matplotlib.pyplot as plt
# import torchvision
# from dataloader import getDataset
# import os
# import numpy as np
# from tqdm import tqdm
# import math



class HousePriceModel_CNN(nn.Module):
def __init__(self, input_dim):
super(HousePriceModel_CNN, self).__init__()
self.input_dim = input_dim
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels = self.input_dim, out_channels = 32, kernel_size = 3, stride = 1, padding = 1,),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 9, stride = 1, padding = 6),# stride = 1, padding = (kernel_size-1)/2 = (5-1)/2
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 9, stride = 1, padding = 5,),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = (3,3), stride = 1, padding = 1,),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 64, out_channels =32, kernel_size = (1,1), stride = 1, padding = 0,),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 5, stride = 1, padding = 0,),
nn.LeakyReLU(0.1),
# nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = (3,3), stride = 1, padding = 0,),
# nn.LeakyReLU(0.1),
# nn.Conv2d(in_channels = 64, out_channels = 32, kernel_size = (1,1), stride = 1, padding = 0,),
# nn.LeakyReLU(0.1),
# nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = (3,3), stride = 1, padding = 0,),
# nn.LeakyReLU(0.1),
# nn.Conv2d(in_channels = 64, out_channels = 32, kernel_size = (1,1), stride = 1, padding = 0,),
# nn.LeakyReLU(0.1),
nn.Conv2d(in_channels = 32, out_channels = 16, kernel_size = (1,1), stride = 1, padding = 0,),
nn.Conv2d(in_channels = 16, out_channels = 1, kernel_size = (1,1), stride = 1, padding = 0,),
)
# self.sigmoid = nn.Sigmoid()


def forward(self, x):
x = torch.reshape(x,(-1,self.input_dim,1,1))
x = self.conv1(x)
x = torch.reshape(x,(-1,1,1,1))
# print(x.shape)

return x
8 changes: 6 additions & 2 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from torch.utils.data import Dataset, DataLoader
from sklearn.preprocessing import StandardScaler
from model.model import HousePriceModel, TransformerRegressor
from model.model_v2 import HousePriceModel_CNN
from dataloader import HousePriceTrainDataset
import platform

Expand All @@ -20,7 +21,7 @@ def main():
'移轉層次': 'min-max',
'總樓層數': 'min-max',
'屋齡': 'min-max',
# '建物面積': 'min-max',
'建物面積': 'min-max',
'車位面積': 'min-max',
'車位個數': 'min-max',
'橫坐標': 'min-max', #z-score
Expand All @@ -44,7 +45,8 @@ def main():
print(input_dim)
# Initialize model
# model = HousePriceModel(input_dim)
model = TransformerRegressor(input_dim, 4, 11)
# model = TransformerRegressor(input_dim, 4, 6)
model = HousePriceModel_CNN(input_dim)

if gpu:
model = model.cuda()
Expand All @@ -58,6 +60,8 @@ def main():
# Training loop
for epoch in range(epochs):
for batch in train_loader:
# print(batch['features'].shape)
# print(batch['target'].shape)
if gpu:
data = batch['features'].cuda()
targets = batch['target'].cuda()
Expand Down

0 comments on commit b61b64f

Please sign in to comment.