Skip to content

Network

src.ga.network

View source code

DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') module-attribute

NeuralNet

Bases: Module

Small fully-connected neural network for GA players.

last_activations = None instance-attribute

last_input = None instance-attribute

last_output_binary = None instance-attribute

last_output_raw = None instance-attribute

layers = nn.ModuleList() instance-attribute

__init__()

add_layer(size, output_size, activation='relu')

Dynamically add a fully-connected layer.

forward(x: torch.Tensor) -> torch.Tensor

Forward pass.

load_weights(path: str) -> None

Load model weights from file.

mutate(mutation_scale=0.1, mutation_probability=0.1)

Random Gaussian mutation of parameters.

predict(inputs_list)

save_weights(path: str) -> None

Save model weights using PyTorch state_dict.

BatchedPopulationBrain

Stacks individual NeuralNet weights into a batched 3D tensor to evaluate the entire population in one single GPU pass.

B = [] instance-attribute

W = [] instance-attribute

acts = [] instance-attribute

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') instance-attribute

__init__(players)

predict_batch(input_array: np.ndarray) -> np.ndarray

Takes (Pop, Inputs), returns boolean (Pop, Outputs).