chipiron.players.boardevaluators.neural_networks.models package

Submodules

chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron module

This module defines the neural network model NetPP2D2_2_PRELU.

The NetPP2D2_2_PRELU class is a subclass of ChiNN and implements the forward pass of the neural network. It consists of two fully connected layers with PReLU activation functions.

chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.__init__()

Initializes the NetPP2D2_2_PRELU model.

forward(x

torch.Tensor) -> torch.Tensor: Performs the forward pass of the neural network.

init_weights(file

str) -> None: Initializes the weights of the model.

chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.print_param() None

Prints the parameters of the model.

print_input(input

torch.Tensor) -> None: Prints the input tensor.

Helper Functions:

print_piece_param(i: int, vec: torch.Tensor) -> None: Prints the parameters of a specific piece.

class chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.MultiLayerPerceptron(args: MultiLayerPerceptronArgs)[source]

Bases: ChiNN

Neural network model for board evaluation using 2D2_2_PRELU architecture.

fc1

The first fully connected layer.

Type:

nn.Linear

relu_1

The first PReLU activation function.

Type:

nn.PReLU

fc2

The second fully connected layer.

Type:

nn.Linear

tanh

The tanh activation function.

Type:

nn.Tanh

forward(x: Tensor) Tensor[source]

Forward pass of the neural network.

Parameters:

x (torch.Tensor) – The input tensor.

Returns:

The output tensor.

Return type:

torch.Tensor

init_weights() None[source]

Initialize the weights of the neural network.

log_readable_model_weights_to_file(file_path: str) None[source]

Writes the model weights and biases into a YAML file.

Parameters:

file_path (str) – The path where the YAML file will be saved.

print_input(input: Tensor) None[source]

Print the input tensor.

Parameters:

input (torch.Tensor) – The input tensor.

print_param() None[source]

Prints the model weights and biases to the console in YAML format.

Parameters:

model (nn.Sequential) – The Sequential model.

class chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.MultiLayerPerceptronArgs(type: Literal[<NNModelType.MultiLayerPerceptron: 'multi_layer_perceptron'>], number_neurons_per_layer: list[int], list_of_activation_functions: list[chipiron.players.boardevaluators.neural_networks.NNModelType.ActivationFunctionType])[source]

Bases: object

filename() str[source]
list_of_activation_functions: list[chipiron.players.boardevaluators.neural_networks.NNModelType.ActivationFunctionType]
number_neurons_per_layer: list[int]
type: MultiLayerPerceptron: 'multi_layer_perceptron'>]
chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.build_sequential(layers: list[int], activations: list[chipiron.players.boardevaluators.neural_networks.NNModelType.ActivationFunctionType]) Sequential[source]
chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.extract_sequential_model_data(model: Sequential) Dict[str, Any][source]

Extracts the weights and biases from a Sequential model in a layered dictionary format.

Parameters:

model (nn.Sequential) – The Sequential model to extract from.

Returns:

A dictionary with layer names as keys and parameter data as values.

Return type:

Dict[str, Any]

chipiron.players.boardevaluators.neural_networks.models.multi_layer_perceptron.print_piece_param(i: int, vec: Tensor) None[source]

Prints the piece parameter at index i from the given tensor.

Parameters:
  • i (int) – The index of the piece parameter to print.

  • vec (torch.Tensor) – The tensor containing the piece parameters.

Returns:

None

chipiron.players.boardevaluators.neural_networks.models.transformer_one module

class chipiron.players.boardevaluators.neural_networks.models.transformer_one.Block(n_embd: int, n_head: int, dropout_ratio: float)[source]

Bases: Module

Transformer block: communication followed by computation

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class chipiron.players.boardevaluators.neural_networks.models.transformer_one.FeedFoward(n_embd: int, dropout_ratio: float)[source]

Bases: Module

a simple linear layer followed by a non-linearity

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class chipiron.players.boardevaluators.neural_networks.models.transformer_one.Head(n_embd: int, head_size: int, dropout_ratio: float)[source]

Bases: Module

one head of self-attention

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class chipiron.players.boardevaluators.neural_networks.models.transformer_one.MultiHeadAttention(num_heads: int, head_size: int, dropout_ratio: float, n_embd: int)[source]

Bases: Module

multiple heads of self-attention in parallel

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class chipiron.players.boardevaluators.neural_networks.models.transformer_one.TransformerArgs(number_occupancy_types: int = 13, len_square_tensor: int = 832, number_pieces_types: int = 6, type: Literal[<NNModelType.Transformer: 'transformer'>] = <NNModelType.Transformer: 'transformer'>, n_embd: int = 27, n_head: int = 3, n_layer: int = 2, dropout_ratio: float = 0.0)[source]

Bases: object

dropout_ratio: float = 0.0
filename() str[source]

Generates a filename based on the TransformerArgs instance.

len_square_tensor: int = 832
n_embd: int = 27
n_head: int = 3
n_layer: int = 2
number_occupancy_types: int = 13
number_pieces_types: int = 6
type: Transformer: 'transformer'>] = 'transformer'
class chipiron.players.boardevaluators.neural_networks.models.transformer_one.TransformerOne(args: TransformerArgs)[source]

Bases: ChiNN

compute_representation(node: Any, parent_node: Any, board_modifications: Any) None[source]

Compute the input representation for the given node.

Parameters:
  • node (Any) – Current node.

  • parent_node (Any) – Parent node.

  • board_modifications (Any) – Board modifications.

forward(indices: list[list[int]]) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_nn_input(node: Any) None[source]

Get the input tensor for the given node.

Parameters:

node (Any) – Current node.

Returns:

None

print_param() None[source]

Print the parameters of the model.

Module contents