Commit
·
fee4a7e
1
Parent(s):
d3ef89c
feature: added inference test
Browse files- agent/backend/data.py +5 -2
- agent/backend/utils.py +78 -4
- agent/dashboard/__init__.py +1 -1
- agent/dashboard/training.py +5 -1
agent/backend/data.py
CHANGED
|
@@ -6,12 +6,15 @@ import numpy as np
|
|
| 6 |
class ExplorationDataset(Dataset):
|
| 7 |
def __init__(self, df: pd.DataFrame,
|
| 8 |
input_cols,
|
| 9 |
-
output_cols):
|
| 10 |
super().__init__()
|
| 11 |
self.df = df
|
| 12 |
self.input_cols = input_cols
|
| 13 |
self.output_cols = output_cols
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
self.input_transformed = torch.tensor(self.input_transform(df[input_cols]).values).to(torch.float)
|
| 16 |
self.output_transformed = torch.tensor(self.output_transform(df[output_cols]).values).to(torch.float)
|
| 17 |
|
|
|
|
| 6 |
class ExplorationDataset(Dataset):
|
| 7 |
def __init__(self, df: pd.DataFrame,
|
| 8 |
input_cols,
|
| 9 |
+
output_cols, transform_dict = None):
|
| 10 |
super().__init__()
|
| 11 |
self.df = df
|
| 12 |
self.input_cols = input_cols
|
| 13 |
self.output_cols = output_cols
|
| 14 |
+
if transform_dict is None:
|
| 15 |
+
self.transform_dict = self.transform_fit(df)
|
| 16 |
+
else:
|
| 17 |
+
self.transform_dict = transform_dict
|
| 18 |
self.input_transformed = torch.tensor(self.input_transform(df[input_cols]).values).to(torch.float)
|
| 19 |
self.output_transformed = torch.tensor(self.output_transform(df[output_cols]).values).to(torch.float)
|
| 20 |
|
agent/backend/utils.py
CHANGED
|
@@ -3,22 +3,51 @@ from tqdm import tqdm
|
|
| 3 |
from torch.utils.data import Dataset, DataLoader
|
| 4 |
from functools import partial
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from .data import ExplorationDataset
|
| 8 |
from .models import Perceptron
|
| 9 |
from .loss import loss_mape
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
batch_size_trn, batch_size_val, optimizer_name, learning_rate,
|
| 15 |
max_epoch, loss_name, seed):
|
| 16 |
torch.manual_seed(seed)
|
|
|
|
|
|
|
| 17 |
if model_name == "Perceptron":
|
| 18 |
model = Perceptron(in_features=len(input_cols), out_features=len(output_cols))
|
| 19 |
if loss_name == "mape":
|
| 20 |
loss_fn = loss_mape
|
| 21 |
-
ds = ExplorationDataset(df, input_cols=input_cols, output_cols=output_cols)
|
| 22 |
|
| 23 |
trn_size = int(len(ds)*trn_ratio)
|
| 24 |
val_size = len(ds) - trn_size
|
|
@@ -103,6 +132,7 @@ def predict_dataloader(model, dataloader):
|
|
| 103 |
predictions = torch.cat([predictions, y_pred], dim=0)
|
| 104 |
targets = torch.cat([targets, y], dim=0)
|
| 105 |
return predictions, targets
|
|
|
|
| 106 |
|
| 107 |
def evaluate(model, dataloader, loss_fn):
|
| 108 |
with torch.no_grad():
|
|
@@ -140,3 +170,47 @@ def update_policy(model, rewards, log_probabilities, gamma, learning_rate, optim
|
|
| 140 |
# policy_gradient.backward()
|
| 141 |
policy_gradient.backward(retain_graph=True)
|
| 142 |
optimizer.step()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from torch.utils.data import Dataset, DataLoader
|
| 4 |
from functools import partial
|
| 5 |
import pandas as pd
|
| 6 |
+
from typing import Dict, Union, List
|
| 7 |
+
import itertools
|
| 8 |
+
import solara
|
| 9 |
|
| 10 |
from .data import ExplorationDataset
|
| 11 |
from .models import Perceptron
|
| 12 |
from .loss import loss_mape
|
| 13 |
|
| 14 |
+
def predict_dict(model, ds, inputs: Dict[str, Union[List[int], List[float]]]):
|
| 15 |
+
combinations = itertools.product(*inputs.values())
|
| 16 |
+
input_cols = list(inputs.keys())
|
| 17 |
+
|
| 18 |
+
input_for_df = {col: [] for col in input_cols}
|
| 19 |
+
for combination in combinations:
|
| 20 |
+
for input_col, value in zip(input_cols, combination):
|
| 21 |
+
input_for_df[input_col].append(value)
|
| 22 |
+
|
| 23 |
+
input_df = pd.DataFrame(input_for_df)
|
| 24 |
+
input_df_transformed = ds.input_transform(input_df)
|
| 25 |
+
#print(input_df)
|
| 26 |
+
#print(input_df_transformed)
|
| 27 |
+
ds_new = ExplorationDataset(input_df, input_cols=ds.input_cols,
|
| 28 |
+
output_cols=[], transform_dict=ds.transform_dict)
|
| 29 |
+
dl_new = DataLoader(ds_new, batch_size=len(input_df), shuffle=False)
|
| 30 |
+
for x, y in dl_new:
|
| 31 |
+
output = model.forward(x)
|
| 32 |
+
output_for_df = {col_name: output[:,col_index].detach().numpy() for col_index, col_name in enumerate(ds.output_cols)}
|
| 33 |
+
output_df = pd.DataFrame(output_for_df)
|
| 34 |
+
output_df_transformed = ds.output_inv_transform(output_df)
|
| 35 |
+
#print(output_df)
|
| 36 |
+
#print(output_df_transformed)
|
| 37 |
+
return input_df, output_df_transformed
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def train(ds: ExplorationDataset, model_name, trn_ratio,
|
| 42 |
batch_size_trn, batch_size_val, optimizer_name, learning_rate,
|
| 43 |
max_epoch, loss_name, seed):
|
| 44 |
torch.manual_seed(seed)
|
| 45 |
+
input_cols, output_cols = ds.input_cols, ds.output_cols
|
| 46 |
+
df = ds.df
|
| 47 |
if model_name == "Perceptron":
|
| 48 |
model = Perceptron(in_features=len(input_cols), out_features=len(output_cols))
|
| 49 |
if loss_name == "mape":
|
| 50 |
loss_fn = loss_mape
|
|
|
|
| 51 |
|
| 52 |
trn_size = int(len(ds)*trn_ratio)
|
| 53 |
val_size = len(ds) - trn_size
|
|
|
|
| 132 |
predictions = torch.cat([predictions, y_pred], dim=0)
|
| 133 |
targets = torch.cat([targets, y], dim=0)
|
| 134 |
return predictions, targets
|
| 135 |
+
|
| 136 |
|
| 137 |
def evaluate(model, dataloader, loss_fn):
|
| 138 |
with torch.no_grad():
|
|
|
|
| 170 |
# policy_gradient.backward()
|
| 171 |
policy_gradient.backward(retain_graph=True)
|
| 172 |
optimizer.step()
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
@solara.component
|
| 176 |
+
def Plot1D(x: List, y: List, title='title',xlabel='xlabel', ylabel='ylabel', force_render=0):
|
| 177 |
+
options = {
|
| 178 |
+
'title': {
|
| 179 |
+
'text': title,
|
| 180 |
+
'left': 'center'},
|
| 181 |
+
'tooltip': {
|
| 182 |
+
'trigger': 'axis',
|
| 183 |
+
'axisPointer': {
|
| 184 |
+
'type': 'cross'
|
| 185 |
+
}
|
| 186 |
+
},
|
| 187 |
+
'xAxis': {
|
| 188 |
+
'axisTick': {
|
| 189 |
+
'alignWithLabel': True
|
| 190 |
+
},
|
| 191 |
+
'data': x,
|
| 192 |
+
'name': xlabel,
|
| 193 |
+
'nameLocation': 'middle',
|
| 194 |
+
'nameTextStyle': {'verticalAlign': 'top','padding': [10, 0, 0, 0]}
|
| 195 |
+
},
|
| 196 |
+
'yAxis': [
|
| 197 |
+
{
|
| 198 |
+
'type': 'value',
|
| 199 |
+
'name': ylabel,
|
| 200 |
+
'position': 'left',
|
| 201 |
+
'alignTicks': True,
|
| 202 |
+
'axisLine': {
|
| 203 |
+
'show': True,
|
| 204 |
+
'lineStyle': {'color': 'green'}}
|
| 205 |
+
},
|
| 206 |
+
],
|
| 207 |
+
'series': [
|
| 208 |
+
{
|
| 209 |
+
'name': ylabel,
|
| 210 |
+
'data': y,
|
| 211 |
+
'type': 'line',
|
| 212 |
+
'yAxisIndex': 1
|
| 213 |
+
},
|
| 214 |
+
],
|
| 215 |
+
}
|
| 216 |
+
solara.FigureEcharts(option=options)
|
agent/dashboard/__init__.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import solara
|
| 2 |
|
| 3 |
-
route_order = ["/","data","training","testing"]
|
| 4 |
|
| 5 |
@solara.component
|
| 6 |
def Page():
|
|
|
|
| 1 |
import solara
|
| 2 |
|
| 3 |
+
route_order = ["/","data","training","testing","inference"]
|
| 4 |
|
| 5 |
@solara.component
|
| 6 |
def Page():
|
agent/dashboard/training.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
from typing import Optional, cast
|
| 4 |
import solara.express as solara_px
|
| 5 |
from .data import state
|
|
|
|
| 6 |
from ..backend.utils import train
|
| 7 |
from ..backend.loss import loss_mape
|
| 8 |
|
|
@@ -21,6 +22,7 @@ local_state = solara.reactive(
|
|
| 21 |
'loss_plot_data': solara.reactive({'epoch': [], 'trn_loss': [], 'val_loss': []}),
|
| 22 |
'render_count': solara.reactive(0),
|
| 23 |
'model': solara.reactive(None),
|
|
|
|
| 24 |
'seed': solara.reactive(42),
|
| 25 |
}
|
| 26 |
)
|
|
@@ -85,7 +87,9 @@ def ExecutePanel(df):
|
|
| 85 |
epoch_list = []
|
| 86 |
trn_loss_list = []
|
| 87 |
val_loss_list = []
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
batch_size_trn, batch_size_val, optimizer_name, learning_rate,
|
| 90 |
max_epoch, loss_name, seed):
|
| 91 |
epoch_list.append(epoch)
|
|
|
|
| 3 |
from typing import Optional, cast
|
| 4 |
import solara.express as solara_px
|
| 5 |
from .data import state
|
| 6 |
+
from ..backend.data import ExplorationDataset
|
| 7 |
from ..backend.utils import train
|
| 8 |
from ..backend.loss import loss_mape
|
| 9 |
|
|
|
|
| 22 |
'loss_plot_data': solara.reactive({'epoch': [], 'trn_loss': [], 'val_loss': []}),
|
| 23 |
'render_count': solara.reactive(0),
|
| 24 |
'model': solara.reactive(None),
|
| 25 |
+
'ds': solara.reactive(None),
|
| 26 |
'seed': solara.reactive(42),
|
| 27 |
}
|
| 28 |
)
|
|
|
|
| 87 |
epoch_list = []
|
| 88 |
trn_loss_list = []
|
| 89 |
val_loss_list = []
|
| 90 |
+
ds = ExplorationDataset(dff, input_cols, output_cols)
|
| 91 |
+
local_state.value['ds'].set(ds)
|
| 92 |
+
for epoch, trn_loss, val_loss, model in train(ds, "Perceptron", trn_ratio,
|
| 93 |
batch_size_trn, batch_size_val, optimizer_name, learning_rate,
|
| 94 |
max_epoch, loss_name, seed):
|
| 95 |
epoch_list.append(epoch)
|