In [1]:
import os
import sys
root_path = os.path.abspath("../../../")
if root_path not in sys.path:
    sys.path.append(root_path)

from Util.Util import DataUtil

x_cv = y_cv = None
(x, y), (x_test, y_test) = DataUtil.gen_noisy_linear(one_hot=False)

In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (18, 8)

def draw_acc(*models):
    plt.figure()
    for nn in models:
        name = str(nn)
        el, tl = nn.log["train_acc"], nn.log["test_acc"]
        ee_base = np.arange(len(el))
        cse_base = np.linspace(0, len(el) - 1, len(tl))
        plt.plot(ee_base, el, label="Train acc ({})".format(name))
        plt.plot(cse_base, tl, label="Test acc ({})".format(name))
    plt.ylim(0.6, 1.05)
    plt.legend()
    plt.show()

In [3]:
from _Dist.NeuralNetworks.c_BasicNN.NN import Basic

base_params = {
    "model_param_settings": {"n_epoch": 60, "max_epoch": 60, "metric": "acc"},
}
basic = Basic(**base_params).fit(x, y, x_test, y_test, snapshot_ratio=0)


Epoch      0   Iter        0   Snapshot      0 (acc)  -  Train : 0.531000   Test : 0.514000
Epoch     -1   Iter       -1   Snapshot     -1 (acc)  -  Train : 1.000000   Test : 0.865333  -  Time Cost: 6.677941560745239

In [4]:
from _Dist.NeuralNetworks.e_AdvancedNN.NN import Advanced

numerical_idx = [True] * 100 + [False]
categorical_columns = []
advanced_params = {
    "data_info": {"numerical_idx": numerical_idx, "categorical_columns": categorical_columns},
    "model_structure_settings": {"use_dndf": False, "use_pruner": False}
}
advanced_params.update(base_params)

In [5]:
wnd = Advanced(**advanced_params).fit(x, y, x_test, y_test, snapshot_ratio=0)


====================================================================================================
This is a 2-classes problem
----------------------------------------------------------------------------------------------------
Data     : 10000 training samples, 1500 test samples
Features : 0 categorical, 100 numerical
====================================================================================================
Deep model: DNN
Deep model input: Continuous features only
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Using dropout with keep_prob = 0.5
Training without batch norm
Hidden units: [512, 512]
====================================================================================================
Wide model: logistic regression
Wide model input: Continuous features only
----------------------------------------------------------------------------------------------------
====================================================================================================
Hyper parameters
----------------------------------------------------------------------------------------------------
This is a Wide & Deep model
----------------------------------------------------------------------------------------------------
Activation       : ['relu', 'relu']
Batch size       : 128
Epoch num        : 60
Optimizer        : Adam
Metric           : acc
Loss             : cross_entropy
lr               : 0.001
----------------------------------------------------------------------------------------------------
Pruner           : None
----------------------------------------------------------------------------------------------------

Epoch      0   Iter        0   Snapshot      0 (acc)  -  Train : 0.554000   Test : 0.570000
Epoch     -1   Iter       -1   Snapshot     -1 (acc)  -  Train : 1.000000   Test : 0.896667  -  Time Cost: 8.59854769706726

In [6]:
advanced_params["model_structure_settings"]["use_dndf"] = True
wnd_dndf = Advanced(**advanced_params).fit(x, y, x_test, y_test, snapshot_ratio=0)


c:\users\carefree0910\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py:96: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
====================================================================================================
This is a 2-classes problem
----------------------------------------------------------------------------------------------------
Data     : 10000 training samples, 1500 test samples
Features : 0 categorical, 100 numerical
====================================================================================================
Deep model: DNN
Deep model input: Continuous features only
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Using dropout with keep_prob = 0.5
Training without batch norm
Hidden units: [512, 512]
====================================================================================================
Wide model: DNDF
Wide model input: Continuous features only
----------------------------------------------------------------------------------------------------
Using DNDF with n_tree = 10, tree_depth = 4
====================================================================================================
Hyper parameters
----------------------------------------------------------------------------------------------------
This is a hybrid model
----------------------------------------------------------------------------------------------------
Activation       : ['relu', 'relu']
Batch size       : 128
Epoch num        : 60
Optimizer        : Adam
Metric           : acc
Loss             : cross_entropy
lr               : 0.001
----------------------------------------------------------------------------------------------------
Pruner           : None
----------------------------------------------------------------------------------------------------

Epoch      0   Iter        0   Snapshot      0 (acc)  -  Train : 0.514000   Test : 0.507333
Epoch     -1   Iter       -1   Snapshot     -1 (acc)  -  Train : 1.000000   Test : 0.896667  -  Time Cost: 31.714452028274536

In [7]:
advanced_params["model_structure_settings"]["use_pruner"] = True
wnd_dndf_pruned = Advanced(**advanced_params).fit(x, y, x_test, y_test, snapshot_ratio=0)


c:\users\carefree0910\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py:96: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
====================================================================================================
This is a 2-classes problem
----------------------------------------------------------------------------------------------------
Data     : 10000 training samples, 1500 test samples
Features : 0 categorical, 100 numerical
====================================================================================================
Deep model: DNN
Deep model input: Continuous features only
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Using dropout with keep_prob = 0.5
Training without batch norm
Hidden units: [512, 512]
====================================================================================================
Wide model: DNDF
Wide model input: Continuous features only
----------------------------------------------------------------------------------------------------
Using DNDF with n_tree = 10, tree_depth = 4
====================================================================================================
Hyper parameters
----------------------------------------------------------------------------------------------------
This is a hybrid model
----------------------------------------------------------------------------------------------------
Activation       : ['relu', 'relu']
Batch size       : 128
Epoch num        : 60
Optimizer        : Adam
Metric           : acc
Loss             : cross_entropy
lr               : 0.001
----------------------------------------------------------------------------------------------------
Pruner           : 
-> alpha         : 0.01
-> beta          : 1
-> eps           : 1e-12
-> gamma         : 1
-> max_ratio     : 1.0
-> method        : soft_prune
----------------------------------------------------------------------------------------------------

Epoch      0   Iter        0   Snapshot      0 (acc)  -  Train : 0.513000   Test : 0.509333
Epoch     -1   Iter       -1   Snapshot     -1 (acc)  -  Train : 0.993000   Test : 0.916667  -  Time Cost: 38.59485220909119

In [8]:
print("BasicNN              ", end="")
basic.evaluate(x, y, x_cv, y_cv, x_test, y_test)
print("WnD                  ", end="")
wnd.evaluate(x, y, x_cv, y_cv, x_test, y_test)
print("WnD & DNDF           ", end="")
wnd_dndf.evaluate(x, y, x_cv, y_cv, x_test, y_test)
print("WnD & DNDF & Pruner  ", end="")
wnd_dndf_pruned.evaluate(x, y, x_cv, y_cv, x_test, y_test)
draw_acc(basic, wnd_dndf, wnd_dndf_pruned)


BasicNN              acc  -  Train : 1.00000000   CV : None   Test : 0.86533333
WnD                  acc  -  Train : 0.99910000   CV : None   Test : 0.89666667
WnD & DNDF           acc  -  Train : 1.00000000   CV : None   Test : 0.89666667
WnD & DNDF & Pruner  acc  -  Train : 0.99540000   CV : None   Test : 0.91666667