ATL_Python - first commit

This commit is contained in:
Marcus Vinicius de Carvalho 2019-12-04 14:18:56 +08:00
parent 16d81b8668
commit 1094164871
10 changed files with 102037 additions and 1 deletions

335
AGMM.py Normal file
View File

@ -0,0 +1,335 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
#
from MyUtil import MyUtil
from MySingletons import MyDevice
import numpy as np
import torch
import math
class GMM:
weight = 1
center = None
variance = None
win_counter = 1
inference_sum = 0
survive_counter = 0
y_count = None
dist = None
_number_features = None
@property
def inference(self):
if self.dist is not None:
return torch.min(self.dist)
return torch.tensor(0)
@property
def hyper_volume(self):
if self.dist is not None:
return self.variance[0][torch.argmin(self.dist)]
return torch.tensor([[0.01]])
@property
def standard_deviation(self):
return torch.sqrt(self.variance)
@property
def std(self):
return self.standard_deviation
@property
def precision(self):
return 1/self.variance
@property
def first_order_moment(self):
return self.center
@property
def second_order_moment(self):
return self.center ** 2 + self.variance
@property
def mode(self):
return self.center
@property
def probX_J(self):
denumerator = torch.sqrt(2 * math.pi * self.hyper_volume)
return denumerator * self.inference
def __init__(self, x):
self._number_features = x.shape[1]
self.center = x
self.variance = 0.01 + torch.zeros(int(self._number_features), dtype=torch.float, device=MyDevice().get())
self.variance = self.variance.view(1, self.variance.shape[0])
def compute_inference(self, x, y=None):
if y is not None:
if self.y_count is None:
self.y_count = y
else:
self.y_count += y
c = self.center
dist = ((x - c) ** 2) / (- 2 * self.variance)
self.dist = torch.exp(dist)
class AGMM:
gmm_array = None
number_samples_feed = 0
rho = 0.1
number_features = None
@property
def hyper_volume(self):
hyper_volume = torch.tensor(0, dtype=torch.float, device=MyDevice().get())
for i in range(len(self.gmm_array)):
hyper_volume += self.gmm_array[i].hyper_volume
return hyper_volume
@property
def weight_sum(self):
weight_sum = torch.tensor(0, dtype=torch.float, device=MyDevice().get())
for i in range(self.M()):
weight_sum += self.gmm_array[i].weight
return weight_sum
def run(self, x, bias2):
self.number_samples_feed += 1
if self.gmm_array is None:
self.gmm_array = [GMM(x)]
self.number_features = x.shape[1]
else:
self.compute_inference(x)
gmm_winner_idx = np.argmax(self.update_weights())
if self.M() > 1:
self.compute_overlap_degree(gmm_winner_idx, 3, 3)
denominator = 1.25 * torch.exp(-bias2) + 0.75 * self.number_features
numerator = 4 - 2 * torch.exp(torch.tensor(-self.number_features / 2.0, dtype=torch.float, device=MyDevice().get()))
threshold = torch.exp(- denominator / numerator)
condition1 = self.gmm_array[gmm_winner_idx].inference < threshold
condition2 = self.gmm_array[gmm_winner_idx].hyper_volume > self.rho * (self.hyper_volume - self.gmm_array[gmm_winner_idx].hyper_volume)
condition3 = self.number_samples_feed > 10
if condition1 and condition2 and condition3:
self.create_cluster(x)
self.gmm_array[-1].variance = (x - self.gmm_array[gmm_winner_idx].center) ** 2
else:
self.update_cluster(x, self.gmm_array[gmm_winner_idx])
def create_cluster(self, x):
self.gmm_array.append(GMM(x))
weight_sum = self.weight_sum
for gmm in self.gmm_array:
gmm.weight = gmm.weight / weight_sum
def update_cluster(self, x, gmm):
gmm.win_counter += 1
gmm.center += (x - gmm.center) / gmm.win_counter
gmm.variance += ((x - gmm.center) ** 2 - gmm.variance) / gmm.win_counter
def delete_cluster(self):
if self.M() <= 1:
return
accumulated_inference = []
for gmm in self.gmm_array:
if gmm.survive_counter > 0:
accumulated_inference.append(gmm.inference_sum / gmm.survive_counter)
accumulated_inference = torch.stack(accumulated_inference)[torch.isnan(torch.stack(accumulated_inference)) == False]
delete_list = torch.where(accumulated_inference <= (torch.mean(accumulated_inference) - 0.5 * torch.std(accumulated_inference)))[0]
if len(delete_list) == len(self.gmm_array):
raise TypeError('problem') # FIXME if this happen, it means you have a great problem at your code
if len(delete_list):
self.gmm_array = np.delete(self.gmm_array, delete_list.cpu().numpy()).tolist()
accumulated_inference = torch.tensor(np.delete(accumulated_inference.cpu().numpy(), delete_list.cpu().numpy()).tolist(), dtype=torch.float, device=MyDevice().get())
sum_weight = 0
for gmm in self.gmm_array:
sum_weight += gmm.weight
if sum_weight == 0:
max_index = torch.argmax(accumulated_inference)
self.gmm_array[max_index].weight += 1
sum_weight = 0
for gmm in self.gmm_array:
sum_weight += gmm.weight
for gmm in self.gmm_array:
gmm.weight = gmm.weight / sum_weight
def compute_inference(self, x, y=None):
for gmm in self.gmm_array:
gmm.compute_inference(x, y)
def update_weights(self):
probX_JprobJ = torch.zeros(len(self.gmm_array))
weights = torch.zeros(len(self.gmm_array))
sum_winner_counter = 0
max_inference = 0
max_inference_index = 0
for i in range(self.M()):
sum_winner_counter += self.gmm_array[i].win_counter
if self.gmm_array[i].inference > max_inference:
max_inference = self.gmm_array[i].inference
max_inference_index = i
for i in range(self.M()):
self.gmm_array[i].inference_sum += self.gmm_array[i].inference
self.gmm_array[i].survive_counter += 1
probJ = self.gmm_array[i].win_counter / sum_winner_counter
probX_JprobJ[i] = self.gmm_array[i].probX_J * probJ
if torch.sum(probX_JprobJ) == 0:
probX_JprobJ[max_inference_index] += 1
for i in range(self.M()):
self.gmm_array[i].weight = float(probX_JprobJ[i] / torch.sum(probX_JprobJ))
weights[i] = self.gmm_array[i].weight
return weights
def compute_overlap_degree(self, gmm_winner_idx, maximum_limit=None, minimum_limit=None):
if maximum_limit is None:
maximum_limit = minimum_limit = 3
elif minimum_limit is None:
minimum_limit = maximum_limit
overlap_coefficient = torch.tensor(1 / self.M(), dtype=torch.float, device=MyDevice().get())
sigma_maximum_winner = maximum_limit * torch.sqrt(self.gmm_array[gmm_winner_idx].variance)
sigma_minimum_winner = minimum_limit * torch.sqrt(self.gmm_array[gmm_winner_idx].variance)
winner_center = self.gmm_array[gmm_winner_idx].center
if maximum_limit == minimum_limit:
mean_positive_sigma_winner = winner_center + sigma_maximum_winner
mean_negative_sigma_winner = winner_center - sigma_minimum_winner
else:
# FIXME This seems wrong
mean_positive_sigma_winner = winner_center + sigma_minimum_winner + sigma_maximum_winner
mean_negative_sigma_winner = winner_center - sigma_minimum_winner - sigma_maximum_winner
mean_positive_sigma = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
mean_negative_sigma = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
overlap_mins_mins = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
overlap_mins_plus = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
overlap_plus_mins = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
overlap_plus_plus = torch.zeros(int(self.M()), int(self.number_features), dtype=torch.float, device=MyDevice().get())
overlap_score = []
for i in range(self.M()):
sigma_maximum = maximum_limit * torch.sqrt(self.gmm_array[i].variance)
sigma_minimum = minimum_limit * torch.sqrt(self.gmm_array[i].variance)
if maximum_limit == minimum_limit:
mean_positive_sigma[i] = self.gmm_array[i].center + sigma_maximum
mean_negative_sigma[i] = self.gmm_array[i].center - sigma_maximum
else:
#FIXME This seems wrong
mean_positive_sigma[i] = sigma_maximum + sigma_minimum
mean_negative_sigma[i] = -sigma_minimum - sigma_maximum
overlap_mins_mins[i] = torch.mean(mean_negative_sigma[i] - mean_negative_sigma_winner)
overlap_mins_plus[i] = torch.mean(mean_positive_sigma[i] - mean_negative_sigma_winner)
overlap_plus_mins[i] = torch.mean(mean_negative_sigma[i] - mean_positive_sigma_winner)
overlap_plus_plus[i] = torch.mean(mean_positive_sigma[i] - mean_positive_sigma_winner)
condition1 = (overlap_mins_mins[i] >= 0).all() \
and (overlap_mins_plus[i] >= 0).all() \
and (overlap_plus_mins[i] <= 0).all() \
and (overlap_plus_plus[i] <= 0).all()
condition2 = (overlap_mins_mins[i] <= 0).all() \
and (overlap_mins_plus[i] >= 0).all() \
and (overlap_plus_mins[i] <= 0).all() \
and (overlap_plus_plus[i] >= 0).all()
condition3 = (overlap_mins_mins[i] > 0).all() \
and (overlap_mins_plus[i] > 0).all() \
and (overlap_plus_mins[i] < 0).all() \
and (overlap_plus_plus[i] > 0).all()
condition4 = (overlap_mins_mins[i] < 0).all() \
and (overlap_mins_plus[i] > 0).all() \
and (overlap_plus_mins[i] < 0).all() \
and (overlap_plus_plus[i] < 0).all()
if condition1 or condition2:
# full overlap, the cluster is inside the winning cluster
# the score is full score
overlap_score.append(overlap_coefficient)
elif condition3 or condition4:
# partial overlap, the score is the full score multiplied by the overlap degree
reward = MyUtil.norm_2(self.gmm_array[i].center - self.gmm_array[gmm_winner_idx].center) \
/ MyUtil.norm_2(self.gmm_array[i].center + self.gmm_array[gmm_winner_idx].center) \
+ MyUtil.norm_2(self.gmm_array[i].center - torch.sqrt(self.gmm_array[gmm_winner_idx].variance)) \
/ MyUtil.norm_2(self.gmm_array[i].center + torch.sqrt(self.gmm_array[gmm_winner_idx].variance))
overlap_score.append(overlap_coefficient * reward)
else:
# No overlap, then the score is 0
overlap_score.append(torch.zeros(1))
overlap_score.pop(gmm_winner_idx)
self.rho = torch.sum(torch.stack(overlap_score))
self.rho = torch.min(self.rho, torch.ones_like(self.rho))
self.rho = torch.max(self.rho, torch.ones_like(self.rho) * 0.1) # Do not let rho = zero
def compute_rho_vigilance_test(self, x, gmm_winner_idx):
pass
def compute_rho_containing_rule(self, gmm_winner_idx, maximum_limit, minimum_limit):
pass
def compute_number_of_gmms(self):
if self.gmm_array is None:
return 0
else:
return len(self.gmm_array)
def M(self):
return self.compute_number_of_gmms()

489
ATL.py Normal file
View File

@ -0,0 +1,489 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
from DataManipulator import DataManipulator
from NeuralNetwork import NeuralNetwork
from AutoEncoder import DenoisingAutoEncoder
from AGMM import AGMM
from MySingletons import MyDevice, TorchDevice
from colorama import Fore, Back, Style
from itertools import cycle
import numpy as np
import matplotlib.pylab as plt
import math
import torch
import time
def copy_weights(source: NeuralNetwork, target: NeuralNetwork, layer_numbers=[1], copy_moment: bool = True):
for layer_number in layer_numbers:
layer_number -= 1
if layer_number >= source.number_hidden_layers:
target.output_weight = source.output_weight
target.output_bias = source.output_bias
if copy_moment:
target.output_momentum = source.output_momentum
target.output_bias_momentum = source.output_bias_momentum
else:
target.weight[layer_number] = source.weight[layer_number]
target.bias[layer_number] = source.bias[layer_number]
if copy_moment:
target.momentum[layer_number] = source.momentum[layer_number]
target.bias_momentum[layer_number] = source.bias_momentum[layer_number]
def grow_nodes(*networks):
origin = networks[0]
if origin.growable[origin.number_hidden_layers]:
if origin.get_agmm() is None:
nodes = 1
else:
nodes = origin.get_agmm().M()
for i in range(nodes):
for network in networks:
network.grow_node(origin.number_hidden_layers)
return True
else:
return False
def prune_nodes(*networks):
origin = networks[0]
if origin.prunable[origin.number_hidden_layers][0] >= 0:
nodes_to_prune = origin.prunable[origin.number_hidden_layers].tolist()
for network in networks:
for node_to_prune in nodes_to_prune[::-1]:
network.prune_node(origin.number_hidden_layers, node_to_prune)
def width_evolution(network: NeuralNetwork, x: torch.tensor, y: torch.tensor = None, agmm: AGMM = None, train_agmm: bool = False):
if y is None:
y = x
if agmm is not None:
network.set_agmm(agmm)
if train_agmm:
network.forward_pass(x)
network.run_agmm(x, y)
network.feedforward(x, y)
network.width_adaptation_stepwise(y)
def discriminative(network: NeuralNetwork, x: torch.tensor, y: torch.tensor = None, agmm: AGMM = None):
if agmm is not None:
network.set_agmm(agmm)
if y is None:
y = x
network.train(x, y)
def generative(network: NeuralNetwork, x: torch.tensor, y: torch.tensor = None, agmm: AGMM = None, is_tied_weight=False, noise_ratio=0.1, glw_epochs: int = 1):
if agmm is not None:
network.set_agmm(agmm)
if y is None:
y = x
network.greedy_layer_wise_pretrain(x=x, number_epochs=glw_epochs, noise_ratio=0.0)
network.train(x=x, y=y, noise_ratio=noise_ratio, is_tied_weight=is_tied_weight)
def test(network: NeuralNetwork, x: torch.tensor, y: torch.tensor = None, is_source: bool = False, is_discriminative: bool = False, metrics=None):
with torch.no_grad():
if y is None:
y = x
network.test(x=x, y=y)
if is_source:
if is_discriminative:
metrics['classification_rate_source'].append(network.classification_rate)
metrics['classification_source_loss'].append(float(network.loss_value))
else:
metrics['reconstruction_source_loss'].append(float(network.loss_value))
else:
if is_discriminative:
metrics['classification_rate_target'].append(network.classification_rate)
metrics['classification_target_loss'].append(float(network.loss_value))
else:
metrics['reconstruction_target_loss'].append(float(network.loss_value))
def force_same_size(a_tensor, b_tensor, shuffle=True, strategy='max'):
common = np.min([a_tensor.shape[0], b_tensor.shape[0]])
if shuffle:
a_tensor = a_tensor[torch.randperm(a_tensor.shape[0])]
b_tensor = b_tensor[torch.randperm(b_tensor.shape[0])]
if strategy == 'max':
if math.ceil(a_tensor.shape[0] / common) <= math.ceil(b_tensor.shape[0] / common):
b_tensor = torch.stack(list(target for target, source in zip(b_tensor[torch.randperm(b_tensor.shape[0])], cycle(a_tensor[torch.randperm(a_tensor.shape[0])]))))
a_tensor = torch.stack(list(source for target, source in zip(b_tensor[torch.randperm(b_tensor.shape[0])], cycle(a_tensor[torch.randperm(a_tensor.shape[0])]))))
else:
b_tensor = torch.stack(list(target for target, source in zip(cycle(b_tensor[torch.randperm(b_tensor.shape[0])]), a_tensor[torch.randperm(a_tensor.shape[0])])))
a_tensor = torch.stack(list(source for target, source in zip(cycle(b_tensor[torch.randperm(b_tensor.shape[0])]), a_tensor[torch.randperm(a_tensor.shape[0])])))
elif strategy == 'min':
a_tensor = a_tensor[:common]
b_tensor = b_tensor[:common]
if shuffle:
a_tensor = a_tensor[torch.randperm(a_tensor.shape[0])]
b_tensor = b_tensor[torch.randperm(b_tensor.shape[0])]
return a_tensor, b_tensor
def kl(ae: NeuralNetwork, x_source: torch.tensor, x_target: torch.tensor):
x_source, x_target = force_same_size(x_source, x_target)
ae.reset_grad()
kl_loss = torch.nn.functional.kl_div(ae.forward_pass(x_target).layer_value[1],
ae.forward_pass(x_source).layer_value[1], reduction='batchmean')
kl_loss.backward()
ae.weight[0] = ae.weight[0] - ae.learning_rate * ae.weight[0].grad
ae.bias[0] = ae.bias[0] - ae.learning_rate * ae.bias[0].grad
return kl_loss.detach().cpu().numpy()
def print_annotation(lst):
def custom_range(xx):
return range(0, len(xx), int(len(xx) * 0.25) - 1)
for idx in custom_range(lst):
pos = lst[idx] if isinstance(lst[idx], (int, float)) else lst[idx][0]
plt.annotate(format(pos, '.2f'), (idx, pos))
pos = lst[-1] if isinstance(lst[-1], (int, float)) else lst[-1][0]
plt.annotate(format(pos, '.2f'), (len(lst), pos))
def plot_time(train, test, annotation=True):
plt.title('Processing time')
plt.ylabel('Seconds')
plt.xlabel('Minibatches')
plt.plot(train, linewidth=1, label=('Train time Mean | Accumulative %f | %f' % (np.mean(train), np.sum(train))))
plt.plot(test, linewidth=1, label=('Test time Mean | Accumulative %f | %f' % (np.mean(test), np.sum(test))))
plt.legend()
if annotation:
print_annotation(train)
print_annotation(test)
plt.tight_layout()
plt.show()
def plot_agmm(agmm_source, agmm_target, annotation=True):
plt.title('AGMM evolution')
plt.ylabel('GMMs')
plt.xlabel('Samples')
plt.plot(agmm_source, linewidth=1, label=('AGMM Source Discriminative Mean: %f' % (np.mean(agmm_source))))
plt.plot(agmm_target, linewidth=1, label=('AGMM Target Generative Mean: %f' % (np.mean(agmm_target))))
plt.legend()
if annotation:
print_annotation(agmm_source)
print_annotation(agmm_target)
plt.tight_layout()
plt.show()
def plot_node_evolution(nodes, annotation=True):
plt.title('Node evolution')
plt.ylabel('Nodes')
plt.xlabel('Minibatches')
plt.plot(nodes, linewidth=1,
label=('Hidden Layer Mean | Final: %f | %d' % (np.mean(nodes), nodes[-1])))
plt.legend()
if annotation:
print_annotation(nodes)
plt.tight_layout()
plt.show()
def plot_losses(classification_source_loss, classification_target_loss, reconstruction_source_loss,
reconstruction_target_loss, annotation=True):
plt.title('Losses evolution')
plt.ylabel('Loss value')
plt.xlabel('Minibatches')
plt.plot(classification_source_loss, linewidth=1,
label=('Classification Source Loss mean: %f' % (np.mean(classification_source_loss))))
plt.plot(classification_target_loss, linewidth=1,
label=('Classification Target Loss mean: %f' % (np.mean(classification_target_loss))))
plt.plot(reconstruction_source_loss, linewidth=1,
label=('Reconstruction Source Loss mean: %f' % (np.mean(reconstruction_source_loss))))
plt.plot(reconstruction_target_loss, linewidth=1,
label=('Reconstruction Target Loss mean: %f' % (np.mean(reconstruction_target_loss))))
plt.legend()
if annotation:
print_annotation(classification_source_loss)
print_annotation(classification_target_loss)
print_annotation(reconstruction_source_loss)
print_annotation(reconstruction_target_loss)
plt.tight_layout()
plt.show()
def plot_classification_rates(source_rate, target_rate, annotation=True):
plt.title('Source and Target Classification Rates')
plt.ylabel('Classification Rate')
plt.xlabel('Minibatches')
plt.plot(source_rate, linewidth=1, label=('Source CR mean: %f' % (np.mean(source_rate))))
plt.plot(target_rate, linewidth=1, label=('Target CR mean: %f' % (np.mean(target_rate))))
if annotation:
print_annotation(source_rate)
print_annotation(target_rate)
plt.legend()
plt.tight_layout()
plt.show()
def plot_ns(bias, var, ns, annotation=True):
plt.plot(bias, linewidth=1, label=('Bias2 mean: %f' % (np.mean(bias))))
plt.plot(var, linewidth=1, label=('Variance mean: %f' % (np.mean(var))))
plt.plot(ns, linewidth=1, label=('Network Significance mean: %f' % (np.mean(ns))))
plt.legend()
if annotation:
print_annotation(bias)
print_annotation(var)
print_annotation(ns)
plt.tight_layout()
plt.show()
def plot_discriminative_network_significance(bias, var, annotation=True):
plt.title('Discriminative Source BIAS2, VAR, NS')
plt.ylabel('Value')
plt.xlabel('Sample')
plot_ns(bias, var, (np.array(bias) + np.array(var)).tolist(), annotation)
def plot_generative_network_significance(bias, var, annotation=True, is_source=True):
if is_source:
plt.title('Generative Source BIAS2, VAR, NS')
else:
plt.title('Generative Target BIAS2, VAR, NS')
plt.ylabel('Value')
plt.xlabel('Sample')
plot_ns(bias, var, (np.array(bias) + np.array(var)).tolist(), annotation)
def ATL(epochs: int = 1, n_batch: int = 1000, device='cpu'):
def print_metrics(minibatch, metrics, nn, ae, Xs, Xt):
print('Minibatch: %d | Execution time (dataset load/pre-processing + model run): %f' % (minibatch, time.time() - metrics['start_execution_time']))
if minibatch > 1:
string_max = '' + Fore.GREEN + 'Max' + Style.RESET_ALL
string_mean = '' + Fore.YELLOW + 'Mean' + Style.RESET_ALL
string_min = '' + Fore.RED + 'Min' + Style.RESET_ALL
string_now = '' + Fore.BLUE + 'Now' + Style.RESET_ALL
string_accu = '' + Fore.MAGENTA + 'Accu' + Style.RESET_ALL
print(('Total of samples:' + Fore.BLUE + ' %d Source' + Style.RESET_ALL +' |' + Fore.RED +' %d Target' + Style.RESET_ALL) % (Xs.shape[0], Xt.shape[0]))
print(('%s %s %s %s %s Training time:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Fore.MAGENTA + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now, string_accu,
np.max(metrics['train_time']),
np.mean(metrics['train_time']),
np.min(metrics['train_time']),
metrics['train_time'][-1],
np.sum(metrics['train_time'])))
print(('%s %s %s %s %s Testing time:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Fore.MAGENTA + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now, string_accu,
np.max(metrics['test_time']),
np.mean(metrics['test_time']),
np.min(metrics['test_time']),
metrics['test_time'][-1],
np.sum(metrics['test_time'])))
print(('%s %s %s %s CR Source:' + Fore.GREEN + ' %f%% ' + Back.BLUE + Fore.YELLOW + Style.BRIGHT + '%f%%' + Style.RESET_ALL + Fore.RED + ' %f%%' + Fore.BLUE + ' %f%%' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['classification_rate_source']) * 100,
np.mean(metrics['classification_rate_source']) * 100,
np.min(metrics['classification_rate_source']) * 100,
metrics['classification_rate_source'][-1] * 100))
print(('%s %s %s %s CR Target:' + Fore.GREEN + ' %f%% ' + Back.RED + Fore.YELLOW + Style.BRIGHT + '%f%%' + Style.RESET_ALL + Fore.RED + ' %f%%' + Fore.BLUE + ' %f%%' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['classification_rate_target']) * 100,
np.mean(metrics['classification_rate_target']) * 100,
np.min(metrics['classification_rate_target']) * 100,
metrics['classification_rate_target'][-1] * 100))
print(('%s %s %s %s Classification Source Loss:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['classification_source_loss']),
np.mean(metrics['classification_source_loss']),
np.min(metrics['classification_source_loss']),
metrics['classification_source_loss'][-1]))
print(('%s %s %s %s Classification Target Loss:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['classification_target_loss']),
np.mean(metrics['classification_target_loss']),
np.min(metrics['classification_target_loss']),
metrics['classification_target_loss'][-1]))
print(('%s %s %s %s Reconstruction Target Loss:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['reconstruction_target_loss']),
np.mean(metrics['reconstruction_target_loss']),
np.min(metrics['reconstruction_target_loss']),
metrics['reconstruction_target_loss'][-1]))
print(('%s %s %s %s Kullback-Leibler loss 1:' + Fore.GREEN + ' %f' + Fore.YELLOW + ' %f' + Fore.RED + ' %f' + Fore.BLUE + ' %f' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['kl_loss']),
np.mean(metrics['kl_loss']),
np.min(metrics['kl_loss']),
metrics['kl_loss'][-1]))
print(('%s %s %s %s Nodes:' + Fore.GREEN + ' %d' + Fore.YELLOW + ' %f' + Fore.RED + ' %d' + Fore.BLUE + ' %d' + Style.RESET_ALL) % (
string_max, string_mean, string_min, string_now,
np.max(metrics['node_evolution']),
np.mean(metrics['node_evolution']),
np.min(metrics['node_evolution']),
metrics['node_evolution'][-1]))
print(('Network structure:' + Fore.BLUE + ' %s (Discriminative) %s (Generative)' + Style.RESET_ALL) % (
" ".join(map(str, nn.layers)),
" ".join(map(str, ae.layers))))
print(Style.RESET_ALL)
metrics = {'classification_rate_source': [],
'classification_rate_target': [],
'train_time': [],
'test_time': [],
'node_evolution': [],
'classification_target_loss': [],
'classification_source_loss': [],
'reconstruction_source_loss': [],
'reconstruction_target_loss': [],
'kl_loss': [],
'agmm_target_size_by_batch': [],
'agmm_source_size_by_batch': [],
'start_execution_time': time.time()}
TorchDevice.instance().device = device
dm = DataManipulator('')
dm.load_custom_csv()
dm.normalize()
dm.split_as_source_target_streams(n_batch, 'dallas_2', 0.5)
nn = NeuralNetwork([dm.number_features, 1, dm.number_classes])
ae = DenoisingAutoEncoder([nn.layers[0], nn.layers[1], nn.layers[0]])
# I am building the greedy_layer_bias
x = dm.get_Xs(0)
x = torch.tensor(np.atleast_2d(x), dtype=torch.float, device=MyDevice().get())
ae.greedy_layer_wise_pretrain(x=x, number_epochs=0)
# I am building the greedy_layer_bias
agmm_source_discriminative = AGMM()
agmm_target_generative = AGMM()
for i in range(dm.number_minibatches):
Xs = torch.tensor(dm.get_Xs(i), dtype=torch.float, device=MyDevice().get())
ys = torch.tensor(dm.get_ys(i), dtype=torch.float, device=MyDevice().get())
Xt = torch.tensor(dm.get_Xt(i), dtype=torch.float, device=MyDevice().get())
yt = torch.tensor(dm.get_yt(i), dtype=torch.float, device=MyDevice().get())
if i > 0:
metrics['test_time'].append(time.time())
test(nn, Xt, yt, is_source=False, is_discriminative=True, metrics=metrics)
metrics['test_time'][-1] = time.time() - metrics['test_time'][-1]
test(nn, Xs, ys, is_source=True, is_discriminative=True, metrics=metrics)
test(ae, Xt, is_source=False, is_discriminative=False, metrics=metrics)
metrics['train_time'].append(time.time())
for epoch in range(epochs):
for x, y in [(x.view(1, x.shape[0]), y.view(1, y.shape[0])) for x, y in zip(Xs, ys)]:
width_evolution(network=nn, x=x, y=y, agmm=agmm_source_discriminative, train_agmm=True if epoch == 1 else False)
if not grow_nodes(nn, ae): prune_nodes(nn, ae)
discriminative(network=nn, x=x, y=y, agmm=agmm_source_discriminative)
copy_weights(source=nn, target=ae, layer_numbers=[1])
for x in [x.view(1, x.shape[0]) for x in Xt]:
width_evolution(network=ae, x=x, agmm=agmm_target_generative, train_agmm=True if epoch == 1 else False)
if not grow_nodes(ae, nn): prune_nodes(ae, nn)
generative(network=ae, x=x, agmm=agmm_target_generative)
metrics['kl_loss'].append(kl(ae=ae, x_source=Xs, x_target=Xt))
copy_weights(source=ae, target=nn, layer_numbers=[1])
if agmm_target_generative.M() > 1: agmm_target_generative.delete_cluster()
if agmm_source_discriminative.M() > 1: agmm_source_discriminative.delete_cluster()
metrics['agmm_target_size_by_batch'].append(agmm_target_generative.M())
metrics['agmm_source_size_by_batch'].append(agmm_source_discriminative.M())
metrics['train_time'][-1] = time.time() - metrics['train_time'][-1]
metrics['node_evolution'].append(nn.layers[1])
print_metrics(i + 1, metrics, nn, ae, Xs, Xt)
result = '%f (T) ''| %f (S) \t %f | %d \t %f | %f' % (
np.mean(metrics['classification_rate_target']),
np.mean(metrics['classification_rate_source']),
np.mean(metrics['node_evolution']),
metrics['node_evolution'][-1],
np.mean(metrics['train_time']),
np.sum(metrics['train_time']))
print(result)
plot_time(metrics['train_time'], metrics['test_time'])
plot_node_evolution(metrics['node_evolution'])
plot_classification_rates(metrics['classification_rate_source'], metrics['classification_rate_target'])
return result
atl = ATL(epochs = 1, device='cpu')
print(atl)

168
AutoEncoder.py Normal file
View File

@ -0,0 +1,168 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
from NeuralNetwork import NeuralNetwork
from MySingletons import MyDevice
import numpy as np
import torch
class AutoEncoder(NeuralNetwork):
_greedy_layer_bias = None
_greedy_layer_output_bias = None
def __init__(self, layers=[]):
NeuralNetwork.__init__(self, layers)
for i in range(self.number_hidden_layers):
self.activation_function[i] = self.ACTIVATION_FUNCTION_SIGMOID
self.output_activation_function = self.ACTIVATION_FUNCTION_SIGMOID
self.loss_function = self.LOSS_FUNCTION_MSE
def train(self, x: torch.tensor, is_tied_weight: bool = False, noise_ratio: float = 0.0, weight_number: int = None, y: torch.tensor = None):
if is_tied_weight:
for i in range(int(self.number_hidden_layers/2)):
if i == 0:
self.output_weight = self.weight[i].T
else:
self.weight[-i] = self.weight[i].T
if y is None:
y = x
NeuralNetwork.train(self, x=self.masking_noise(x=x, noise_ratio=noise_ratio), y=y, weight_no=weight_number)
def test(self, x: torch.tensor, is_beta_updatable: bool = False, y: torch.tensor = None):
if y is None:
y = x
NeuralNetwork.test(self, x=x, y=y, is_beta_updatable=is_beta_updatable)
def grow_node(self, layer_number):
NeuralNetwork.grow_node(self, layer_number)
self.grow_greedy_layer_bias(layer_number)
def prune_node(self, layer_number, node_number):
NeuralNetwork.prune_node(self, layer_number, node_number)
self.prune_greedy_layer_bias(layer_number, node_number)
def grow_greedy_layer_bias(self, layer_number):
b = layer_number
if b is self.number_hidden_layers:
[n_out, n_in] = self._greedy_layer_output_bias.shape
self._greedy_layer_output_bias = torch.cat((self._greedy_layer_output_bias, self.xavier_weight_initialization(1, 1)), axis=1)
else:
[n_out, n_in] = self._greedy_layer_bias[b].shape
n_in = n_in + 1
self._greedy_layer_bias[b] = np.append(self._greedy_layer_bias[b], self.xavier_weight_initialization(n_out, n_in, shape=(n_out, 1)))
def prune_greedy_layer_bias(self, layer_number, node_number):
def remove_nth_element(greedy_bias_tensor, n):
bias_tensor = torch.cat([greedy_bias_tensor[0][:n], greedy_bias_tensor[0][n + 1:]])
return bias_tensor.view(1, bias_tensor.shape[0])
b = layer_number # readability
n = node_number # readability
if b is self.number_hidden_layers:
self._greedy_layer_output_bias = remove_nth_element(self._greedy_layer_output_bias, n)
else:
self._greedy_layer_bias[b] = remove_nth_element(self._greedy_layer_bias[b], n)
def greedy_layer_wise_pretrain(self, x: torch.tensor, number_epochs: int = 1, is_tied_weight: bool = False, noise_ratio: float = 0.0):
for i in range(len(self.layers) - 1):
if i > self.number_hidden_layers:
nn = NeuralNetwork([self.layers[i], self.layers[-1], self.layers[i]])
else:
nn = NeuralNetwork([self.layers[i], self.layers[i + 1], self.layers[i]])
nn.output_activation_function = self.ACTIVATION_FUNCTION_SIGMOID
nn.loss_function = self.LOSS_FUNCTION_MSE
nn.momentum_rate = 0
if i >= self.number_hidden_layers:
nn.weight[0] = self.output_weight.clone()
nn.bias[0] = self.output_bias.clone()
nn.output_weight = self.output_weight.T.clone()
if self._greedy_layer_output_bias is None:
nodes_before = nn.layers[-2]
nodes_after = nn.layers[-1]
self._greedy_layer_output_bias = self.xavier_weight_initialization(1, nodes_after)
nn.output_bias = self._greedy_layer_output_bias.clone()
else:
nn.weight[0] = self.weight[i].clone()
nn.bias[0] = self.bias[i].clone()
nn.output_weight = self.weight[i].T.clone()
try:
nn.output_bias = self._greedy_layer_bias[i].clone()
except (TypeError, IndexError):
nodes_before = nn.layers[-2]
nodes_after = nn.layers[-1]
if self._greedy_layer_bias is None:
self._greedy_layer_bias = []
self._greedy_layer_bias.append(self.xavier_weight_initialization(1, nodes_after))
nn.output_bias = self._greedy_layer_bias[i].clone()
for j in range(0, number_epochs):
training_x = self.forward_pass(x=x).layer_value[i]
nn.train(self.masking_noise(x=training_x, noise_ratio=noise_ratio), training_x)
if i >= self.number_hidden_layers:
self.output_weight = nn.weight[0].clone()
self.output_bias = nn.bias[0].clone()
else:
self.weight[i] = nn.weight[0].clone()
self.bias[i] = nn.bias[0].clone()
def update_weights_kullback_leibler(self, Xs, Xt, gamma=0.0001):
loss = NeuralNetwork.update_weights_kullback_leibler(self, Xs, Xs, Xt, Xt, gamma)
return loss
def compute_bias(self, y):
return torch.mean((self.Ey.T - y) ** 2)
@property
def network_variance(self):
return torch.mean(self.Ey2 - self.Ey ** 2)
class DenoisingAutoEncoder(AutoEncoder):
def __init__(self, layers=[]):
AutoEncoder.__init__(self, layers)
def train(self, x: torch.tensor, noise_ratio: float = 0.0, is_tied_weight: bool = False, weight_number: int = None, y: torch.tensor = None):
AutoEncoder.train(self, x=x, noise_ratio=noise_ratio, is_tied_weight=is_tied_weight, weight_number=weight_number, y=y)
def greedy_layer_wise_pretrain(self, x: torch.tensor, number_epochs: int = 1, is_tied_weight: bool = False, noise_ratio: float = 0.0, y: torch.tensor = None):
AutoEncoder.greedy_layer_wise_pretrain(self, x=x, number_epochs=number_epochs, is_tied_weight=is_tied_weight, noise_ratio=noise_ratio)

177
DataManipulator.py Normal file
View File

@ -0,0 +1,177 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
import numpy as np
import pandas as pd
class DataManipulator:
data = None
number_features = None
number_classes = None
number_fold_elements = None
number_minibatches = None
source_data = None
target_data = None
__X = None
__y = None
__Xs = None
__ys = None
__Xt = None
__yt = None
__permutedX = None
__permutedy = None
__index_permutation = None
__data_folder_path = None
def __init__(self, data_folder_path):
self.__data_folder_path = data_folder_path
def load_mnist(self):
raise TypeError('Not implemented')
def load_custom_csv(self):
print('Loading data.csv')
self.data = pd.read_csv(filepath_or_buffer='data.csv', header=None)
self.check_dataset_is_even()
self.number_features = self.data.shape[1] - 1
self.X = self.data.iloc[:, 0:self.number_features].add_prefix('feature_').astype(dtype=np.float64)
self.y = pd.get_dummies(self.data.iloc[:, self.number_features], prefix='class', dtype=np.float64)
self.number_classes = self.y.shape[1]
self.data = self.X.join(self.y)
def normalize(self):
print('Normalizing data')
self.X = (self.X - self.X.min())/(self.X.max() - self.X.min())
self.data = self.X.join(self.y)
def normalize_image(self):
raise TypeError('Not implemented')
def split_as_source_target_streams(self, number_fold_elements=0, method=None, sampling_ratio=0.5):
if number_fold_elements == 0:
self.number_fold_elements == self.data.shape[0]
else:
self.number_fold_elements = number_fold_elements
if method == None or method == 'none' or method == 'None':
self.__split_as_source_target_streams_none(self.number_fold_elements, sampling_ratio)
elif method == 'dallas_1' or method == 'dallas1':
self.__split_as_source_target_streams_dallas_1(self.number_fold_elements, sampling_ratio)
elif method == 'dallas_2' or method == 'dallas2':
self.__split_as_source_target_streams_dallas_2(self.number_fold_elements, sampling_ratio)
self.__create_Xs_ys_Xt_yt()
def get_Xs(self, number_minibatch):
return self.Xs[number_minibatch].values
def get_ys(self, number_minibatch):
return self.ys[number_minibatch].values
def get_Xt(self, number_minibatch):
return self.Xt[number_minibatch].values
def get_yt(self, number_minibatch):
return self.yt[number_minibatch].values
def __split_as_source_target_streams_dallas_2(self, elements_per_fold=1000, sampling_ratio=0.5):
rows_number = self.data.shape[0]
number_of_folds = round(rows_number / elements_per_fold)
chunk_size = round(rows_number / number_of_folds)
number_of_folds_rounded = round(rows_number / chunk_size)
if (rows_number / number_of_folds_rounded) % 2:
self.number_fold_elements = min(elements_per_fold, np.floor(rows_number / number_of_folds_rounded) - 1)
else:
self.number_fold_elements = min(elements_per_fold, np.floor(rows_number / number_of_folds_rounded))
if rows_number / number_of_folds_rounded > elements_per_fold:
number_of_folds = number_of_folds + 1
self.number_minibatches = number_of_folds
ck = self.number_fold_elements
self.source = []
self.target = []
def chunkify(pnds):
nfe = self.number_fold_elements # readability
nof = self.number_minibatches # readability
return [pnds[i * nfe: (i + 1) * nfe] for i in range(nof)]
for x, data in zip(chunkify(self.X), chunkify(self.data)):
x_mean = np.mean(x, axis=0)
norm_1 = np.linalg.norm(x - x_mean)
norm_2 = np.linalg.norm(x - x_mean, axis=1)
numerator = norm_2
denominator = 2 * (norm_1.std() ** 2)
probability = np.exp(-numerator / denominator)
idx = np.argsort(probability)
m = data.shape[0]
self.source.append(data.iloc[idx[: round(m * sampling_ratio)]].sort_index())
self.target.append(data.iloc[idx[round(m * sampling_ratio):]].sort_index())
def __create_Xs_ys_Xt_yt(self):
self.X, self.y = [], []
self.Xs, self.ys = [], []
self.Xt, self.yt = [], []
self.__permutedX, self.__permutedy = [], []
self.__index_permutation = []
for i in range(0, self.number_minibatches):
self.Xs.append(self.source[i].iloc[:, : -self.number_classes])
self.ys.append(self.source[i].iloc[:, self.number_features:])
self.Xt.append(self.target[i].iloc[:, : -self.number_classes])
self.yt.append(self.target[i].iloc[:, self.number_features:])
self.X.append(pd.concat([self.Xs[i], self.Xt[i]]))
self.y.append(pd.concat([self.ys[i], self.yt[i]]))
x = self.X[i]
y = self.y[i]
p = np.random.permutation(x.shape[0])
self.__permutedX.append(x.iloc[p])
self.__permutedy.append(y.iloc[p])
self.__index_permutation.append(p)
def check_dataset_is_even(self):
if self.data.shape[0] % 2:
self.data.drop(axis='index', index=np.random.randint(1, self.data.shape[0]), inplace=True)

95
ElasticNodes.py Normal file
View File

@ -0,0 +1,95 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
import numpy as np
class ElasticNodes:
###### Elastic Nodes ######
growable = None
prunable = None
data_mean = 0
data_standard_deviation = 0
data_variance = 0
number_samples_feed = 0
number_samples_layer = None
bias_mean = None
bias_variance = None
bias_standard_deviation = None
minimum_bias_mean = None
minimum_bias_standard_deviation = None
bias = None
var_mean = None
var_variance = None
var_standard_deviation = None
minimum_var_mean = None
minimum_var_standard_deviation = None
var = None
node_evolution = None
bias_gradient = None
bias_mean_net = None
var_mean_net = None
######
def __init__(self, number_hidden_layers=1):
nhl = number_hidden_layers #readability
self.number_samples_layer = np.zeros(nhl)
self.bias_mean = np.zeros(nhl)
self.bias_variance = np.zeros(nhl)
self.bias_standard_deviation = np.zeros(nhl)
self.minimum_bias_mean = np.ones(nhl) * np.inf
self.minimum_bias_standard_deviation = np.ones(nhl) * np.inf
self.BIAS = []
self.var_mean = np.zeros(nhl)
self.var_variance = np.zeros(nhl)
self.var_standard_deviation = np.zeros(nhl)
self.minimum_var_mean = np.ones(nhl) * np.inf
self.minimum_var_standard_deviation = np.ones(nhl) * np.inf
self.VAR = []
self.growable = np.ones(nhl) * False
self.prunable = []
for i in range(nhl):
self.prunable.append([-1])

69
MySingletons.py Normal file
View File

@ -0,0 +1,69 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
import torch
class MyDevice:
def get(self):
return TorchDevice.instance().device
class TorchDevice:
class __TorchDevice:
def __init__(self, device: torch.device = None):
if device:
self.device = device
else:
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def __str__(self):
return repr(self) + self.device
_instance = None
__instance = None
def __init__(self):
raise RuntimeError('Call instance() instead')
@classmethod
def instance(cls, device: torch.device = None):
if cls._instance is None:
cls._instance = cls.__new__(cls)
if device is None:
cls.__instance = TorchDevice.__TorchDevice()
else:
cls.__instance = TorchDevice.__TorchDevice(device)
return cls._instance
def __getattr__(self, name):
return getattr(self.__instance, name)

63
MyUtil.py Normal file
View File

@ -0,0 +1,63 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
import math
import torch
class MyUtil:
def __init__(self):
pass
@staticmethod
def recursive_mean_standard_deviation(x, old_mean, old_variance, number_samples):
mean = old_mean + (x - old_mean) / number_samples
var = old_variance + (x - old_mean) * (x - mean)
return mean, var, torch.sqrt(var/number_samples)
@staticmethod
def probit(mean, standard_deviation):
p = (1 + math.pi * (standard_deviation ** 2) / 8)
return mean / torch.sqrt(p)
@staticmethod
def norm_1(x):
return torch.norm(x, 1)
@staticmethod
def norm_2(x):
return torch.norm(x, 2)
@staticmethod
def frobenius_norm(x):
return torch.norm(x, 'fro')

563
NeuralNetwork.py Normal file
View File

@ -0,0 +1,563 @@
# Marcus Vinicius Sousa Leite de Carvalho
# marcus.decarvalho@ntu.edu.sg
# ivsucram@gmail.com
#
# NANYANG TECHNOLOGICAL UNIVERSITY - NTUITIVE PTE LTD Dual License Agreement
# Non-Commercial Use Only
# This NTUITIVE License Agreement, including all exhibits ("NTUITIVE-LA") is a legal agreement between you and NTUITIVE (or “we”) located at 71 Nanyang Drive, NTU Innovation Centre, #01-109, Singapore 637722, a wholly owned subsidiary of Nanyang Technological University (“NTU”) for the software or data identified above, which may include source code, and any associated materials, text or speech files, associated media and "online" or electronic documentation and any updates we provide in our discretion (together, the "Software").
#
# By installing, copying, or otherwise using this Software, found at https://github.com/Ivsucram/ATL_Matlab, you agree to be bound by the terms of this NTUITIVE-LA. If you do not agree, do not install copy or use the Software. The Software is protected by copyright and other intellectual property laws and is licensed, not sold. If you wish to obtain a commercial royalty bearing license to this software please contact us at marcus.decarvalho@ntu.edu.sg.
#
# SCOPE OF RIGHTS:
# You may use, copy, reproduce, and distribute this Software for any non-commercial purpose, subject to the restrictions in this NTUITIVE-LA. Some purposes which can be non-commercial are teaching, academic research, public demonstrations and personal experimentation. You may also distribute this Software with books or other teaching materials, or publish the Software on websites, that are intended to teach the use of the Software for academic or other non-commercial purposes.
# You may not use or distribute this Software or any derivative works in any form for commercial purposes. Examples of commercial purposes would be running business operations, licensing, leasing, or selling the Software, distributing the Software for use with commercial products, using the Software in the creation or use of commercial products or any other activity which purpose is to procure a commercial gain to you or others.
# If the Software includes source code or data, you may create derivative works of such portions of the Software and distribute the modified Software for non-commercial purposes, as provided herein.
# If you distribute the Software or any derivative works of the Software, you will distribute them under the same terms and conditions as in this license, and you will not grant other rights to the Software or derivative works that are different from those provided by this NTUITIVE-LA.
# If you have created derivative works of the Software, and distribute such derivative works, you will cause the modified files to carry prominent notices so that recipients know that they are not receiving the original Software. Such notices must state: (i) that you have changed the Software; and (ii) the date of any changes.
#
# You may not distribute this Software or any derivative works.
# In return, we simply require that you agree:
# 1. That you will not remove any copyright or other notices from the Software.
# 2. That if any of the Software is in binary format, you will not attempt to modify such portions of the Software, or to reverse engineer or decompile them, except and only to the extent authorized by applicable law.
# 3. That NTUITIVE is granted back, without any restrictions or limitations, a non-exclusive, perpetual, irrevocable, royalty-free, assignable and sub-licensable license, to reproduce, publicly perform or display, install, use, modify, post, distribute, make and have made, sell and transfer your modifications to and/or derivative works of the Software source code or data, for any purpose.
# 4. That any feedback about the Software provided by you to us is voluntarily given, and NTUITIVE shall be free to use the feedback as it sees fit without obligation or restriction of any kind, even if the feedback is designated by you as confidential.
# 5. THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS, IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ANY WARRANTY AGAINST INTERFERENCE WITH YOUR ENJOYMENT OF THE SOFTWARE OR ANY WARRANTY OF TITLE OR NON-INFRINGEMENT. THERE IS NO WARRANTY THAT THIS SOFTWARE WILL FULFILL ANY OF YOUR PARTICULAR PURPOSES OR NEEDS. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 6. THAT NEITHER NTUITIVE NOR NTU NOR ANY CONTRIBUTOR TO THE SOFTWARE WILL BE LIABLE FOR ANY DAMAGES RELATED TO THE SOFTWARE OR THIS NTUITIVE-LA, INCLUDING DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS, NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
# 7. That we have no duty of reasonable care or lack of negligence, and we are not obligated to (and will not) provide technical support for the Software.
# 8. That if you breach this NTUITIVE-LA or if you sue anyone over patents that you think may apply to or read on the Software or anyone's use of the Software, this NTUITIVE-LA (and your license and rights obtained herein) terminate automatically. Upon any such termination, you shall destroy all of your copies of the Software immediately. Sections 3, 4, 5, 6, 7, 8, 11 and 12 of this NTUITIVE-LA shall survive any termination of this NTUITIVE-LA.
# 9. That the patent rights, if any, granted to you in this NTUITIVE-LA only apply to the Software, not to any derivative works you make.
# 10. That the Software may be subject to U.S. export jurisdiction at the time it is licensed to you, and it may be subject to additional export or import laws in other places. You agree to comply with all such laws and regulations that may apply to the Software after delivery of the software to you.
# 11. That all rights not expressly granted to you in this NTUITIVE-LA are reserved.
# 12. That this NTUITIVE-LA shall be construed and controlled by the laws of the Republic of Singapore without regard to conflicts of law. If any provision of this NTUITIVE-LA shall be deemed unenforceable or contrary to law, the rest of this NTUITIVE-LA shall remain in full effect and interpreted in an enforceable manner that most nearly captures the intent of the original language.
#
# Copyright (c) NTUITIVE. All rights reserved.
from MyUtil import MyUtil as MyUtil
from ElasticNodes import ElasticNodes
from MySingletons import MyDevice
import AGMM
import numpy as np
import torch
class NeuralNetwork(ElasticNodes):
layers = None
layer_value = None
output_layer_value = None
weight = None
bias = None
momentum = None
bias_momentum = None
output_weight = None
output_bias = None
output_momentum = None
output_bias_momentum = None
activation_function = None
output_activation_function = None
loss_function = None
learning_rate = 0.01
momentum_rate = 0.95
###
error_value = None
loss_value = None
classification_rate = None
###
###
output_beta = None
output_beta_decreasing_factor = None
###
__Eh = None
__Eh2 = None
###
###
is_agmm_able = False
agmm = None
###
@property
def number_hidden_layers(self):
return len(self.layers) - 2
@property
def input_size(self):
return self.layers[0]
@property
def output_size(self):
return self.layers[-1]
@property
def output(self):
return self.output_layer_value
@property
def raw_output(self):
return torch.max(self.output, axis=1)
@property
def outputed_classes(self):
return torch.argmax(self.output, axis=1)
@property
def residual_error(self):
return 1 - self.raw_output.values
ACTIVATION_FUNCTION_AFFINE = 1
ACTIVATION_FUNCTION_SIGMOID = ACTIVATION_FUNCTION_AFFINE + 1
ACTIVATION_FUNCTION_TANH = ACTIVATION_FUNCTION_SIGMOID + 1
ACTIVATION_FUNCTION_RELU = ACTIVATION_FUNCTION_TANH + 1
ACTIVATION_FUNCTION_LINEAR = ACTIVATION_FUNCTION_RELU + 1
ACTIVATION_FUNCTION_SOFTMAX = ACTIVATION_FUNCTION_LINEAR + 1
LOSS_FUNCTION_MSE = ACTIVATION_FUNCTION_SOFTMAX + 1
LOSS_FUNCTION_CROSS_ENTROPY = LOSS_FUNCTION_MSE + 1
PRUNE_NODE_STRATEGY_SINGLE = LOSS_FUNCTION_CROSS_ENTROPY + 1
PRUNE_NODE_STRATEGY_MULTIPLE = PRUNE_NODE_STRATEGY_SINGLE + 1
def __init__(self, layers: list):
self.layers = layers
self.weight = []
self.bias = []
self.momentum = []
self.bias_momentum = []
self.activation_function = []
for i in range(self.number_hidden_layers):
nodes_before = layers[i]
nodes_after = layers[i + 1]
self.weight.append(self.xavier_weight_initialization(nodes_after, nodes_before))
self.bias.append(self.xavier_weight_initialization(1, nodes_after))
self.momentum.append(torch.zeros(self.weight[i].shape, dtype=torch.float, device=MyDevice().get()))
self.bias_momentum.append(torch.zeros(self.bias[i].shape, dtype=torch.float, device=MyDevice().get()))
self.activation_function.append(self.ACTIVATION_FUNCTION_SIGMOID)
nodes_before = layers[-2]
nodes_after = layers[-1]
self.output_weight = self.xavier_weight_initialization(nodes_after, nodes_before)
self.output_bias = self.xavier_weight_initialization(1, nodes_after)
self.output_momentum = torch.zeros(self.output_weight.shape, dtype=torch.float, device=MyDevice().get())
self.output_bias_momentum = torch.zeros(self.output_bias.shape, dtype=torch.float, device=MyDevice().get())
self.output_activation_function = self.ACTIVATION_FUNCTION_SOFTMAX
self.loss_function = self.LOSS_FUNCTION_CROSS_ENTROPY
ElasticNodes.__init__(self, len(self.layers))
##### Weight initializations #####
def xavier_weight_initialization(self, n_out: int, n_in: int, uniform: bool = False):
if uniform:
return torch.nn.init.xavier_uniform(tensor=torch.zeros(int(n_out), int(n_in), dtype=torch.float, requires_grad=True, device=MyDevice().get()))
return torch.nn.init.xavier_normal_(tensor=torch.zeros(int(n_out), int(n_in), dtype=torch.float, requires_grad=True, device=MyDevice().get()))
def he_weight_initialization(self, n_out, n_in, shape=None):
#TODO
mean = 0.0
sigma = np.sqrt(2 / n_in)
if shape is None:
shape = (n_out, n_in)
return np.random.normal(mean, sigma, shape)
##### Noise #####
def masking_noise(self, x: torch.tensor, noise_ratio=0.0):
return x.clone().masked_fill(torch.rand(x.shape, device=MyDevice().get()) <= noise_ratio, 0)
##### Activation functions #####
@staticmethod
def sigmoid(z: torch.tensor):
return torch.sigmoid(z)
@staticmethod
def tanh(z):
return torch.tanh(z)
@staticmethod
def relu(z):
return torch.nn.functional.relu(z)
@staticmethod
def linear(layer_value: torch.tensor, weight: torch.tensor, bias: torch.tensor):
return torch.nn.functional.linear(layer_value, weight, bias)
@staticmethod
def softmax(z, axis: int = 1):
return torch.nn.functional.softmax(z, dim=axis)
def reset_grad(self):
for i in range(self.number_hidden_layers):
self.weight[i] = self.weight[i].detach()
self.bias[i] = self.bias[i].detach()
self.weight[i].requires_grad = True
self.bias[i].requires_grad = True
self.output_weight = self.output_weight.detach()
self.output_bias = self.output_bias.detach()
self.output_weight.requires_grad = True
self.output_bias.requires_grad = True
def feedforward(self, x: torch.Tensor, y: torch.Tensor, train: bool = False):
return self.forward_pass(x, train=train).calculate_error(y)
def backpropagate(self):
self.loss_value.backward()
return self
def test(self, x: torch.Tensor, y: torch.Tensor, is_beta_updatable: bool = False):
self.feedforward(x=x, y=y)
m = y.shape[0]
true_classes = torch.argmax(y, axis=1)
misclassified = torch.sum(torch.ne(self.outputed_classes, true_classes)).item()
self.classification_rate = 1 - misclassified / m
if is_beta_updatable:
class_label = self.output_layer_value.max(axis=2)
for i in range(m):
if self.true_classes[i] == class_label[i]:
self.output_beta = np.max(self.output_beta * self.output_beta_decreasing_factor, 0)
self.output_beta_decreasing_factor = np.max(self.output_beta_decreasing_factor - 0.01, 0)
else:
self.output_beta = max(self.output_beta * (1 + self.output_beta_decreasing_factor), 1)
self.output_beta_decreasing_factor = max(self.output_beta_decreasing_factor + 0.01, 1)
def train(self, x: torch.Tensor, y: torch.Tensor, weight_no: int = None):
self.feedforward(x=x, y=y, train=True).backpropagate()
if weight_no is None:
for i in range(self.number_hidden_layers, -1, -1):
self.update_weight(i)
else:
self.update_weight(weight_no)
def update_weight(self, weight_no: int):
if weight_no >= self.number_hidden_layers:
dW: torch.Tensor = self.learning_rate * self.output_weight.grad
db: torch.Tensor = self.learning_rate * self.output_bias.grad
if self.momentum_rate > 0:
self.output_momentum: torch.Tensor = self.momentum_rate * self.output_momentum + dW
self.output_bias_momentum: torch.Tensor = self.momentum_rate * self.output_bias_momentum + db
dW: torch.Tensor = self.output_momentum
db: torch.Tensor = self.output_bias_momentum
self.output_weight: torch.Tensor = self.output_weight - dW
self.output_bias: torch.Tensor = self.output_bias - db
else:
dW: torch.Tensor = self.learning_rate * self.weight[weight_no].grad
db: torch.Tensor = self.learning_rate * self.bias[weight_no].grad
if self.momentum_rate > 0:
self.momentum[weight_no]: torch.Tensor = self.momentum_rate * self.momentum[weight_no] + dW
self.bias_momentum[weight_no]: torch.Tensor = self.momentum_rate * self.bias_momentum[weight_no] + db
dW: torch.Tensor = self.momentum[weight_no]
db: torch.Tensor = self.bias_momentum[weight_no]
self.weight[weight_no]: torch.Tensor = self.weight[weight_no] - dW
self.bias[weight_no]: torch.Tensor = self.bias[weight_no] - db
def forward_pass(self, x: torch.Tensor, train: bool = False):
if train:
self.reset_grad()
self.layer_value = []
self.layer_value.append(x)
for i in range(self.number_hidden_layers):
if self.activation_function[i] == self.ACTIVATION_FUNCTION_AFFINE:
self.layer_value.append(self.linear(self.layer_value[i], self.weight[i], self.bias[i]))
elif self.activation_function[i] == self.ACTIVATION_FUNCTION_SIGMOID:
self.layer_value.append(self.sigmoid(self.linear(self.layer_value[i], self.weight[i], self.bias[i])))
elif self.activation_function[i] == self.ACTIVATION_FUNCTION_TANH:
self.layer_value.append(self.tanh(self.linear(self.layer_value[i], self.weight[i], self.bias[i])))
elif self.activation_function[i] == self.ACTIVATION_FUNCTION_RELU:
self.layer_value.append(self.relu(self.linear(self.layer_value[i], self.weight[i], self.bias[i])))
elif self.activation_function[i] == self.ACTIVATION_FUNCTION_LINEAR:
raise TypeError('Not implemented')
elif self.activation_function[i] == self.ACTIVATION_FUNCTION_SOFTMAX:
self.layer_value.append(self.softmax(self.linear(self.layer_value[i], self.weight[i], self.bias[i])))
raise TypeError('Not implemented')
if self.output_activation_function == self.ACTIVATION_FUNCTION_AFFINE:
self.output_layer_value = self.linear(self.layer_value[-1], self.output_weight, self.output_bias)
elif self.output_activation_function == self.ACTIVATION_FUNCTION_SIGMOID:
self.output_layer_value = self.sigmoid(self.linear(self.layer_value[-1], self.output_weight, self.output_bias))
elif self.output_activation_function == self.ACTIVATION_FUNCTION_TANH:
self.output_layer_value = self.tanh(self.linear(self.layer_value[-1], self.output_weight, self.output_bias))
elif self.output_activation_function == self.ACTIVATION_FUNCTION_RELU:
self.output_layer_value = self.relu(self.linear(self.layer_value[-1], self.output_weight, self.output_bias))
elif self.output_activation_function == self.ACTIVATION_FUNCTION_SOFTMAX:
self.output_layer_value = self.softmax(self.linear(self.layer_value[-1], self.output_weight, self.output_bias), axis=1)
return self
def calculate_error(self, y: torch.tensor):
self.error_value = y - self.output_layer_value
if self.loss_function == self.LOSS_FUNCTION_MSE:
self.loss_value = torch.nn.functional.mse_loss(self.output_layer_value, y)
elif self.loss_function == self.LOSS_FUNCTION_CROSS_ENTROPY:
self.loss_value = torch.nn.functional.cross_entropy(self.output_layer_value, torch.argmax(y, 1))
return self
def compute_expected_values(self, in_place: bool = False):
if in_place:
data_mean, data_var, data_std = self.data_mean, self.data_variance, self.data_standard_deviation
self.number_samples_feed += 1
self.data_mean, self.data_variance, self.data_standard_deviation = \
MyUtil.recursive_mean_standard_deviation(self.layer_value[0],
self.data_mean,
self.data_variance,
self.number_samples_feed)
if self.is_agmm_able and self.agmm.M() > 0:
self.Eh, self.Eh2 = 0, 0
for gmm in self.agmm.gmm_array:
tempEh, tempEh2 = self.compute_inbound_expected_values(gmm=gmm)
self.Eh, self.Eh2 = self.Eh + tempEh, self.Eh2 + tempEh2
else:
self.Eh, self.Eh2 = self.compute_inbound_expected_values()
if in_place:
self.Eh, self.Eh2 = self.compute_inbound_expected_values()
self.data_mean, self.data_variance, self.data_standard_deviation = data_mean, data_var, data_std
self.number_samples_feed -= 1
def compute_inbound_expected_values(self, number_hidden_layer: int = None, gmm: AGMM.GMM = None):
nhl = number_hidden_layer # readability
if nhl is None:
nhl = self.number_hidden_layers - 1
if nhl == 0:
inference, center, std = (1, self.data_mean, self.data_standard_deviation) if gmm is None else (gmm.weight, gmm.center, gmm.std)
py = MyUtil.probit(center, std)
Eh = inference * self.sigmoid(self.linear(self.weight[0], py, self.bias[0].T))
else:
Eh, _ = self.compute_inbound_expected_values(number_hidden_layer=nhl - 1, gmm=gmm)
weight, bias = (self.weight[nhl], self.bias[nhl]) if nhl < self.number_hidden_layers + 1 else (self.output_weight, self.output_bias)
Eh = self.sigmoid(self.linear(weight, Eh.T, bias.T))
return Eh, Eh ** 2
@property
def Eh(self):
return self.__Eh
@Eh.setter
def Eh(self, value: torch.tensor):
self.__Eh = value
@property
def Eh2(self):
return self.__Eh2
@Eh2.setter
def Eh2(self, value: torch.tensor):
self.__Eh2 = value
@property
def Ey(self):
return self.softmax(self.linear(self.output_weight, self.Eh.T, self.output_bias.T), axis=0)
@property
def Ey2(self):
return self.softmax(self.linear(self.output_weight, self.Eh2.T, self.output_bias.T), axis=0)
@property
def network_variance(self):
return MyUtil.frobenius_norm(self.Ey2 - self.Ey ** 2)
def compute_bias(self, y):
return MyUtil.frobenius_norm((self.Ey.T - y) ** 2)
def set_agmm(self, agmm: AGMM.GMM):
self.is_agmm_able = True
self.agmm = agmm
def get_agmm(self):
return self.agmm
def run_agmm(self, x: torch.tensor, y: torch.tensor):
self.compute_expected_values(in_place=True)
self.agmm.run(x, self.compute_bias(y))
return self.agmm
def width_adaptation_stepwise(self, y, prune_strategy: int = None):
if prune_strategy is None:
prune_strategy = self.PRUNE_NODE_STRATEGY_MULTIPLE
nhl: int = self.number_hidden_layers
self.number_samples_feed = self.number_samples_feed + 1
self.number_samples_layer[nhl] = self.number_samples_layer[nhl] + 1
self.compute_expected_values()
self.bias_mean[nhl], self.bias_variance[nhl], self.bias_standard_deviation[nhl] = \
MyUtil.recursive_mean_standard_deviation(self.compute_bias(y),
self.bias_mean[nhl],
self.bias_variance[nhl],
self.number_samples_feed)
self.var_mean[nhl], self.var_variance[nhl], self.var_standard_deviation[nhl] = \
MyUtil.recursive_mean_standard_deviation(self.network_variance,
self.var_mean[nhl],
self.var_variance[nhl],
self.number_samples_feed)
if self.number_samples_layer[nhl] <= 1 or self.growable[nhl]:
self.minimum_bias_mean[nhl] = self.bias_mean[nhl]
self.minimum_bias_standard_deviation[nhl] = self.bias_standard_deviation[nhl]
else:
self.minimum_bias_mean[nhl] = np.min([self.minimum_bias_mean[nhl], self.bias_mean[nhl]])
self.minimum_bias_standard_deviation[nhl] = np.min([self.minimum_bias_standard_deviation[nhl], self.bias_standard_deviation[nhl]])
if self.number_samples_layer[nhl] <= self.input_size + 1 or self.prunable[nhl][0] != -1:
self.minimum_var_mean[nhl] = self.var_mean[nhl]
self.minimum_var_standard_deviation[nhl] = self.var_standard_deviation[nhl]
else:
self.minimum_var_mean[nhl] = np.min([self.minimum_var_mean[nhl], self.var_mean[nhl]])
self.minimum_var_standard_deviation[nhl] = np.min([self.minimum_var_standard_deviation[nhl], self.var_standard_deviation[nhl]])
self.BIAS.append(self.bias_mean[nhl])
self.VAR.append(self.var_mean[nhl])
self.growable[nhl] = self.is_growable(self.compute_bias(y))
self.prunable[nhl] = self.is_prunable(prune_strategy)
def is_growable(self, bias: torch.tensor, alpha_1: float = 1.25, alpha_2: float = 0.75):
nhl = self.number_hidden_layers # readability
current = self.bias_mean[nhl] + self.bias_standard_deviation[nhl]
biased_min = self.minimum_bias_mean[nhl] \
+ (alpha_1 * torch.exp(-bias) + alpha_2) * self.minimum_bias_standard_deviation[nhl]
if self.number_samples_layer[nhl] > 1 and current >= biased_min:
return True
return False
def is_prunable(self, prune_strategy: int = None, alpha_1: float = 2.5, alpha_2: float = 1.5):
if prune_strategy is None:
prune_strategy = self.PRUNE_NODE_STRATEGY_MULTIPLE
nhl = self.number_hidden_layers # readability
current = self.var_mean[nhl] + self.var_standard_deviation[nhl]
biased_min = self.minimum_var_mean[nhl] \
+ (alpha_1 * torch.exp(-self.network_variance) + alpha_2) * self.minimum_var_standard_deviation[nhl]
if not self.growable[nhl] \
and self.layers[nhl] > 1 \
and self.number_samples_layer[nhl] > self.input_size + 1 \
and current >= biased_min:
if prune_strategy == self.PRUNE_NODE_STRATEGY_SINGLE:
return torch.argmin(self.Eh)
elif prune_strategy == self.PRUNE_NODE_STRATEGY_MULTIPLE:
nodes_to_prune = torch.where(self.Eh < torch.abs(torch.mean(self.Eh) - torch.var(self.Eh)))
if len(nodes_to_prune[0]):
return nodes_to_prune[0]
else:
return torch.argmin(self.Eh)
return [-1]
def grow_node(self, layer_number: int):
self.layers[layer_number] += 1
if layer_number >= 0:
self.grow_weight_row(layer_number - 1)
self.grow_bias(layer_number - 1)
if layer_number <= self.number_hidden_layers:
self.grow_weight_column(layer_number)
def grow_weight_row(self, layer_number: int):
def add_element(tensor_data: torch.tensor, momentum_tensor_data: torch.tensor, n_out: int):
tensor_data = torch.cat((tensor_data, self.xavier_weight_initialization(1, n_out)), axis=0)
momentum_tensor_data = torch.cat((momentum_tensor_data, torch.zeros(1, n_out, dtype=torch.float, device=MyDevice().get())), axis=0)
return tensor_data, momentum_tensor_data
if layer_number >= len(self.weight):
[_, n_out] = self.output_weight.shape
self.output_weight, self.output_momentum = add_element(self.output_weight, self.output_momentum, n_out)
else:
[_, n_out] = self.weight[layer_number].shape
self.weight[layer_number], self.momentum[layer_number] = add_element(self.weight[layer_number], self.momentum[layer_number], n_out)
def grow_weight_column(self, layer_number: int):
def add_element(tensor_data: torch.tensor, momentum_tensor_data: torch.tensor, n_out: int):
tensor_data = torch.cat((tensor_data, self.xavier_weight_initialization(n_out, 1)), axis=1)
momentum_tensor_data = torch.cat((momentum_tensor_data, torch.zeros(n_out, 1, dtype=torch.float, device=MyDevice().get())), axis=1)
return tensor_data, momentum_tensor_data
if layer_number >= len(self.weight):
[n_out, _] = self.output_weight.shape
self.output_weight, self.output_momentum = add_element(self.output_weight, self.output_momentum, n_out)
else:
[n_out, _] = self.weight[layer_number].shape
self.weight[layer_number], self.momentum[layer_number] = add_element(self.weight[layer_number], self.momentum[layer_number], n_out)
def grow_bias(self, layer_number):
def add_element(tensor_data: torch.tensor, momentum_tensor_data: torch.tensor, n_out: int):
tensor_data = torch.cat((tensor_data, self.xavier_weight_initialization(1, n_out)), axis=1)
momentum_tensor_data = torch.cat((momentum_tensor_data, torch.zeros(1, n_out, dtype=torch.float, device=MyDevice().get())), axis=1)
return tensor_data, momentum_tensor_data
if layer_number >= len(self.bias):
[n_out, _] = self.output_bias.shape
self.output_bias, self.output_bias_momentum = add_element(self.output_bias, self.output_bias_momentum, n_out)
else:
[n_out, _] = self.bias[layer_number].shape
self.bias[layer_number], self.bias_momentum[layer_number] = add_element(self.bias[layer_number], self.bias_momentum[layer_number], n_out)
pass
def prune_node(self, layer_number: int, node_number: int):
self.layers[layer_number] -= 1
if layer_number >= 0:
self.prune_weight_row(layer_number - 1, node_number)
self.prune_bias(layer_number - 1, node_number)
if layer_number <= self.number_hidden_layers:
self.prune_weight_column(layer_number, node_number)
def prune_weight_row(self, layer_number: int, node_number: int):
def remove_nth_row(tensor_data: torch.tensor, n: int):
return torch.cat([tensor_data[:n], tensor_data[n+1:]])
if layer_number >= len(self.weight):
self.output_weight = remove_nth_row(self.output_weight, node_number)
self.output_momentum = remove_nth_row(self.output_momentum, node_number)
else:
self.weight[layer_number] = remove_nth_row(self.weight[layer_number], node_number)
self.momentum[layer_number] = remove_nth_row(self.momentum[layer_number], node_number)
def prune_weight_column(self, layer_number: int, node_number: int):
def remove_nth_column(weight_tensor: torch.tensor, n: int):
return torch.cat([weight_tensor.T[:n], weight_tensor.T[n+1:]]).T
if layer_number >= len(self.weight):
self.output_weight = remove_nth_column(self.output_weight, node_number)
self.output_momentum = remove_nth_column(self.output_momentum, node_number)
else:
self.weight[layer_number] = remove_nth_column(self.weight[layer_number], node_number)
self.momentum[layer_number] = remove_nth_column(self.momentum[layer_number], node_number)
def prune_bias(self, layer_number: int, node_number: int):
def remove_nth_element(bias_tensor: torch.tensor, n: int):
bias_tensor = torch.cat([bias_tensor[0][:n], bias_tensor[0][n+1:]])
return bias_tensor.view(1, bias_tensor.shape[0])
if layer_number >= len(self.bias):
self.output_bias = remove_nth_element(self.output_bias, node_number)
self.output_bias_momentum = remove_nth_element(self.output_bias_momentum, node_number)
else:
self.bias[layer_number] = remove_nth_element(self.bias[layer_number], node_number)
self.bias_momentum[layer_number] = remove_nth_element(self.bias_momentum[layer_number], node_number)

View File

@ -1,2 +1,79 @@
# Reference
[ArXiv -> ATL: Autonomous Knowledge Transfer from Many Streaming Processes](https://arxiv.org/abs/1910.03434)
[ResearchGate -> ATL: Autonomous Knowledge Transfer from Many Streaming Processes](https://www.researchgate.net/publication/336361712_ATL_Autonomous_Knowledge_Transfer_from_Many_Streaming_Processes)
# Notes
If you want to see the original code used for this paper, access [ATL_Matlab](https://github.com/Ivsucram/ATL_Matlab)
`ATL_Python` is a reconstruction of `ATL_Matlab` by the same author, but using Python 3.6 and PyTorch (with autograd enabled and GPU support). The code is still not one-to-one and some differences in results can be found (specially on the data split methods in `DataManipulator`, however the network structure is correct and can be used by whoever is interested on this work in order to understand the structure or to build comparative results with your own research work.
Having said that, expect `ATL_Python` to be updated in the following weeks, including functions refactoring and functions documentation.
# ATL_Python # ATL_Python
ATL code converted to Python 3
ATL: Autonomous Knowledge Transfer From Many Streaming Processes
ACM CIKM 2019
1. Clone `ATL_Python` git to your computer, or just download the files.
2. Provide a dataset by replacing the file `data.csv`
The current `data.csv` holds [https://www.researchgate.net/publication/221653408_A_Streaming_Ensemble_Algorithm_SEA_for_Large-Scale_Classification](SEA) dataset.
`data.csv` must be prepared as following:
```
- Each row presents a new data sample
- Each column presents a data feature
- The last column presents the label for that sample. Don't use one-hot encoding. Use a format from 1 onwards
```
3. Open Matlab. The code was developed using Matlab 2018b, so if you use an older version, you might get some incompability errors.
You can use Matlab 2018b or newer.
Matlab may prompt you to install some official add-ons, as:
```
- Deep Learning Toolbox
- Fuzzy Logic Toolbox
- Digital Processing Signal Toolbox
```
4. Inside Matlab, travel until the folder where you downloaded `ATL_Matlab`.
5. On the Matlab terminal, just type `ATL`. This will execute ATL, which will read your data.csv and process it.
ATL will automatically normalize your data and split your data into 2 streams (Source and Target data streams) with a bias between them, as described in the paper.
Matlab will print ATL status at the end of every minibatch, where you will be able to follow useful information as:
```
- Training time (maximum, mean, minimum, current and accumulated)
- Testing time (maximum, mean, minimum, current and accumulated)
- The number of GMM clusters (maximum, mean, minimum and current)
- The target classification rate
- And a quick review of ATL structure (both discriminative and generative phases), where you can see how many automatically generated nodes were created.
```
At the end of the process, Matlab will plot 6 graphs:
```
- Network bias and Network variance w.r.t. the generative phase
- Network bias and Network variance w.r.t. the discriminative phase
- The target and source classification rate evolution, as well as the final mean accuracy of the network
- All losses over time, and how they influence the network learning
- The evolution of GMMs on Source and Taret AGMMs over time
- The processing time per mini-batch and the total processing time as well, both for training and testing
```
Thank you.
# Download all datasets used on the paper
As some datasets are too big, we can't upload them to GitHub. GitHub has a size limite of 35MB per file. Because of that, you can find all the datasets in a csv format on the anonymous link below. To test it, copy the desired dataset to the same foler as ATL and rename it to `data.csv`.
- [https://drive.google.com/open?id=1emgVw6muSodzozQcuz7ks8XeZYPxGEZ7](https://drive.google.com/open?id=1emgVw6muSodzozQcuz7ks8XeZYPxGEZ7)

100000
data.csv Normal file

File diff suppressed because it is too large Load Diff