In [1]:
from physlearn.NeuralNet.NeuralNetPro import NeuralNetPro
from physlearn.examples import Titanic
from physlearn.Optimizer import NelderMead
from physlearn.Optimizer import DifferentialEvolution
import tensorflow as tf
import matplotlib.pyplot as plt
import sys
import os
%matplotlib inline

In [20]:
(learn_data, learn_output), (cv_data, cv_output) = Titanic.create_datasets(0.2)

In [21]:
net = NeuralNetPro(-5, 5)

In [22]:
net.add_input_layer(3)
net.add(5, net.sigmoid)
net.add_output_layer(1, net.sigmoid)

In [23]:
net.compile()

In [24]:
net.set_train_type('logistic')

In [25]:
net.set_random_matrixes()

In [26]:
dim = net.return_unroll_dim()

In [9]:
net.calculate_cost(learn_data, learn_output)


Out[9]:
2519.1855310211622

In [10]:
def cost(params):
    net.roll_matrixes(params)
    return net.calculate_cost(learn_data, learn_output)

In [17]:
#res = NelderMead.optimize(cost, dim, 3000, end_method='max_iter', min_element=-7, max_element=7)
res = DifferentialEvolution.optimize(cost, 100, dim, 100)


100%|██████████| 3000/3000 [03:55<00:00, 12.74it/s]

In [29]:
net.roll_matrixes(res)

In [30]:
net.calculate_cost(learn_data, learn_output)


Out[30]:
243.64311056236718

In [31]:
net.calculate_cost(cv_data, cv_output)


Out[31]:
186.80435917527475

In [32]:
ok_class = 0
output = net.run(learn_data)[0]
pred_clases = []
for item in output:
    if item >= 0.5:
        pred_clases.append(1)
    else:
        pred_clases.append(0)
for index, _ in enumerate(pred_clases):
    if pred_clases[index] == learn_output[0][index]:
        ok_class += 1
        
ok_percent = (ok_class / output.shape[0]) * 100
print('ok percent on learn data: ', ok_percent, '%')


ok percent on learn data:  80.56042031523643 %

In [33]:
ok_class = 0
output = net.run(cv_data)[0]
pred_clases = []
for item in output:
    if item >= 0.5:
        pred_clases.append(1)
    else:
        pred_clases.append(0)
for index, _ in enumerate(pred_clases):
    if pred_clases[index] == cv_output[0][index]:
        ok_class += 1
        
ok_percent = (ok_class / output.shape[0]) * 100
print('ok percent on cv data: ', ok_percent, '%')


ok percent on cv data:  79.72027972027972 %

In [ ]: