Adapted from https://github.com/MarcoVernaUSI/distributed/blob/Refactored/network.py
© 2019-2020 Marco Verna – 2020 Giorgia Adorni
networks.distributed_network.
DistributedNet
(input_size)¶Simplified “distributed network”, that takes as input an array containing the
response values of the sensors – which can be either prox_values
, prox_comm
or all_sensors
– and the output is an array of 1 float that represents the speed of the wheels,
which is assumed to be the same both right and left.
The architecture of the network is straightforward: there are three linear layers each of size (inpuz_size, 10), (10, 10) and (10, 1) respectively, where input_size is the sum of the shape of the sensing – 7 or 14. The Tanh non-linear activation function is applied to the first and the second layer.
input_size – dimension of the sensing vector
controller
(thymio) → Callable[[Sequence[Sensing]], Tuple[Sequence[Control], Sequence[Communication]]]¶thymio – unused
controller function
forward
(xs, _)¶xs – input sensing batch
_ – mask of the batch
speed
Adapted from https://github.com/MarcoVernaUSI/distributed/blob/Refactored/com_network.py
© 2019-2020 Marco Verna – 2020 Giorgia Adorni
networks.communication_network.
CommunicationNet
(input_size, device, sync: networks.communication_network.Sync = <Sync.sequential: 2>, module: torch.nn.modules.module.Module = <class 'networks.communication_network.SingleNet'>, input_fn=<function input_from>)¶High-level module that handle the sensing of the agents.
input_size – dimension of the sensing vector (can be 7 or 14)
device – device used (cpu or gpu)
sync – kind of synchronisation
module – SingleNet
input_fn – input function
self.tmp_indices – communication indices
self.distribution – distribution used for the initialisation of the communication
controller
(thymio, sync: networks.communication_network.Sync = <Sync.sync: 3>) → Callable[[Sequence[Sensing]], Tuple[Sequence[Control], Sequence[Communication]]]¶thymio – number of agents in the simulation
sync – kind of synchronisation
controller function
forward
(batch, batch_size)¶batch – batch elements
batch_size – batch mask
network output
step
(xs, comm, sync: networks.communication_network.Sync, sim=False, i=None)¶Danger
Sync of types ‘random’, ‘sequential’ and ‘random_sequential’ are actually not working in simulation.
xs – input sensing of a certain timestep (shape: 1 x N x sensing)
comm – communication vector (shape: 1 X N)
sync – kind of synchronisation
sim – boolean true if step executed in simulation
i – index of the thymio in the row
speed of the agent
networks.communication_network.
CommunicationNetNoSensing
(input_size, device, sync: networks.communication_network.Sync = <Sync.sequential: 2>, module: torch.nn.modules.module.Module = <class 'networks.communication_network.SingleNetNoSensing'>, input_fn=<function input_from_no_sensing>)¶High-level module that handle the sensing of the agents.
input_size – dimension of the sensing vector (can be 7 or 14)
device – device used (cpu or gpu)
sync – kind of synchronisation
module – SingleNetNoSensing
input_fn – input function
self.tmp_indices – communication indices
self.distribution – distribution used for the initialisation of the communication
networks.communication_network.
SingleNet
(input_size)¶Low-level module that works on the sensing and the communication received by a single agent (in a certain timestep), producing as output the control and the communication to transmit.
input_size – dimension of the sensing vector
forward
(in_put)¶in_put – input of the network, vector containing the sensing and the messages received by the robot (can be multidimensional, that means a row for each robot)
output of the network containing the control and the message to communicate (shape: 1 x 2)
networks.communication_network.
SingleNetNoSensing
(_)¶Low-level module that works on the communication received by a single agent (in a certain timestep), producing as output the communication to transmit and the probability of a certain colour.
_ – dimension of the sensing vector
forward
(in_put)¶in_put – input of the network, vector containing the messages received by the robot (can be multidimensional, that means a row for each robot)
output of the network containing the message to communicate and the probability of a certain colour (shape: 1 x 2)
networks.communication_network.
Sync
(value)¶Class used to define the synchronisation approach of the communication updates, that can be random, sequential, synchronous and random_sequential
networks.communication_network.
init_comm
(thymio: int, distribution)¶Initialise the communication vector (for the initial timestep of each sequence).
thymio – number of agents
distribution – distribution used for the initialisation
communication vector
networks.communication_network.
input_from
(ss, comm, i, sim=False)¶Parse the sensing and the communication and prepare it to be the input on the single net.
ss – sensing
comm – communication vector
i – index of the thymio in the row
sim – boolean True if executed in simulation, False otherwise
parsed input containing the sensing values and the communication received
networks.communication_network.
input_from_no_sensing
(_, comm, i)¶Prepare the communication vector to be the input on the single net.
comm – communication vector
i – index of the thymio in the row
parsed input containing the communication received
Adapted from https://github.com/GiorgiaAuroraAdorni/learning-relative-interactions-through-imitation
© 2020 Giorgia Adorni and Elia Cereda
networks.metrics.
NetMetrics
(t: tqdm.std.tqdm, metrics_path)¶This class is supposed to create a dataframe that collects, updates and saves to file the metrics of a model.
t – tqdm
metrics_path – file where to save the metrics
finalize
()¶update
(train_loss, valid_loss)¶train_loss – training loss
valid_loss – validation loss
networks.metrics.
StreamingMean
¶Compute the (possibly weighted) mean of a sequence of values in streaming fashion.
This class stores the current mean and current sum of the weights and updates them when a new data point comes in.
This should have better stability than summing all samples and dividing at the end, since here the partial mean is always kept at the same scale as the samples.
mean
¶mean
return self._mean
reset
()¶update
(sample, weight=1.0)¶sample – sample
weight – weight
network_training_distribute.
network_train
(indices, file_losses, runs_dir, model_dir, model, communication, net_input, save_net)¶Split the dataset also defining input and output, using the indices. Generate tensors. Create the neural network and optimizer, and set the device. Train the model and save it.
indices – sample indices
file_losses – file where to save the metrics
runs_dir – directory containing the simulation runs
model_dir – directory containing the network data
model – network
communication – states if the communication is used by the network
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
save_net – states if save or not the network
network_training_distribute.
train_net
(epochs: int, train_dataset: torch.utils.data.dataset.TensorDataset, valid_dataset: torch.utils.data.dataset.TensorDataset, test_dataset: torch.utils.data.dataset.TensorDataset, net: torch.nn.modules.module.Module, metrics_path: AnyStr, device, batch_size: int = 100, learning_rate: float = 0.01, criterion=MSELoss()) → networks.metrics.NetMetrics¶epochs – number of epochs
train_dataset – training dataset
valid_dataset – validation dataset
test_dataset – testing dataset
net – model
metrics_path – file where to save the metrics
device – device used (cpu or cuda)
batch_size – size of the batch (default: 100)
learning_rate – learning rate (default: 0.01)
criterion – loss function (default: Mean Squared Error)
output losses of the datasets
network_training_distribute.
validate_net
(net, device, valid_minibatch, criterion=MSELoss())¶net – model
device – device used (cpu or cuda)
valid_minibatch – validation batch
criterion – loss function
resulting loss of the validation set
network_training_colour.
network_train
(indices, file_losses, runs_dir, model_dir, model, communication, net_input, save_net)¶Split the dataset also defining input and output, using the indices. Generate tensors. Create the neural network and optimizer, and set the device. Train the model and save it.
indices – sample indices
file_losses – file where to save the metrics
runs_dir – directory containing the simulation runs
model_dir – directory containing the network data
model – network
communication – states if the communication is used by the network
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
save_net – states if save or not the network
network_training_colour.
train_net
(epochs: int, train_dataset: torch.utils.data.dataset.TensorDataset, valid_dataset: torch.utils.data.dataset.TensorDataset, test_dataset: torch.utils.data.dataset.TensorDataset, net: torch.nn.modules.module.Module, metrics_path: AnyStr, device, batch_size: int = 100, learning_rate: float = 0.01, criterion=BCELoss()) → networks.metrics.NetMetrics¶epochs – number of epochs
train_dataset – training dataset
valid_dataset – validation dataset
test_dataset – testing dataset
net – model
metrics_path – file where to save the metrics
device – device used (cpu or cuda)
batch_size – size of the batch (default: 100)
learning_rate – learning rate (default: 0.01)
criterion – loss function (default: Mean Squared Error)
output losses of the datasets
network_training_colour.
validate_net
(net, device, valid_minibatch, criterion=MSELoss())¶net – model
device – device used (cpu or cuda)
valid_minibatch – validation batch
criterion – loss function
resulting loss of the validation set
network_evaluation.
controller_plots
(model_dir, ds, ds_eval, groundtruth, prediction, communication)¶model_dir – directory containing the trained model
ds – name of the dataset
ds_eval – name of the dataset for the evaluation (usually the manual one)
groundtruth – evidence
prediction – output control
communication – states if the communication is used by the network
network_evaluation.
evaluate_controller
(model_dir, ds, ds_eval, groundtruth, sensing, net_input, communication, goal, controllers)¶model_dir – directory containing the trained model
ds – name of the dataset
ds_eval – name of the dataset for the evaluation (usually the manual one)
groundtruth – evidence
sensing – used to obtain the prediction
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
communication – states if the communication is used by the network
goal – task to perform (in this case distribute)
controllers – reference to the controller class
network_evaluation.
evaluate_net
(model_img, model, net, net_input, net_title, sensing, index, x_label, goal, communication, controllers)¶model_img – directory for the output image of the model
model – nae of the model
net – model used
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
net_title – network title
sensing – input sensing
index – 1D vector
x_label – label of the x axis
goal – task to perform (in this case distribute)
communication – states if the communication is used by the network
controllers – reference to the controller class
network_evaluation.
generate_sensing
()¶x is the configuration, s is the zeros array
network_evaluation.
network_evaluation
(indices, file_losses, runs_dir, model_dir, model, model_img, ds, ds_eval, communication, net_input, avg_gap=None, task='Task1')¶indices – sample indices
file_losses – file where to save the metrics
runs_dir – directory containing the simulation runs
model_dir – directory containing the network data
model – network
model_img – directory for the output image of the model
ds – name of the dataset
ds_eval – name of the dataset for the evaluation (usually the manual one)
communication – states if the communication is used by the network
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
avg_gap – average distance among the robots
task – task to be performed
network_evaluation.
network_plots
(model_img, dataset, model, net_input, prediction, training_loss, validation_loss, x_train, y_valid, communication, goal)¶model_img – directory for the output image of the model
dataset – name of the dataset
model – netowork
net_input – input of the network (between: prox_values, prox_comm and all_sensors)
prediction – output control of the model
training_loss – error on the train set
validation_loss – error on the validation set
x_train – train input
y_valid – validation output
communication – states if the communication is used by the network