Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Created October 17, 2020 20:35
Show Gist options
  • Save MCarlomagno/9fe408389554dea36019947834706008 to your computer and use it in GitHub Desktop.
Save MCarlomagno/9fe408389554dea36019947834706008 to your computer and use it in GitHub Desktop.
var underscore = require('underscore');
var replay = async function (batchSize, index) {
var minibatch = [];
if (index === subzeroIndex) {
minibatch = underscore.sample(subzeroMemory, batchSize);
} else {
minibatch = underscore.sample(kanoMemory, batchSize);
}
for (var i = 0; i < minibatch.length; i++) {
// deconstructs secuence
var { state, action, reward, nextState, done } = minibatch[i];
// predicts action
var tensorState = stateToTensor(state, index);
target = model.predict(tensorState);
if (done) {
target.dataSync()[action] = reward;
} else {
var tensorNextState = stateToTensor(nextState, index);
t = targetModel.predict(tensorNextState).dataSync();
target.dataSync()[action] = reward + gamma * Math.max(...t);
}
await model.fit(tensorState, target);
}
if (epsilon > epsilonMin) {
epsilon *= epsilonDecay;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment