Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jantic
GitHub Repository: jantic/deoldify
Path: blob/master/fastai/tabular/models.py
781 views
1
from ..torch_core import *
2
from ..layers import *
3
from ..basic_data import *
4
from ..basic_train import *
5
from ..train import ClassificationInterpretation
6
7
__all__ = ['TabularModel']
8
9
class TabularModel(Module):
10
"Basic model for tabular data."
11
def __init__(self, emb_szs:ListSizes, n_cont:int, out_sz:int, layers:Collection[int], ps:Collection[float]=None,
12
emb_drop:float=0., y_range:OptRange=None, use_bn:bool=True, bn_final:bool=False):
13
super().__init__()
14
ps = ifnone(ps, [0]*len(layers))
15
ps = listify(ps, layers)
16
self.embeds = nn.ModuleList([embedding(ni, nf) for ni,nf in emb_szs])
17
self.emb_drop = nn.Dropout(emb_drop)
18
self.bn_cont = nn.BatchNorm1d(n_cont)
19
n_emb = sum(e.embedding_dim for e in self.embeds)
20
self.n_emb,self.n_cont,self.y_range = n_emb,n_cont,y_range
21
sizes = self.get_sizes(layers, out_sz)
22
actns = [nn.ReLU(inplace=True) for _ in range(len(sizes)-2)] + [None]
23
layers = []
24
for i,(n_in,n_out,dp,act) in enumerate(zip(sizes[:-1],sizes[1:],[0.]+ps,actns)):
25
layers += bn_drop_lin(n_in, n_out, bn=use_bn and i!=0, p=dp, actn=act)
26
if bn_final: layers.append(nn.BatchNorm1d(sizes[-1]))
27
self.layers = nn.Sequential(*layers)
28
29
def get_sizes(self, layers, out_sz):
30
return [self.n_emb + self.n_cont] + layers + [out_sz]
31
32
def forward(self, x_cat:Tensor, x_cont:Tensor) -> Tensor:
33
if self.n_emb != 0:
34
x = [e(x_cat[:,i]) for i,e in enumerate(self.embeds)]
35
x = torch.cat(x, 1)
36
x = self.emb_drop(x)
37
if self.n_cont != 0:
38
x_cont = self.bn_cont(x_cont)
39
x = torch.cat([x, x_cont], 1) if self.n_emb != 0 else x_cont
40
x = self.layers(x)
41
if self.y_range is not None:
42
x = (self.y_range[1]-self.y_range[0]) * torch.sigmoid(x) + self.y_range[0]
43
return x
44
45
@classmethod
46
def _cl_int_from_learner(cls, learn:Learner, ds_type=DatasetType.Valid, activ:nn.Module=None):
47
"Creates an instance of 'ClassificationInterpretation"
48
preds = learn.get_preds(ds_type=ds_type, activ=activ, with_loss=True)
49
return cls(learn, *preds, ds_type=ds_type)
50
51
def _cl_int_plot_top_losses(self, k, largest:bool=True, return_table:bool=False)->Optional[plt.Figure]:
52
"Generates a dataframe of 'top_losses' along with their prediction, actual, loss, and probability of the actual class."
53
tl_val, tl_idx = self.top_losses(k, largest)
54
classes = self.data.classes
55
cat_names = self.data.x.cat_names
56
cont_names = self.data.x.cont_names
57
df = pd.DataFrame(columns=[['Prediction', 'Actual', 'Loss', 'Probability'] + cat_names + cont_names])
58
for i, idx in enumerate(tl_idx):
59
da, cl = self.data.dl(self.ds_type).dataset[idx]
60
cl = int(cl)
61
t1 = str(da)
62
t1 = t1.split(';')
63
arr = []
64
arr.extend([classes[self.pred_class[idx]], classes[cl], f'{self.losses[idx]:.2f}',
65
f'{self.preds[idx][cl]:.2f}'])
66
for x in range(len(t1)-1):
67
_, value = t1[x].rsplit(' ', 1)
68
arr.append(value)
69
df.loc[i] = arr
70
display(df)
71
return_fig = return_table
72
if ifnone(return_fig, defaults.return_fig): return df
73
74
75
ClassificationInterpretation.from_learner = _cl_int_from_learner
76
ClassificationInterpretation.plot_top_losses = _cl_int_plot_top_losses
77
78
def _learner_interpret(learn:Learner, ds_type:DatasetType = DatasetType.Valid):
79
"Create a 'ClassificationInterpretation' object from 'learner' on 'ds_type'."
80
return ClassificationInterpretation.from_learner(learn, ds_type=ds_type)
81
82
Learner.interpret = _learner_interpret
83
84