Skip to content

v17.0 — Recurrent networks train every weight; DQN reads the right state

Choose a tag to compare

@danmcleran danmcleran released this 10 Nov 01:18
· 44 commits to master since this release
0425b66

v17.0 — Recurrent networks train every weight; DQN reads the right state

This release fixes training in the recurrent networks and in deep Q-learning. Both changed what a trained model learns, so the major version moves: if you trained an LstmNeuralNetwork, GruNeuralNetwork, ElmanNeuralNetwork or a DQN with QValueNeuralNetworkPolicy, retrain it. The int8 layers, the importers and inference-only code are unchanged.

Fixes

  • LSTM and GRU gates never trained (cpp/neuralnet.hpp, #257). The call that turns the hidden layer's error into one delta per gate was dropped in an April refactor, so every gate delta stayed 0: the input, input-bias and recurrent weights never changed, and only the hidden-to-output readout learned. The gates now train. The hidden-to-output bias of LSTM and GRU networks, which was also never trained, now trains too.
  • Recurrent state and gradients (#257). The forward pass wrote each neuron's new output into the recurrent layer while later neurons were still reading it, so they saw a mix of this step's and last step's state, and the recurrent gradients used this step's state instead of last step's. Each layer now reads h_{t-1} consistently and keeps it for the gradients.
  • Fixed-point GRU and LSTM arithmetic (#257). The GRU computed 1 - z with ValueType(1), which for a QValue is the raw value 2^-16, not 1.0; the LSTM gate deltas did the same. The GRU candidate's recurrent weights were also missing the reset gate in their gradient.
  • Resetting state (#257). LstmNeuralNetwork::resetState cleared the cell state but not h_{t-1}, so sequences bled into each other. It now clears both, and Elman and RecurrentNeuralNetwork networks get a resetState.
  • DQN read Q values for the wrong state (cpp/qlearn.hpp, #258). QValueNeuralNetworkPolicy::getQValue(state, action) ignored the state and returned the network's outputs for whichever state it had last seen, so greedy action selection and the Q-learning update both used another state's values. It now evaluates the state it is asked about.
  • Greedy choice with negative Q values (#258). With three or more valid actions, ArgMaxPolicy treated 0 as a floor, so when every Q value was negative it gave up and the learner moved at random. It now picks the largest Q value whatever its sign, and moves at random only when every action is tied, which keeps the tabular maze, whose Q table starts at 0, exploring as before.

How the fixes are verified

  • One training step of an LSTM, a GRU and an Elman network in double precision matches central finite differences to 1e-7 for every weight.
  • Q16.16 and double networks with the same weights produce the same outputs to within 0.01.
  • Six planted defects (each bug above, put back one at a time) each fail at least one of the new tests.
  • New Q-learning tests cover negative and tied Q values and per-state DQN values; three of their checks fail on v16.9.

Examples

  • har_activity: 97.5% → 100% test accuracy.
  • gru_xor: final four-case error 0.088 → 0.061.
  • air_quality: one-step forecast MAE 0.129 → 0.067.
  • elman_vowels: 94.05% → 96.76%, with Q8.8 still matching the float model exactly.
  • elman_temporal_xor: had been at chance (51%) since v16.3's lookup-table fix while its docs still said 98.33%. It now trains at a learning rate of 0.1 (the default failed on 3 of 18 seeds) and scores 100%.
  • dqn_maze: its earlier 100/100 depended on the wrong-state reads; with correct Q values it reached the goal in 63 of 100 greedy runs. The state is now one-hot encoded and the output layer is linear (Q values reach 5, beyond tanh's range), and it reaches the goal in 100 of 100, on a shortest path from every room.
  • The LSTM sinusoid unit tests now check one-step-ahead accuracy, which improved, instead of a 20-step free-running forecast, which drifts further in phase now that the gates train.

All of these were found while porting tinymind to Rust, where tinymind-rs's parity tests require both implementations to produce the same model.

Docs

  • Example READMEs, their Pages pages and eight gallery plots carry the new results; the DQN tutorial describes the new state encoding and output layer.

Documentation, architecture guides, and example gallery: https://danmcleran.github.io/tinymind/

Full changelog: v16.9...v17.0