-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
149 lines (140 loc) · 4.43 KB
/
Copy pathbasic.py
File metadata and controls
149 lines (140 loc) · 4.43 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Disable output of logging messages from TensorFlow if it's too noisy
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
import os.path
import pandas as pd
from ludwig.api import LudwigModel
from ludwig import visualize
# Definition of input features, for encoder see: https://ludwig-ai.github.io/ludwig-docs/user_guide/#stacked-parallel-cnn-encoder
input_features = [
{
'name': 'subject',
'type': 'text',
'preprocessing': {
'lowercase': True,
},
},
{
'name': 'content',
'type': 'text',
'preprocessing': {
'lowercase': True,
},
'encoder': 'stacked_parallel_cnn',
'reduce_output': 'sum',
'activation': 'relu',
},
]
# Definition of output features
output_features = [
{
'name': 'spam',
'type': 'category',
}
]
base_model_definition = {
'input_features': input_features,
'output_features': output_features,
}
def load_or_create_model(model_dir, model_config, **model_kwargs):
if os.path.exists(model_dir):
print('Loading the model: {}'.format(model_dir))
return (LudwigModel.load(model_dir, **model_kwargs), True)
else:
print('Defining the model')
return (LudwigModel(model_config, **model_kwargs), False)
base_model, base_model_loaded = load_or_create_model(
'trained/basic', base_model_definition, gpus=[0], gpu_memory_limit=2000
)
if not base_model_loaded:
print('Training the model')
base_model.train(
training_set='./datasets/spam_train.csv',
test_set='./datasets/spam_test.csv',
skip_save_processed_input=True,
)
base_model.save('trained/basic')
stats = base_model.evaluate(
dataset='./datasets/spam_test.csv'
)[0]
print(stats)
print('Creating a 2nd model classifier with some adjustments')
other_model_definition = base_model_definition.copy()
other_model_definition['input_features'] = [
{
'name': 'subject',
'type': 'text',
'preprocessing': {
'lowercase': True,
},
},
{
'name': 'content',
'type': 'text',
'preprocessing': {
'lowercase': True,
},
'encoder': 'bert', # See: https://ludwig-ai.github.io/ludwig-docs/user_guide/#bert-encoder
'reduce_output': 'avg',
'activation': 'tanh', # Activation used for *all* layers.
'num_filters': 64,
'stacked_layers': [
[
{ 'filter_size': 2 },
{ 'filter_size': 3 },
{ 'filter_size': 4 },
{ 'filter_size': 5 }
],
[
{ 'filter_size': 2 },
{ 'filter_size': 3 },
{ 'filter_size': 4 },
{ 'filter_size': 5 }
],
[
{ 'filter_size': 2 },
{ 'filter_size': 3 },
{ 'filter_size': 4 },
{ 'filter_size': 5 }
]
],
},
]
other_model, other_model_loaded = load_or_create_model(
'trained/basic_other', other_model_definition, gpus=[0], gpu_memory_limit=2000
)
if not other_model_loaded:
print('Training the model')
other_model.train(
training_set='./datasets/spam_train.csv',
test_set='./datasets/spam_test.csv',
skip_save_processed_input=True,
)[0]['training']
other_model.save('trained/basic_other')
other_stats = other_model.evaluate(
dataset='./datasets/spam_test.csv'
)[0]
print(other_stats)
# Comparing the testing results of the models
visualize.compare_performance(
test_stats_per_model=[stats, other_stats],
output_feature_name='spam',
model_names=['Base Model', 'Other Model'],
)
def print_predictions(unpredicted_emails, model):
prediction_result, output_directory = model.predict(dataset=unpredicted_emails)
emails = unpredicted_emails.join(prediction_result)
for index, row in emails.iterrows():
print('{} ({:.6f}): {} / {}'.format(
row.get('spam_predictions'),
row.get('spam_probability'),
row.get('subject')[0:30],
row.get('content')[0:30],
))
unpredicted_emails = pd.read_csv('./datasets/spam_unpredicted.csv')
print('Prediction Results for Base Model')
print_predictions(unpredicted_emails, base_model)
print('Prediction Results for Other Model')
print_predictions(unpredicted_emails, other_model)