use crate::matrix::Matrix;
use crate::param::Param;
use rand::Rng;
use rand::rngs::StdRng;
const BIAS_PRIOR: [[f32; 4]; 5] = [
[0.0, 0.0, 1.0, 0.0],
[-4.0, -2.0, -1.0, -1.0],
[-4.0, -2.0, -1.0, -1.0],
[4.0, 2.0, 0.0, 2.0],
[8.0, 4.0, 0.0, 1.0],
];
#[derive(Clone, Debug)]
pub struct InteroceptiveCore {
pub w_gain: Param,
pub c_gain: Param,
pub w_bias: Param,
}
#[derive(Clone, Debug)]
pub struct GainCache {
pub gate: Matrix,
}
impl InteroceptiveCore {
pub fn new(hidden: usize, actions: usize, rng: &mut StdRng) -> Self {
assert_eq!(actions, BIAS_PRIOR.len());
let w_gain = (0..hidden * 4).map(|_| rng.gen_range(-0.1..0.1)).collect();
Self {
w_gain: Param::new(Matrix::from_vec(hidden, 4, w_gain)),
c_gain: Param::zeros(1, hidden),
w_bias: Param::new(Matrix::from_vec(actions, 4, BIAS_PRIOR.concat())),
}
}
pub fn modulate(&self, h: &Matrix, body: &Matrix) -> (Matrix, GainCache) {
let mut gate = Matrix::new(h.rows, h.cols);
body.dot_rhs_transposed(&self.w_gain.value, &mut gate);
let mut out = h.clone();
for r in 0..h.rows {
for ((g, o), c) in gate
.row_mut(r)
.iter_mut()
.zip(out.row_mut(r))
.zip(&self.c_gain.value.data)
{
*g = (*g + c).tanh();
*o *= 1.0 + *g;
}
}
(out, GainCache { gate })
}
pub fn modulate_backward(
&mut self,
h: &Matrix,
body: &Matrix,
cache: &GainCache,
grad_out: &Matrix,
) -> Matrix {
let mut grad_h = grad_out.clone();
let mut grad_pre = grad_out.clone();
for i in 0..grad_out.data.len() {
let g = cache.gate.data[i];
grad_h.data[i] *= 1.0 + g;
grad_pre.data[i] *= h.data[i] * (1.0 - g * g);
}
grad_pre.dot_self_transposed_accumulate(body, &mut self.w_gain.grad);
add_column_sums(&grad_pre, &mut self.c_gain);
grad_h
}
pub fn add_bias(&self, body: &Matrix, logits: &mut Matrix) {
let mut bias = Matrix::new(logits.rows, logits.cols);
body.dot_rhs_transposed(&self.w_bias.value, &mut bias);
for (l, b) in logits.data.iter_mut().zip(&bias.data) {
*l += b;
}
}
pub fn add_bias_backward(&mut self, body: &Matrix, grad_logits: &Matrix) {
grad_logits.dot_self_transposed_accumulate(body, &mut self.w_bias.grad);
}
pub fn params_mut(&mut self) -> Vec<&mut Param> {
vec![&mut self.w_gain, &mut self.c_gain, &mut self.w_bias]
}
}
pub fn temperature(tau0: f32, beta: f32, drawdown_velocity: f32) -> f32 {
tau0 * (-beta * drawdown_velocity).exp()
}
pub(crate) fn add_column_sums(m: &Matrix, target: &mut Param) {
for r in 0..m.rows {
for (t, v) in target.grad.data.iter_mut().zip(m.row(r)) {
*t += v;
}
}
}