Skip to content

Commit

Permalink
Branch seems ready to be merged and deployed.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugarosa committed May 12, 2021
1 parent de98408 commit fd1ba07
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 55 deletions.
2 changes: 1 addition & 1 deletion examples/integrations/learnergy/dropout_rbm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import torchvision
from learnergy.models.binary import DropoutRBM
from learnergy.models.bernoulli import DropoutRBM

from opytimizer import Opytimizer
from opytimizer.core import Function
Expand Down
2 changes: 1 addition & 1 deletion examples/integrations/learnergy/rbm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import torchvision
from learnergy.models.binary import RBM
from learnergy.models.bernoulli import RBM

from opytimizer import Opytimizer
from opytimizer.core import Function
Expand Down
18 changes: 7 additions & 11 deletions examples/integrations/nalp/lstm.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import tensorflow as tf
from nalp.corpus.text import TextCorpus
from nalp.datasets.language_modeling import LanguageModelingDataset
from nalp.encoders.integer import IntegerEncoder
from nalp.models.generators.lstm import LSTMGenerator
from nalp.corpus import TextCorpus
from nalp.datasets import LanguageModelingDataset
from nalp.encoders import IntegerEncoder
from nalp.models.generators import LSTMGenerator

from opytimizer import Opytimizer
from opytimizer.core import Function
Expand All @@ -12,17 +12,13 @@
# Creates a character TextCorpus from file
corpus = TextCorpus(from_file='examples/integrations/nalp/chapter1_harry.txt', corpus_type='char')

# Creates an IntegerEncoder
# Creating an IntegerEncoder, learning encoding and encoding tokens
encoder = IntegerEncoder()

# Learns the encoding based on the TextCorpus dictionary and reverse dictionary
encoder.learn(corpus.vocab_index, corpus.index_vocab)

# Applies the encoding on new data
encoded_tokens = encoder.encode(corpus.tokens)

# Creates Language Modeling Dataset
dataset = LanguageModelingDataset(encoded_tokens, max_length=10, batch_size=64)
# Creating Language Modeling Dataset
dataset = LanguageModelingDataset(encoded_tokens, max_contiguous_pad_length=10, batch_size=64)


def lstm(opytimizer):
Expand Down
18 changes: 7 additions & 11 deletions examples/integrations/nalp/rnn.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import tensorflow as tf
from nalp.corpus.text import TextCorpus
from nalp.datasets.language_modeling import LanguageModelingDataset
from nalp.encoders.integer import IntegerEncoder
from nalp.models.generators.rnn import RNNGenerator
from nalp.corpus import TextCorpus
from nalp.datasets import LanguageModelingDataset
from nalp.encoders import IntegerEncoder
from nalp.models.generators import RNNGenerator

from opytimizer import Opytimizer
from opytimizer.core import Function
Expand All @@ -12,17 +12,13 @@
# Creates a character TextCorpus from file
corpus = TextCorpus(from_file='examples/integrations/nalp/chapter1_harry.txt', corpus_type='char')

# Creates an IntegerEncoder
# Creating an IntegerEncoder, learning encoding and encoding tokens
encoder = IntegerEncoder()

# Learns the encoding based on the TextCorpus dictionary and reverse dictionary
encoder.learn(corpus.vocab_index, corpus.index_vocab)

# Applies the encoding on new data
encoded_tokens = encoder.encode(corpus.tokens)

# Creates Language Modeling Dataset
dataset = LanguageModelingDataset(encoded_tokens, max_length=10, batch_size=64)
# Creating Language Modeling Dataset
dataset = LanguageModelingDataset(encoded_tokens, max_contiguous_pad_length=10, batch_size=64)


def rnn(opytimizer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from opytimizer.optimizers.boolean import BPSO
from opytimizer.spaces import BooleanSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
Expand All @@ -19,7 +19,7 @@
# Adding 1 to labels, i.e., OPF should have labels from 1+
Y += 1

# Splitting data into training and testing sets
# Splits data into training and testing sets
X_train, X_val, Y_train, Y_val = s.split(
X, Y, percentage=0.5, random_state=1)

Expand Down
4 changes: 2 additions & 2 deletions examples/integrations/opfython/unsupervised_opf_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
Expand All @@ -18,7 +18,7 @@
# Adding 1 to labels, i.e., OPF should have labels from 1+
Y += 1

# Splitting data into training and testing sets
# Splits data into training and testing sets
X_train, X_test, Y_train, Y_test = s.split(
X, Y, percentage=0.5, random_state=1)

Expand Down
10 changes: 5 additions & 5 deletions examples/integrations/pytorch/enhanced_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
X = digits.data
Y = digits.target

# Splitting the data
# Splits the data
X_train, X_val, Y_train, Y_val = train_test_split(
X, Y, test_size=0.5, random_state=42)

# Converting from numpy array to torch tensors
# Converts from numpy array to torch tensors
X_train = torch.from_numpy(X_train).float()
X_val = torch.from_numpy(X_val).float()
Y_train = torch.from_numpy(Y_train).long()
Expand All @@ -32,7 +32,7 @@ def fit(model, loss, opt, x, y):
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

# Resetting the gradient
# Resets the gradient
opt.zero_grad()

# Performs the foward pass
Expand All @@ -55,7 +55,7 @@ def predict(model, x_val):
# Performs backward pass with this variable
output = model.forward(x)

# Getting the index of the prediction
# Gets the index of the prediction
y_val = output.data.numpy().argmax(axis=1)

return y_val
Expand Down
2 changes: 1 addition & 1 deletion examples/integrations/pytorch/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fit(model, loss, opt, x, y):
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

# Resetting the gradient
# Resets the gradient
opt.zero_grad()

# Performs the foward pass
Expand Down
10 changes: 5 additions & 5 deletions examples/integrations/pytorch/logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
X = digits.data
Y = digits.target

# Splitting the data
# Splits the data
X_train, X_val, Y_train, Y_val = train_test_split(
X, Y, test_size=0.5, random_state=42)

# Converting from numpy array to torch tensors
# Converts from numpy array to torch tensors
X_train = torch.from_numpy(X_train).float()
X_val = torch.from_numpy(X_val).float()
Y_train = torch.from_numpy(Y_train).long()
Expand All @@ -32,7 +32,7 @@ def fit(model, loss, opt, x, y):
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

# Resetting the gradient
# Resets the gradient
opt.zero_grad()

# Performs the foward pass
Expand All @@ -55,7 +55,7 @@ def predict(model, x_val):
# Performs backward pass with this variable
output = model.forward(x)

# Getting the index of the prediction
# Gets the index of the prediction
y_val = output.data.numpy().argmax(axis=1)

return y_val
Expand Down
14 changes: 7 additions & 7 deletions examples/integrations/pytorch/lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
X = digits.data
Y = digits.target

# Splitting the data
# Splits the data
X_train, X_val, Y_train, Y_val = train_test_split(
X, Y, test_size=0.5, random_state=42)

# Reshaping the data
# Reshapes the data
X_train = X_train.reshape(-1, 8, 8)
X_val = X_val.reshape(-1, 8, 8)

# Converting to sequence shape
# Converts to sequence shape
X_train = np.swapaxes(X_train, 0, 1)
X_val = np.swapaxes(X_val, 0, 1)

# Converting from numpy array to torch tensors
# Converts from numpy array to torch tensors
X_train = torch.from_numpy(X_train).float()
X_val = torch.from_numpy(X_val).float()
Y_train = torch.from_numpy(Y_train).long()
Expand Down Expand Up @@ -72,7 +72,7 @@ def fit(model, loss, opt, x, y):
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

# Resetting the gradient
# Resets the gradient
opt.zero_grad()

# Performs the foward pass
Expand All @@ -95,7 +95,7 @@ def predict(model, x_val):
# Performs backward pass with this variable
output = model.forward(x)

# Getting the index of the prediction
# Gets the index of the prediction
y_val = output.data.numpy().argmax(axis=1)

return y_val
Expand Down
10 changes: 5 additions & 5 deletions examples/integrations/pytorch/neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
X = digits.data
Y = digits.target

# Splitting the data
# Splits the data
X_train, X_val, Y_train, Y_val = train_test_split(
X, Y, test_size=0.5, random_state=42)

# Converting from numpy array to torch tensors
# Converts from numpy array to torch tensors
X_train = torch.from_numpy(X_train).float()
X_val = torch.from_numpy(X_val).float()
Y_train = torch.from_numpy(Y_train).long()
Expand All @@ -32,7 +32,7 @@ def fit(model, loss, opt, x, y):
x = Variable(x, requires_grad=False)
y = Variable(y, requires_grad=False)

# Resetting the gradient
# Resets the gradient
opt.zero_grad()

# Performs the foward pass
Expand All @@ -55,7 +55,7 @@ def predict(model, x_val):
# Performs backward pass with this variable
output = model.forward(x)

# Getting the index of the prediction
# Gets the index of the prediction
y_val = output.data.numpy().argmax(axis=1)

return y_val
Expand Down
2 changes: 1 addition & 1 deletion examples/integrations/sklearn/k_means_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
Expand Down
2 changes: 1 addition & 1 deletion examples/integrations/sklearn/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading digits dataset
# Loads digits dataset
digits = load_digits()

# Gathers samples and targets
Expand Down
2 changes: 1 addition & 1 deletion examples/integrations/tensorflow/cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Loading CIFAR-10 data
# Loads CIFAR-10 data
(X_train, Y_train), (X_val, Y_val) = datasets.cifar10.load_data()

# Normalizes inputs between 0 and 1
Expand Down
2 changes: 1 addition & 1 deletion opytimizer/optimizers/swarm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from opytimizer.optimizers.swarm.mfo import MFO
from opytimizer.optimizers.swarm.mrfo import MRFO
from opytimizer.optimizers.swarm.pio import PIO
from opytimizer.optimizers.swarm.pso import PSO, AIWPSO, RPSO, SAVPSO, VPSO
from opytimizer.optimizers.swarm.pso import AIWPSO, PSO, RPSO, SAVPSO, VPSO
from opytimizer.optimizers.swarm.sbo import SBO
from opytimizer.optimizers.swarm.sca import SCA
from opytimizer.optimizers.swarm.sfo import SFO
Expand Down

0 comments on commit fd1ba07

Please sign in to comment.