Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
张 建国
onn在线训练
Commits
a457886e
Commit
a457886e
authored
Jun 19, 2019
by
Alison Carrera
Browse files
Added new contextual bandit algorithm and updated example notebook.
parent
ed53456a
Changes
3
Show whitespace changes
Inline
Side-by-side
ONN_Usage_Example.ipynb
View file @
a457886e
...
@@ -3,23 +3,25 @@
...
@@ -3,23 +3,25 @@
#Installing Dependencies
#Installing Dependencies
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
!pip install onn
!pip install
--upgrade
onn
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
##Importing Dependencies
##Importing Dependencies
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
```
from onn.OnlineNeuralNetwork import ONN
from onn.OnlineNeuralNetwork import ONN
from sklearn.datasets import make_classification
from onn.OnlineNeuralNetwork import ONN_THS
from sklearn.datasets import make_classification, make_circles
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split
import torch
from sklearn.metrics import accuracy_score
from sklearn.metrics import accuracy_score
import numpy as np
import numpy as np
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
...
@@ -56,5 +58,148 @@
...
@@ -56,5 +58,148 @@
if i % 1000 == 0:
if i % 1000 == 0:
predictions = onn_network.predict(X_test)
predictions = onn_network.predict(X_test)
print("Online Accuracy: {}".format(accuracy_score(y_test, predictions)))
print("Online Accuracy: {}".format(accuracy_score(y_test, predictions)))
```
```
%% Cell type:markdown id: tags:
# Learning in batch with CUDA
%% Cell type:code id: tags:
```
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)
```
%% Cell type:code id: tags:
```
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)
```
%% Cell type:code id: tags:
```
for local_X, local_y in dataloader:
onn_network.partial_fit(np.squeeze(torch.stack(local_X).numpy()), local_y.numpy())
```
%% Cell type:code id: tags:
```
predictions = onn_network.predict(X_test)
print("Accuracy: {}".format(accuracy_score(y_test, predictions)))
```
%% Cell type:markdown id: tags:
#Using contextual bandit - ONN_THS
%% Cell type:markdown id: tags:
In this example the ONN acts like a contextual bandits a reinforcement learning algorithm type.
%% Cell type:code id: tags:
```
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:]
```
%% Cell type:code id: tags:
```
gp = ONN_THS(2, 5, 100, 2)
```
%% Cell type:code id: tags:
```
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(accuracy_score(Y_linear_test, pred)))
print("======================================================")
print('Finished Training')
```
%% Cell type:code id: tags:
```
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(accuracy_score(Y_non_linear_test, pred)))
print("======================================================")
print('Finished Training')
```
%% Cell type:code id: tags:
```
```
...
...
onn/OnlineNeuralNetwork.py
View file @
a457886e
...
@@ -153,7 +153,7 @@ class ONN(nn.Module):
...
@@ -153,7 +153,7 @@ class ONN(nn.Module):
class
ONN_THS
(
ONN
):
class
ONN_THS
(
ONN
):
def
__init__
(
self
,
features_size
,
max_num_hidden_layers
,
qtd_neuron_per_hidden_layer
,
n_classes
,
b
=
0.99
,
n
=
0.01
,
def
__init__
(
self
,
features_size
,
max_num_hidden_layers
,
qtd_neuron_per_hidden_layer
,
n_classes
,
b
=
0.99
,
n
=
0.01
,
s
=
0.2
,
e
=
[
0.5
,
0.35
,
0.2
,
0.1
],
use_cuda
=
False
):
s
=
0.2
,
e
=
[
0.5
,
0.35
,
0.2
,
0.1
,
0.05
],
use_cuda
=
False
):
super
().
__init__
(
features_size
,
max_num_hidden_layers
,
qtd_neuron_per_hidden_layer
,
n_classes
,
b
=
b
,
n
=
n
,
s
=
s
,
super
().
__init__
(
features_size
,
max_num_hidden_layers
,
qtd_neuron_per_hidden_layer
,
n_classes
,
b
=
b
,
n
=
n
,
s
=
s
,
use_cuda
=
use_cuda
)
use_cuda
=
use_cuda
)
self
.
e
=
e
self
.
e
=
e
...
...
setup.py
View file @
a457886e
...
@@ -6,7 +6,7 @@ with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
...
@@ -6,7 +6,7 @@ with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description
=
f
.
read
()
long_description
=
f
.
read
()
setup
(
name
=
'onn'
,
setup
(
name
=
'onn'
,
version
=
'0.1.
1
'
,
version
=
'0.1.
3
'
,
description
=
'Online Neural Network'
,
description
=
'Online Neural Network'
,
url
=
'https://github.com/alison-carrera/onn'
,
url
=
'https://github.com/alison-carrera/onn'
,
author
=
'Alison Carrera'
,
author
=
'Alison Carrera'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment