1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103

use std::time::{Duration};

use errors::ErrorKind::{InvalidLatestMSE, InvalidRecentMSE};
use errors::Result;
use traits::{LearnRate, LearnMomentum};

/// Cirterias after which the learning process holds.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Criterion {
	/// Stop after the given duration of time.
	TimeOut(Duration),

	/// Stop after the given amount of learning iterations.
	Iterations(u64),

	/// Stop when the latest mean square error drops below the given value.
	LatestMSE(f64),

	/// Stop as soon as the recent mean squared error
	/// drops below the given value.
	RecentMSE(f64),
}

impl Criterion {
	/// Checks if this criterion is valid.
	pub fn check_validity(&self) -> Result<()> {
		use self::Criterion::*;
		match *self {
			TimeOut(_) => Ok(()),
			Iterations(_) => Ok(()),
			LatestMSE(mse) => {
				if mse > 0.0 && mse < 1.0 {
					Ok(())
				} else {
					Err(InvalidLatestMSE)
				}
			}
			RecentMSE(recent) => {
				if recent > 0.0 && recent < 1.0 {
					Ok(())
				} else {
					Err(InvalidRecentMSE)
				}
			}
		}
	}
}

/// Learning rate configuration.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LearnRateConfig {
	/// Automatically adapt learn rate during learning.
	Adapt,

	/// Use the given fixed learn rate.
	Fixed(LearnRate),
}

/// Learning momentum configuration.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LearnMomentumConfig {
	/// Automatically adapt learn momentum during learning.
	Adapt,

	/// Use the given fixed learn momentum.
	Fixed(LearnMomentum),
}

/// Logging interval for logging stats during the learning process.
/// 
/// Default logging configuration is to never log anything.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LogConfig {
	/// Never log anything.
	Never,

	/// Log in intervals based on the given duration.
	TimeSteps(Duration),

	/// Log every given number of training iterations.
	Iterations(u64)
}

impl Default for LogConfig {
	fn default() -> Self {
		LogConfig::Never
	}
}

/// Sample scheduling strategy while learning.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Scheduling {
	/// Pick samples randomly.
	///
	/// This usually is a good approach to defeat sample-pattern learning.
	Random,

	/// Pick samples in order.
	///
	/// This maybe useful for testing purposes.
	Iterative,
}