Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed for cuda implementation #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/adaptive/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import click
import neat

# import torch
import torch
import numpy as np

from pytorch_neat import t_maze
Expand All @@ -29,6 +29,7 @@

batch_size = 4
DEBUG = True
DEVICE = "cuda:0"


def make_net(genome, config, _batch_size):
Expand All @@ -43,7 +44,7 @@ def make_net(genome, config, _batch_size):
batch_size=batch_size,
activation=tanh_activation,
output_activation=tanh_activation,
device="cpu",
device=DEVICE,
)


Expand All @@ -52,13 +53,13 @@ def activate_net(net, states, debug=False, step_num=0):
print("\n" + "=" * 20 + " DEBUG " + "=" * 20)
print(net.delta_w_node)
print("W init: ", net.input_to_output[0])
outputs = net.activate(states).numpy()
outputs = net.activate(states).numpy() if DEVICE == "cpu" else net.activate(states)
if debug and (step_num - 1) % 100 == 0:
print("\nStep {}".format(step_num - 1))
print("Outputs: ", outputs[0])
print("Delta W: ", net.delta_w[0])
print("W: ", net.input_to_output[0])
return np.argmax(outputs, axis=1)
return np.argmax(outputs, axis=1) if DEVICE == "cpu" else torch.argmax(outputs, dim=1)


@click.command()
Expand Down
6 changes: 4 additions & 2 deletions pytorch_neat/adaptive_linear_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def activate(self, inputs):
inputs, dtype=torch.float32, device=self.device
).unsqueeze(2)

if device == "cuda:0" : self.input_to_output = self.input_to_output.to(device) #additional for CUDA

outputs = self.activation(self.input_to_output.matmul(inputs))

input_activs = inputs.transpose(1, 2).expand(
Expand All @@ -128,8 +130,8 @@ def activate(self, inputs):
)

self.delta_w = delta_w

self.input_to_output[self.w_expressed] += delta_w[self.w_expressed]
self.input_to_output[self.w_expressed] += delta_w[self.w_expressed].to(device)
clamp_weights_(
self.input_to_output, weight_threshold=0.0, weight_max=self.weight_max
)
Expand Down
18 changes: 16 additions & 2 deletions pytorch_neat/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@


def sum_aggregation(inputs):
return sum(inputs)
validatedInputs = []
try:
for tens in inputs:
tens.to("cuda:0")
validatedInputs.append(tens)
except Exception as e:
print(f"The following exeption occured: {str(e)}")
return sum(validatedInputs)


def prod_aggregation(inputs):
return reduce(mul, inputs, 1)
validatedInputs = []
try:
for tens in inputs:
tens.to("cuda:0")
validatedInputs.append(tens)
except Exception as e:
print(f"The following exeption occured: {str(e)}")
return reduce(mul, validatedInputs, 1)


str_to_aggregation = {
Expand Down