Giter VIP home page Giter VIP logo

torchga's Introduction

TorchGA: Training PyTorch Models using the Genetic Algorithm

TorchGA is part of the PyGAD library for training PyTorch models using the genetic algorithm (GA). This feature is supported starting from PyGAD 2.10.0.

The TorchGA project has a single module named torchga.py which has a class named TorchGA for preparing an initial population of PyTorch model parameters.

PyGAD is an open-source Python library for building the genetic algorithm and training machine learning algorithms. Check the library's documentation at Read The Docs: https://pygad.readthedocs.io

Donation

Installation

To install PyGAD, simply use pip to download and install the library from PyPI (Python Package Index). The library lives a PyPI at this page https://pypi.org/project/pygad.

pip3 install pygad

To get started with PyGAD, please read the documentation at Read The Docs https://pygad.readthedocs.io.

PyGAD Source Code

The source code of the PyGAD modules is found in the following GitHub projects:

The documentation of PyGAD is available at Read The Docs https://pygad.readthedocs.io.

PyGAD Documentation

The documentation of the PyGAD library is available at Read The Docs at this link: https://pygad.readthedocs.io. It discusses the modules supported by PyGAD, all its classes, methods, attribute, and functions. For each module, a number of examples are given.

If there is an issue using PyGAD, feel free to post at issue in this GitHub repository https://github.com/ahmedfgad/GeneticAlgorithmPython or by sending an e-mail to [email protected].

If you built a project that uses PyGAD, then please drop an e-mail to [email protected] with the following information so that your project is included in the documentation.

  • Project title
  • Brief description
  • Preferably, a link that directs the readers to your project

Please check the Contact Us section for more contact details.

Life Cycle of PyGAD

The next figure lists the different stages in the lifecycle of an instance of the pygad.GA class. Note that PyGAD stops when either all generations are completed or when the function passed to the on_generation parameter returns the string stop.

PyGAD Lifecycle

The next code implements all the callback functions to trace the execution of the genetic algorithm. Each callback function prints its name.

import pygad
import numpy

function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

def fitness_func(ga_instance, solution, solution_idx):
    output = numpy.sum(solution*function_inputs)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    return fitness

fitness_function = fitness_func

def on_start(ga_instance):
    print("on_start()")

def on_fitness(ga_instance, population_fitness):
    print("on_fitness()")

def on_parents(ga_instance, selected_parents):
    print("on_parents()")

def on_crossover(ga_instance, offspring_crossover):
    print("on_crossover()")

def on_mutation(ga_instance, offspring_mutation):
    print("on_mutation()")

def on_generation(ga_instance):
    print("on_generation()")

def on_stop(ga_instance, last_population_fitness):
    print("on_stop()")

ga_instance = pygad.GA(num_generations=3,
                       num_parents_mating=5,
                       fitness_func=fitness_function,
                       sol_per_pop=10,
                       num_genes=len(function_inputs),
                       on_start=on_start,
                       on_fitness=on_fitness,
                       on_parents=on_parents,
                       on_crossover=on_crossover,
                       on_mutation=on_mutation,
                       on_generation=on_generation,
                       on_stop=on_stop)

ga_instance.run()

Based on the used 3 generations as assigned to the num_generations argument, here is the output.

on_start()

on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation()

on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation()

on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation()

on_stop()

Examples

Check the PyGAD's documentation for more examples information. You can also find more information about the implementation of the examples.

Example 1: Regression Model

import torch
import torchga
import pygad

def fitness_func(ga_instance, solution, sol_idx):
    global data_inputs, data_outputs, torch_ga, model, loss_function

    model_weights_dict = torchga.model_weights_as_dict(model=model,
                                                       weights_vector=solution)

    # Use the current solution as the model parameters.
    model.load_state_dict(model_weights_dict)

    predictions = model(data_inputs)
    abs_error = loss_function(predictions, data_outputs).detach().numpy() + 0.00000001

    solution_fitness = 1.0 / abs_error

    return solution_fitness

def callback_generation(ga_instance):
    print("Generation = {generation}".format(generation=ga_instance.generations_completed))
    print("Fitness    = {fitness}".format(fitness=ga_instance.best_solution()[1]))

# Create the PyTorch model.
input_layer = torch.nn.Linear(3, 5)
relu_layer = torch.nn.ReLU()
output_layer = torch.nn.Linear(5, 1)

model = torch.nn.Sequential(input_layer,
                            relu_layer,
                            output_layer)
# print(model)

# Create an instance of the pygad.torchga.TorchGA class to build the initial population.
torch_ga = torchga.TorchGA(model=model,
                           num_solutions=10)

loss_function = torch.nn.L1Loss()

# Data inputs
data_inputs = torch.tensor([[0.02, 0.1, 0.15],
                            [0.7, 0.6, 0.8],
                            [1.5, 1.2, 1.7],
                            [3.2, 2.9, 3.1]])

# Data outputs
data_outputs = torch.tensor([[0.1],
                             [0.6],
                             [1.3],
                             [2.5]])

# Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/pygad.html#pygad-ga-class
num_generations = 250 # Number of generations.
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
initial_population = torch_ga.population_weights # Initial population of network weights

ga_instance = pygad.GA(num_generations=num_generations, 
                       num_parents_mating=num_parents_mating, 
                       initial_population=initial_population,
                       fitness_func=fitness_func,
                       on_generation=callback_generation)

ga_instance.run()

# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
ga_instance.plot_fitness(title="PyGAD & PyTorch - Iteration vs. Fitness", linewidth=4)

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

# Fetch the parameters of the best solution.
best_solution_weights = torchga.model_weights_as_dict(model=model,
                                                      weights_vector=solution)
model.load_state_dict(best_solution_weights)
predictions = model(data_inputs)
print("Predictions : \n", predictions.detach().numpy())

abs_error = loss_function(predictions, data_outputs)
print("Absolute Error : ", abs_error.detach().numpy())

Example 2: XOR Binary Classification

import torch
import torchga
import pygad

def fitness_func(ga_instance, solution, sol_idx):
    global data_inputs, data_outputs, torch_ga, model, loss_function

    model_weights_dict = torchga.model_weights_as_dict(model=model,
                                                         weights_vector=solution)

    # Use the current solution as the model parameters.
    model.load_state_dict(model_weights_dict)

    predictions = model(data_inputs)

    solution_fitness = 1.0 / (loss_function(predictions, data_outputs).detach().numpy() + 0.00000001)

    return solution_fitness

def callback_generation(ga_instance):
    print("Generation = {generation}".format(generation=ga_instance.generations_completed))
    print("Fitness    = {fitness}".format(fitness=ga_instance.best_solution()[1]))

# Create the PyTorch model.
input_layer  = torch.nn.Linear(2, 4)
relu_layer = torch.nn.ReLU()
dense_layer = torch.nn.Linear(4, 2)
output_layer = torch.nn.Softmax(1)

model = torch.nn.Sequential(input_layer,
                            relu_layer,
                            dense_layer,
                            output_layer)
# print(model)

# Create an instance of the pygad.torchga.TorchGA class to build the initial population.
torch_ga = torchga.TorchGA(model=model,
                           num_solutions=10)

loss_function = torch.nn.BCELoss()

# XOR problem inputs
data_inputs = torch.tensor([[0.0, 0.0],
                            [0.0, 1.0],
                            [1.0, 0.0],
                            [1.0, 1.0]])

# XOR problem outputs
data_outputs = torch.tensor([[1.0, 0.0],
                             [0.0, 1.0],
                             [0.0, 1.0],
                             [1.0, 0.0]])

# Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/pygad.html#pygad-ga-class
num_generations = 250 # Number of generations.
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
initial_population = torch_ga.population_weights # Initial population of network weights.

# Create an instance of the pygad.GA class
ga_instance = pygad.GA(num_generations=num_generations, 
                       num_parents_mating=num_parents_mating, 
                       initial_population=initial_population,
                       fitness_func=fitness_func,
                       on_generation=callback_generation)

# Start the genetic algorithm evolution.
ga_instance.run()

# After the generations complete, some plots are showed that summarize how the outputs/fitness values evolve over generations.
ga_instance.plot_fitness(title="PyGAD & PyTorch - Iteration vs. Fitness", linewidth=4)

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

# Fetch the parameters of the best solution.
best_solution_weights = torchga.model_weights_as_dict(model=model,
                                                      weights_vector=solution)
model.load_state_dict(best_solution_weights)
predictions = model(data_inputs)
print("Predictions : \n", predictions.detach().numpy())

# Calculate the binary crossentropy for the trained model.
print("Binary Crossentropy : ", loss_function(predictions, data_outputs).detach().numpy())

# Calculate the classification accuracy of the trained model.
a = torch.max(predictions, axis=1)
b = torch.max(data_outputs, axis=1)
accuracy = torch.sum(a.indices == b.indices) / len(data_outputs)
print("Accuracy : ", accuracy.detach().numpy())

For More Information

There are different resources that can be used to get started with the building CNN and its Python implementation.

Tutorial: Implementing Genetic Algorithm in Python

To start with coding the genetic algorithm, you can check the tutorial titled Genetic Algorithm Implementation in Python available at these links:

This tutorial is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.

Genetic Algorithm Implementation in Python

Tutorial: Introduction to Genetic Algorithm

Get started with the genetic algorithm by reading the tutorial titled Introduction to Optimization with Genetic Algorithm which is available at these links:

Introduction to Genetic Algorithm

Tutorial: Build Neural Networks in Python

Read about building neural networks in Python through the tutorial titled Artificial Neural Network Implementation using NumPy and Classification of the Fruits360 Image Dataset available at these links:

Building Neural Networks Python

Tutorial: Optimize Neural Networks with Genetic Algorithm

Read about training neural networks using the genetic algorithm through the tutorial titled Artificial Neural Networks Optimization using Genetic Algorithm with Python available at these links:

Training Neural Networks using Genetic Algorithm Python

Tutorial: Building CNN in Python

To start with coding the genetic algorithm, you can check the tutorial titled Building Convolutional Neural Network using NumPy from Scratch available at these links:

This tutorial) is prepared based on a previous version of the project but it still a good resource to start with coding CNNs.

Building CNN in Python

Tutorial: Derivation of CNN from FCNN

Get started with the genetic algorithm by reading the tutorial titled Derivation of Convolutional Neural Network from Fully Connected Network Step-By-Step which is available at these links:

Derivation of CNN from FCNN

Book: Practical Computer Vision Applications Using Deep Learning with CNNs

You can also check my book cited as Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7 which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.

Find the book at these links:

Fig04

Citing PyGAD - Bibtex Formatted Citation

If you used PyGAD, please consider adding a citation to the following paper about PyGAD:

@misc{gad2021pygad,
      title={PyGAD: An Intuitive Genetic Algorithm Python Library}, 
      author={Ahmed Fawzy Gad},
      year={2021},
      eprint={2106.06158},
      archivePrefix={arXiv},
      primaryClass={cs.NE}
}

Contact Us

torchga's People

Contributors

ahmedfgad avatar rasmuspjohansson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

torchga's Issues

pygad for mnist

hi @ahmedfgad assalamualaikum

thank you for sharing this work.
i tried to test pygad torch_ga for MNIST datasets.
but the results is not good. THe accuracy is very poor
i have tried to changed the hyperparameters, but the result is not improve significant.
do you have any opinion for my code?

import torch
import pygad.torchga
import pygad
import numpy
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import os

num_epochs = 5
batch_size = 100
learning_rate = 0.001

train_dataset = dsets.MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)

test_dataset = dsets.MNIST(root='./data/',
train=False,
transform=transforms.ToTensor())

train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
"""
class CNN(nn.Module):
def init(self):
super(CNN, self).init()

    self.layer1 = nn.Sequential(
        nn.Conv2d(1, 16, kernel_size=5, padding=2), #1 * 28 * 28 -> 16 * 28 * 28
        nn.BatchNorm2d(16), #16 * 28 * 28
        nn.ReLU(),                 #16 * 28 * 28
        nn.MaxPool2d(2))       # 16 * 14 * 14
    self.layer2  = nn.Sequential(
        nn.Conv2d(16, 32, kernel_size=5, padding=2), # 16 * 14 * 14 -> 32 * 14 * 14
        nn.BatchNorm2d(32),
        nn.ReLU(),
        nn.MaxPool2d(2))       # 32 * 7 * 7
    self.fc = nn.Linear(32 * 7 * 7, 10)
    
def forward(self, x):
    out = self.layer1(x)
    out = self.layer2(out)
    out = out.view(out.size(0), -1)
    out = self.fc(out)
    return out

"""

def fitness_func(solution, sol_idx):
global images_train, labels_train, loss_function
prediction =pygad.torchga.predict(model=model,
solution=solution,
data=images_train)
solution_fitness = 1.0/(loss_function(prediction,labels_train).detach().numpy()+ 0.00000001)

return solution_fitness

def callback_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))

input_layer = torch.nn.Conv2d(in_channels=1, out_channels = 16, kernel_size = 5, padding = 2)
batch1 = torch.nn.BatchNorm2d(16)
relu_layer1 = torch.nn.ReLU()
max_pool1 = torch.nn.MaxPool2d(2)
conv_layer2 = torch.nn.Conv2d(in_channels=16, out_channels = 32, kernel_size = 5, padding = 2)
batch2 = torch.nn.BatchNorm2d(32)
relu_layer2 = torch.nn.ReLU()
max_pool2 = torch.nn.MaxPool2d(2)
flatten_layer2 = torch.nn.Flatten()
dense_layer2 = torch.nn.Linear(in_features=3277, out_features=10)

model = torch.nn.Sequential(input_layer,
batch1,
relu_layer1,
max_pool1,
conv_layer2,
batch2,
relu_layer2,
max_pool2,
flatten_layer2,
dense_layer2)
torch_ga = pygad.torchga.TorchGA(model=model, num_solutions=10)
loss_function = torch.nn.CrossEntropyLoss()

for i, (images, labels) in enumerate(train_loader):
images_train = Variable(images)
labels_train = Variable(labels)

"""
for images, labels in enumerate (test_loader):
images_test = Variable(images)
labels_test = Variable(labels)
"""
num_generations = 200
num_parents_mating = 5
initial_population = torch_ga.population_weights

ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
initial_population=initial_population,
fitness_func=fitness_func,
on_generation=callback_generation)

ga_instance.run()

ga_instance.plot_fitness(title="PyGAD & PyTorch - Iteration vs. Fitness", linewidth=4)

Returning the details of the best solution.

solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

prediction = pygad.torchga.predict(model=model,
solution=solution,
data=images_train)

print("Predictions : \n", predictions)

Calculate the crossentropy for the trained model.

print("Crossentropy : ", loss_function(prediction, labels_train).detach().numpy())

Calculate the classification accuracy for the trained model.

accuracy = torch.true_divide(torch.sum(torch.max(prediction, axis=1).indices == labels_train), len(labels_train))
print("Accuracy : ", accuracy.detach().numpy())

how to train physics-informed neural network with TorchGA?

Hi, thank you for your nice repository! I am trying recently to use TorchGA for training physics-informed neural network but failed. My
code is as follows.
`

#!pip install pygad

#!pip install pyDOE

import torch

import numpy as np
import pygad.torchga
import pygad

import time
import scipy.io
import math

from pyDOE import lhs

CUDA support

if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')

"""# neural network"""

the deep neural network

import torch

import torch.nn as nn
from collections import OrderedDict

class FNN(torch.nn.Module):
def init(self, layers):
super(FNN, self).init()
# parameters
self.depth = len(layers) - 1
self.activation = torch.nn.Tanh
layer_list = list()
for i in range(self.depth - 1):
layer_list.append(
('layer_%d' % i, torch.nn.Linear(layers[i], layers[i+1]))
)
layer_list.append(('activation_%d' % i, self.activation()))
layer_list.append(
('layer_%d' % (self.depth - 1), torch.nn.Linear(layers[-2], layers[-1]))
)
layerDict = OrderedDict(layer_list)
# deploy layers
self.layers = torch.nn.Sequential(layerDict)

def forward(self, x):
out = self.layers(x)
return out

"""# Res_func"""

predictions = pygad.torchga.predict(model=model,

solution=solution,

data=data_inputs)

def net_f(model, inputs, solution):

u = model(inputs)
x = inputs[:,0:1]
t = inputs[:,1:2]

u_x = torch.autograd.grad(
u, x,
grad_outputs=torch.ones_like(u),
retain_graph=True,
create_graph=True,
allow_unused=True
)[0]

u_xx = torch.autograd.grad(
u_x, x,
grad_outputs=torch.ones_like(u),
retain_graph=True,
create_graph=True,
allow_unused=True
)[0]

u_t = torch.autograd.grad(
u, t,
grad_outputs=torch.ones_like(u),
retain_graph=True,
create_graph=True,
allow_unused=True
)[0]

f_u = u_t + u*u_x - (0.01/torch.tensor(math.pi))*u_xx

return f_u

"""# forword pass"""

Forward pass for stream-pressure formulation

def net_u(model, inputs):

u = model(inputs)

return u

def net_u_x(model, inputs):

u = model(inputs)
x = inputs[:,0:1]
t = inputs[:,1:2]

u_x = torch.autograd.grad(
u, x,
grad_outputs=torch.ones_like(u),
retain_graph=True,
create_graph=True,
allow_unused=True
)[0]

return u, u_x

"""# loss_func"""

def loss_fn(model, input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, solution):

f_u_pred = net_f(model, input_f, solution)
u0_pred = net_u(model, input_xt_0, solution)
u_lb_pred, _ = net_u_x(model, input_xt_lb, solution)
u_ub_pred, _ = net_u_x(model, input_xt_ub, solution)

mse_0_u = torch.mean(torch.square(1.0*(ouptput_u_0 - u0_pred)))

mse_b_u = torch.mean(torch.square(u_lb_pred - 0)) +
torch.mean(torch.square(u_ub_pred - 0)) #since ub/lb is 0

mse_f_u = torch.mean(torch.square(1.0*f_u_pred))

Total loss

loss_bic = mse_0_u + mse_b_u
loss_res = mse_f_u
loss = loss_bic + loss_res

return loss

构建适应度函数,类似于损失函数

def fitness_func(solution, sol_idx):

global input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, model

# 这里的预测是否可以换成model直接前向传播????
# ========================================================
loss_total = loss_fn(model, input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, solution)

loss_total_numpy = loss_total.detach().numpy() + 0.00000001

solution_fitness = 1.0 / loss_total_numpy

return solution_fitness

构建打印函数

def callback_generation(ga_instance):
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution()[1]))

layers = [2] + [20]*5 + [1]

model = FNN(layers)

print(model)

print(model.state_dict)

lb = np.array([-1.0]) #x upper boundary
ub = np.array([1.0]) #x lower boundary

N0 = 100
N_b = 25 #25 per upper and lower boundary, so 50 total
N_f = 10000

#load data, from Raissi et. al

data = scipy.io.loadmat('burgers_shock.mat')

t = data['t'].flatten()[:,None]
x = data['x'].flatten()[:,None]
Exact = data['usol']
Exact_u = np.real(Exact)

#grab random points off the initial condition
idx_x = np.random.choice(x.shape[0], N0, replace=False)

x0 = x[idx_x,:]
u0 = Exact_u[idx_x,0:1]

idx_t = np.random.choice(t.shape[0], N_b, replace=False)
tb = t[idx_t,:]

=========== 残差采样点 =====================

X_f = lb + (ub-lb)*lhs(2, N_f)

时间不能为负数:细节

x_f = X_f[:,0:1]
t_f = np.abs(X_f[:,1:2])

#generate point vectors for training
X0 = np.concatenate((x0, 0x0), 1) # (x0, 0)
X_lb = np.concatenate((0
tb + lb[0], tb), 1) # (lb[0], tb)
X_ub = np.concatenate((0*tb + ub[0], tb), 1) # (ub[0], tb)

#seperate point vectors
x0 = X0[:,0:1]
t0 = X0[:,1:2]

x_lb = X_lb[:,0:1]
t_lb = X_lb[:,1:2]

x_ub = X_ub[:,0:1]
t_ub = X_ub[:,1:2]

x_f_tf = torch.tensor(x_f, requires_grad=True).float()
t_f_tf = torch.tensor(t_f, requires_grad=True).float()

x_0_tf = torch.tensor(x0).float()
t_0_tf = torch.tensor(t0).float()
u_0_tf = torch.tensor(u0).float()

x_lb_tf = torch.tensor(x_lb, requires_grad=True).float()
t_lb_tf = torch.tensor(t_lb, requires_grad=True).float()

x_ub_tf = torch.tensor(x_ub, requires_grad=True).float()
t_ub_tf = torch.tensor(t_ub, requires_grad=True).float()

input_f = torch.cat([x_f_tf, t_f_tf], 1)

input_xt_0 = torch.cat([x_0_tf, t_0_tf], 1)
ouptput_u_0 = u_0_tf

input_xt_lb = torch.cat([x_lb_tf, t_lb_tf], 1)
input_xt_ub = torch.cat([x_ub_tf, t_ub_tf], 1)

Create an instance of the pygad.torchga.TorchGA class to build the initial population.

torch_ga = pygad.torchga.TorchGA(model=model,
num_solutions=10)

Prepare the PyGAD parameters. Check the documentation for more information: https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#pygad-ga-class

num_generations = 250 # Number of generations.
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
initial_population = torch_ga.population_weights # Initial population of network weights

ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
initial_population=initial_population,
fitness_func=fitness_func,
on_generation=callback_generation)

ga_instance.run()

`

then i get an error like this, but i have no idea how to fix it. Could you please give some advice about it?Thank you in advance
`
TypeError Traceback (most recent call last)
in ()
----> 1 ga_instance.run()
2

5 frames
/usr/local/lib/python3.7/dist-packages/pygad/pygad.py in run(self)
1190
1191 # Measuring the fitness of each chromosome in the population. Save the fitness in the last_generation_fitness attribute.
-> 1192 self.last_generation_fitness = self.cal_pop_fitness()
1193
1194 best_solution, best_solution_fitness, best_match_idx = self.best_solution(pop_fitness=self.last_generation_fitness)

/usr/local/lib/python3.7/dist-packages/pygad/pygad.py in cal_pop_fitness(self)
1157 fitness = self.previous_generation_fitness[parent_idx]
1158 else:
-> 1159 fitness = self.fitness_func(sol, sol_idx)
1160 if type(fitness) in GA.supported_int_float_types:
1161 pass

in fitness_func(solution, sol_idx)
8 # 这里的预测是否可以换成model直接前向传播????
9 # ========================================================
---> 10 loss_total = loss_fn(model, input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, solution)
11
12 loss_total_numpy = loss_total.detach().numpy() + 0.00000001

in loss_fn(model, input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, solution)
2 def loss_fn(model, input_f, input_xt_0, ouptput_u_0, input_xt_lb, input_xt_ub, solution):
3
----> 4 f_u_pred = net_f(model, input_f, solution)
5 u0_pred = net_u(model, input_xt_0, solution)
6 u_lb_pred, _ = net_u_x(model, input_xt_lb, solution)

in net_f(model, inputs, solution)
25 retain_graph=True,
26 create_graph=True,
---> 27 allow_unused=True
28 )[0]
29

/usr/local/lib/python3.7/dist-packages/torch/autograd/init.py in grad(outputs, inputs, grad_outputs, retain_graph, create_graph, only_inputs, allow_unused, is_grads_batched)
236 for your use case. Defaults to False.
237 """
--> 238 outputs = (outputs,) if isinstance(outputs, torch.Tensor) else tuple(outputs)
239 inputs = (inputs,) if isinstance(inputs, torch.Tensor) else tuple(inputs)
240 overridable_args = outputs + inputs

TypeError: 'NoneType' object is not iterable
`

Wrong results when when parallel processing is used.

PyTorch gives wrong results when used with parallel processing. This is similar to this issue: ahmedfgad/GeneticAlgorithmPython#145

import torch
import numpy
import concurrent.futures
import copy

numpy.random.seed(1)

def create_rand_weights(model, num_models):
    random_model_weights = []
    for model_idx in range(num_models):
        weights_dict = model.state_dict()
        for key in weights_dict:

            w_matrix = weights_dict[key].cpu().detach().numpy()
            layer_weights_shape = w_matrix.shape
    
            if len(layer_weights_shape) > 1:
                layer_weights = numpy.random.rand(layer_weights_shape[0], layer_weights_shape[1])
            else:
                layer_weights = numpy.random.rand(layer_weights_shape[0])
            weights_dict[key] = torch.from_numpy(layer_weights)
        random_model_weights.append(weights_dict)
    
    return random_model_weights

def model_error(model_weights):
    global data_inputs, data_outputs, model

    # _model = copy.deepcopy(model)
    # _model.load_state_dict(model_weights)

    # predictions = _model(data_inputs)

    model.load_state_dict(model_weights)

    predictions = model(data_inputs)

    abs_error = loss_function(predictions, data_outputs).detach().numpy() + 0.00000001

    return abs_error

input_layer = torch.nn.Linear(3, 2)
relu_layer = torch.nn.ReLU()
output_layer = torch.nn.Linear(2, 1)

model = torch.nn.Sequential(input_layer,
                            relu_layer,
                            output_layer)

loss_function = torch.nn.L1Loss()

data_inputs = torch.tensor([[0.02, 0.1, 0.15],
                           [0.7, 0.6, 0.8],
                           [1.5, 1.2, 1.7],
                           [3.2, 2.9, 3.1]])    
data_outputs = torch.tensor([[0.1],
                            [0.6],
                            [1.3],
                            [2.5]])

num_models = 10
random_model_weights = create_rand_weights(model, num_models)

ExecutorClass = concurrent.futures.ThreadPoolExecutor
thread_output = []
with ExecutorClass(max_workers=2) as executor:
    output = executor.map(model_error, random_model_weights)
for out in output:
    thread_output.append(out)
thread_output=numpy.array(thread_output)
print("Wrong Outputs using Threads")
print(thread_output)

print("\n\n")

correct_output = []
for idx in range(num_models):
    error = model_error(random_model_weights[idx])
    correct_output.append(error)
correct_output=numpy.array(correct_output)
print("Correct Outputs without Threads")
print(correct_output)

print(correct_output - thread_output)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.