<jupyter_start><jupyter_text>Installing Dependencies<jupyter_code>!pip install --upgrade onn
!pip install -U imbalanced-learn<jupyter_output>Requirement already up-to-date: imbalanced-learn in /usr/local/lib/python3.6/dist-packages (0.5.0)
Requirement already satisfied, skipping upgrade: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from imbalanced-learn) (0.13.2)
Requirement already satisfied, skipping upgrade: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from imbalanced-learn) (1.16.4)
Requirement already satisfied, skipping upgrade: scipy>=0.17 in /usr/local/lib/python3.6/dist-packages (from imbalanced-learn) (1.3.0)
Requirement already satisfied, skipping upgrade: scikit-learn>=0.21 in /usr/local/lib/python3.6/dist-packages (from imbalanced-learn) (0.21.2)<jupyter_text>Importing Dependencies<jupyter_code>from onn.OnlineNeuralNetwork import ONN
from onn.OnlineNeuralNetwork import ONN_THS
from sklearn.datasets import make_classification, make_circles
from sklearn.model_selection import train_test_split
import torch
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from imblearn.datasets import make_imbalance
import numpy as np<jupyter_output>Using TensorFlow backend.<jupyter_text>Initializing Network<jupyter_code>onn_network = ONN(features_size=10, max_num_hidden_layers=5, qtd_neuron_per_hidden_layer=40, n_classes=10)<jupyter_output><empty_output><jupyter_text>Creating Fake Classification Dataset<jupyter_code>X, Y = make_classification(n_samples=50000, n_features=10, n_informative=4, n_redundant=0, n_classes=10,
                           n_clusters_per_class=1, class_sep=3)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=42, shuffle=True)<jupyter_output><empty_output><jupyter_text>Learning and predicting at the same time<jupyter_code>for i in range(len(X_train)):
  onn_network.partial_fit(np.asarray([X_train[i, :]]), np.asarray([y_train[i]]))
  
  if i % 1000 == 0:
    predictions = onn_network.predict(X_test)
    print("Online Accuracy: {}".format(balanced_accuracy_score(y_test, predictions)))<jupyter_output>Online Accuracy: 0.14337461746314914
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.83890086 0.04027476 0.04027476 0.04027476 0.04027476]
Training Loss: 1.296051
Online Accuracy: 0.9606844639729234
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.839583   0.04010423 0.04010423 0.04010423 0.04010423]
Training Loss: 0.42200255
Online Accuracy: 0.9587807527275185
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.8393374  0.04016563 0.04016563 0.04016563 0.04016563]
Training Loss: 0.3702089
Online Accuracy: 0.967292853621438
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.83981675 0.0400458  0.0400458  0.0400458  0.0400458 ]
Training Loss: 0.26317188
Online Accuracy: 0.9654591078178111
WARNING: Set 'show_loss' to 'False' when not debugging. It will[...]<jupyter_text>Learning in batch with CUDA<jupyter_code>onn_network = ONN(features_size=10, max_num_hidden_layers=5, qtd_neuron_per_hidden_layer=40, n_classes=10, batch_size=10, use_cuda=True)
from torch.utils.data import Dataset, DataLoader
class Dataset(Dataset):

  def __init__(self, X, Y):
    self.X = X
    self.Y = Y

  def __len__(self):
      return len(self.X)

  def __getitem__(self, idx):
      X = self.X[idx],
      Y = self.Y[idx]

      return X, Y
    
transformed_dataset = Dataset(X_train, y_train)
dataloader = DataLoader(transformed_dataset, batch_size=10,shuffle=True, num_workers=1)
for local_X, local_y in dataloader: 
  onn_network.partial_fit(np.squeeze(torch.stack(local_X).numpy()), local_y.numpy())
predictions = onn_network.predict(X_test)
print("Accuracy: {}".format(balanced_accuracy_score(y_test, predictions)))<jupyter_output>Accuracy: 0.9517950352535276<jupyter_text>Using contextual bandit - ONN_THS In this example the ONN acts like a contextual bandits a reinforcement learning algorithm type.<jupyter_code>X_linear, Y_linear = make_classification(n_samples=10000, n_features=2, n_informative=2, n_redundant=0, n_classes=2, n_clusters_per_class=1, class_sep=200, shuffle=True)
X_non_linear, Y_non_linear = make_circles(n_samples=10000, noise=0.1, factor=0.3, shuffle=True)
X_linear_2, Y_linear_2 = make_classification(n_samples=10000, n_features=2, n_informative=2, n_redundant=0, n_classes=2, n_clusters_per_class=1, class_sep=200, shuffle=True)

X_linear_train = X_linear[:5000]
Y_linear_train = Y_linear[:5000]

X_linear_test = X_linear[5000:]
Y_linear_test = Y_linear[5000:]

X_non_linear_train = X_non_linear[:5000]
Y_non_linear_train = Y_non_linear[:5000]

X_non_linear_test = X_non_linear[5000:]
Y_non_linear_test = Y_non_linear[5000:]

X_linear_train_2 = X_linear_2[:5000]
Y_linear_train_2 = Y_linear_2[:5000]

X_linear_test_2 = X_linear_2[5000:]
Y_linear_test_2 = Y_linear_2[5000:]
gp = ONN_THS(2, 5, 100, 2)
for epoch in range(5):

    for i in range(len(X_linear_train)):
        x = np.asarray([X_linear_train[i, :]])
        y = np.asarray([Y_linear_train[i]])

        arm, exp = gp.predict(x)
        
        if arm == y[0]:  
          gp.partial_fit(x, y, exp)
          
        if i % 2000 == 1999:
          pred = []
          print("======================================================")
          for i in range(len(X_linear_test)):  
            pred.append(gp.predict(np.asarray([X_linear_test[i, :]]))[0])
          print("Accuracy: " + str(balanced_accuracy_score(Y_linear_test, pred)))
          print("======================================================")

print('Finished Training')
for epoch in range(5):

    for i in range(len(X_non_linear_train)):
        x = np.asarray([X_non_linear_train[i, :]])
        y = np.asarray([Y_non_linear_train[i]])

        arm, exp = gp.predict(x)
        
        if arm == y[0]:  
          gp.partial_fit(x, y, exp)
          
        if i % 2000 == 1999:
          pred = []
          print("======================================================")
          for i in range(len(X_linear_test)):  
            pred.append(gp.predict(np.asarray([X_non_linear_test[i, :]]))[0])
          print("Accuracy: " + str(balanced_accuracy_score(Y_non_linear_test, pred)))
          print("======================================================")

print('Finished Training')<jupyter_output>WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.7324713  0.06278172 0.07632934 0.0627818  0.06563586]
Training Loss: 0.565434
======================================================
Accuracy: 0.4978415196546431
======================================================
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.83837765 0.04006734 0.04134858 0.04006734 0.04013911]
Training Loss: 0.32934776
======================================================
Accuracy: 0.6431012228961956
======================================================
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.83944225 0.04013941 0.04013941 0.04013941 0.04013941]
Training Loss: 0.27433354
WARNING: Set 'show_loss' to 'False' when not debugging. It will deteriorate the fitting performance.
Alpha:[0.83982235 0.04004439 0.04004439 0.04004439 0.[...]<jupyter_text>Imbalanced Dataset<jupyter_code>X, Y = make_classification(n_samples=110000, n_features=20, n_classes=10, n_informative=8, n_redundant=0, n_clusters_per_class=1, class_sep=900)
X_t, Y_t = make_imbalance(X, Y, sampling_strategy={0: 800, 1: 5000, 2: 10000, 3: 10000, 4: 1000, 5: 1000, 6: 500, 7: 10000, 8: 5000, 9:5000})
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)

X_train_t, X_test_t, y_train_t, y_test_t = train_test_split(X_t, Y_t, test_size=0.2)
gp = ONN_THS(20, 5, 100, 10)
for i in range(len(X_train)):
    x = np.asarray([X_train[i, :]])
    y = np.asarray([y_train[i]])

    arm, exp = gp.predict(x)

    if arm == y[0]:  
      gp.partial_fit(x, y, exp)

    if i % 2000 == 1999:
      pred = []
      print("======================================================")
      for i in range(len(X_test)):  
        pred.append(gp.predict(np.asarray([X_test[i, :]]))[0])
      print("Accuracy: " + str(balanced_accuracy_score(y_test, pred)))
      print("======================================================")

print('Finished Training')
gp = ONN_THS(20, 5, 100, 10)
for i in range(len(X_train_t)):
    x = np.asarray([X_train_t[i, :]])
    y = np.asarray([y_train_t[i]])

    arm, exp = gp.predict(x)

    if arm == y[0]:  
      gp.partial_fit(x, y, exp)

    if i % 2000 == 1999:
      pred = []
      print("======================================================")
      for i in range(len(X_test_t)):  
        pred.append(gp.predict(np.asarray([X_test_t[i, :]]))[0])
      print("Accuracy: " + str(balanced_accuracy_score(y_test_t, pred)))
      print("======================================================")

print('Finished Training')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18488485241647745, 'fraction_numerical': 0.09146934803762569, 'mean_word_length': 5.174899866488651, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '109579', 'license': 'Apache-2.0', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3237, 'n_words': 674, 'path': 'ONN_Usage_Example.ipynb', 'repo_name': 'vu-luong/onn', 'size': '9249'}