Skip to main content

QNetwork

Struct QNetwork 

Source
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>
where V: Scalar, H: Activation<V>, O: Activation<V>, Opt: Optimizer<V>,

Source

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).

Source

pub fn network(&self) -> &MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>

The online network.

Source

pub fn network_mut( &mut self, ) -> &mut MultilayerPerceptron<V, H, O, IN, HID, ACTIONS, Opt>

The online network, to change its rates or weights.

Source

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>
where V: Scalar, H: Activation<V>, O: Activation<V>, Opt: Optimizer<V>,

Source§

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

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)

Trains the online network once towards value for action, unless its output already equals it (an action out of range is ignored).

Auto Trait Implementations§

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> Freeze for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: Freeze, Opt: Freeze,

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> RefUnwindSafe for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> Send for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: Send, Opt: Send,

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> Sync for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: Sync, Opt: Sync,

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> Unpin for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: Unpin, Opt: Unpin,

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> UnsafeUnpin for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: UnsafeUnpin, Opt: UnsafeUnpin,

§

impl<V, H, O, const IN: usize, const HID: usize, const ACTIONS: usize, Opt> UnwindSafe for QNetwork<V, H, O, IN, HID, ACTIONS, Opt>
where V: UnwindSafe, Opt: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.