Skip to content

Commit

Permalink
Implement Display for Individual and Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-berg committed Apr 11, 2024
1 parent 9ae7119 commit add680b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl<T: TensorBase> Activation<T> {
pub fn d(&self, tensor: &T) -> T {
(self.derivative)(tensor)
}

/// Returns the name of the activation function.
pub fn name(&self) -> &str {
&self.name
}
}


Expand Down
5 changes: 5 additions & 0 deletions src/cost_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ impl<T: TensorBase> CostFunction<T> {
pub fn d(&self, output: &T, desired_output: &T) -> T {
(self.derivative)(output, desired_output)
}

/// Returns the name of the cost function.
pub fn name(&self) -> &str {
&self.name
}
}


Expand Down
20 changes: 18 additions & 2 deletions src/individual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! The `Individual` is the centerpiece of the evolutionary process.

use std::fmt::Debug;
use std::fmt::{Debug, Display};
use std::fs::read_to_string;
use std::io::Error as IOError;
use std::path::Path;
Expand All @@ -11,7 +11,6 @@ use std::slice::Iter;

use log::trace;
use num_traits::FromPrimitive;
use serde_json;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
Expand Down Expand Up @@ -254,6 +253,23 @@ impl<T: Tensor> Individual<T> {
}


impl<T: Tensor> Display for Individual<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let indent = " ";
let mut layers_display = format!("[\n{indent}");
let sep = format!(",\n{indent}");
layers_display.push_str(
&self.layers.iter()
.map(|layer| layer.to_string())
.collect::<Vec<String>>()
.join(&sep)
);
layers_display.push_str("\n]");
write!(f, "Individual {{ layers: {}, cost_function: {} }}", layers_display, self.cost_function.name())
}
}


/// Makes `Individual<T>` callable by value (i.e. consuming the instance).
///
/// This is mainly implemented because [`FnOnce`] is a supertrait of [`Fn`].
Expand Down
9 changes: 9 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// TODO: Documentation
// https://github.com/mfajnberg/tensorevo/issues/22

use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::activation::Activation;
Expand Down Expand Up @@ -32,6 +34,13 @@ impl<T: Tensor> Layer<T> {
}


impl<T: Tensor> Display for Layer<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Layer {{ size: {}, activation: {} }}", self.size(), self.activation.name())
}
}


impl<T: Tensor> FnOnce<(&T,)> for Layer<T> {
type Output = T;

Expand Down

0 comments on commit add680b

Please sign in to comment.