pub struct QNetwork<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt = Sgd> { /* private fields */ }Expand description
Q values from a neural network: a deep Q-network (tinymind (C++)’s
QValueNeuralNetworkPolicy), for QLearner::with_q_values.
The online network is a MultilayerPerceptron with IN inputs, HID
hidden neurons and one output per action. inputs encodes a state as the
network’s input vector (C++ getInputValues; a one-hot vector for a small
set of states). Reading a Q value runs the online network forward without
changing it. Storing one feeds the state forward, sets the taken action’s
output to the new value, keeps the others as they are, and trains once,
unless the value is already there. Every target_update_interval training
steps the online weights are copied into the target network, which the
learner’s lookahead reads (QValues::future_q_value); it starts as a
copy of the online network.
use tinymind::qlearn::{QNetwork, QValues};
use tinymind::{Linear, MultilayerPerceptron, Q16_16, Tanh};
// Three states, one-hot encoded; two actions; four hidden neurons.
fn one_hot(state: usize) -> [Q16_16; 3] {
core::array::from_fn(|i| Q16_16::from_int(i32::from(i == state)))
}
let mut seed = 1;
let net = MultilayerPerceptron::<Q16_16, Tanh, Linear, 3, 4, 2>::new(|| {
seed = (seed * 7 + 3) % 17;
Q16_16::from_raw((seed - 8) * 4096)
});
let mut q = QNetwork::new(net, one_hot, 10);
let target = Q16_16::from_int(1) / Q16_16::from_int(2);
let before = q.q_value(1, 0);
q.set_q_value(1, 0, target);
// One training step moves Q(1, 0) towards the target.
let after = q.q_value(1, 0);
assert!((after - target).raw().abs() < (before - target).raw().abs());Implementations§
Source§impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
Sourcepub fn new(
network: MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>,
inputs: fn(usize) -> [V; IN],
target_update_interval: usize,
) -> Self
pub fn new( network: MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>, inputs: fn(usize) -> [V; IN], target_update_interval: usize, ) -> Self
Q values from network, with inputs encoding each state as its
input vector, and the target network refreshed every
target_update_interval training steps (0 refreshes it after every
step, as 1 does, like the C++ >= test).
Sourcepub fn network(&self) -> &MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>
pub fn network(&self) -> &MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>
The online network.
Sourcepub fn network_mut(
&mut self,
) -> &mut MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>
pub fn network_mut( &mut self, ) -> &mut MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>
The online network, to change its rates or weights.
Sourcepub fn target(&self) -> &InferencePerceptron<V, H, O, IN, HID, ACTIONS>
pub fn target(&self) -> &InferencePerceptron<V, H, O, IN, HID, ACTIONS>
The target network: the online weights as of the last refresh.
Trait Implementations§
Source§impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> QValues<V> for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> QValues<V> for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
Source§fn q_value(&self, state: usize, action: usize) -> V
fn q_value(&self, state: usize, action: usize) -> V
The online network’s output for action (0 for an action out of
range).
Source§fn future_q_value(&mut self, state: usize, action: usize) -> V
fn future_q_value(&mut self, state: usize, action: usize) -> V
The target network’s output for action (0 for an action out of
range).
Source§fn set_q_value(&mut self, state: usize, action: usize, value: V)
fn set_q_value(&mut self, state: usize, action: usize, value: V)
Trains the online network once towards value for action, unless
its output already equals it (an action out of range is ignored).