Skip to content

Commit 0e23b75

Browse files
authored
A few fixes and improvements to the cart-pole demo (#120)
1. tf.sigmoidCrossentropyWithLogits is replaces with tf.losses.sigmoidCrossEntropy in the latest release of tensorflow.js (0.12.4) 2. Replace the unicode arrows with ASCII ones. The unicode ones doesn't work properly on some devices. 3. Put the cart-pole canvas on top, closer to the status message.
1 parent 0b96037 commit 0e23b75

File tree

4 files changed

+126
-103
lines changed

4 files changed

+126
-103
lines changed

Diff for: cart-pole/index.html

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
<style>
2626
#app-status {
2727
color: blue;
28+
font-family: monospace;
2829
font-size: 150%;
2930
padding-bottom: 1em;
3031
}
32+
#cart-pole-canvas {
33+
display: none;
34+
}
3135
button {
3236
font-size: 105%;
3337
min-width: 120px;
@@ -74,6 +78,10 @@ <h1>TensorFlow.js Example:<br/>Reinforcement Learning: Cart Pole </h1>
7478
<span id="app-status">Standing by.</span>
7579
</div>
7680

81+
<div>
82+
<canvas id="cart-pole-canvas" height="150px" width="500px"></canvas>
83+
</div>
84+
7785
<div>
7886
<div class="horizontal-sections">
7987
<div class="input-div">
@@ -136,10 +144,6 @@ <h1>TensorFlow.js Example:<br/>Reinforcement Learning: Cart Pole </h1>
136144
<div class="canvases" id="steps-canvas"></div>
137145
</div>
138146
</div>
139-
140-
<div>
141-
<canvas id="cart-pole-canvas" height="150px" width="500px"></canvas>
142-
</div>
143147
</div>
144148

145149
<script src="index.js"></script>

Diff for: cart-pole/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class PolicyNetwork {
186186
this.currentActions_ = actions.dataSync();
187187
const labels =
188188
tf.sub(1, tf.tensor2d(this.currentActions_, actions.shape));
189-
return tf.sigmoidCrossEntropyWithLogits(labels, logits).asScalar();
189+
return tf.losses.sigmoidCrossEntropy(labels, logits).asScalar();
190190
});
191191
return tf.variableGrads(f);
192192
}

Diff for: cart-pole/ui.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ function enableModelControls() {
125125
deleteStoredModelButton.disabled = false;
126126
}
127127

128-
129128
/**
130129
* Render the current state of the system on an HTML canvas.
131130
*
@@ -134,6 +133,9 @@ function enableModelControls() {
134133
* the rendering will happen.
135134
*/
136135
function renderCartPole(cartPole, canvas) {
136+
if (!canvas.style.display) {
137+
canvas.style.display = 'block';
138+
}
137139
const X_MIN = -cartPole.xThreshold;
138140
const X_MAX = cartPole.xThreshold;
139141
const xRange = X_MAX - X_MIN;
@@ -306,7 +308,7 @@ export async function setUpUI() {
306308
const action = policyNet.getActions(cartPole.getStateTensor())[0];
307309
logStatus(
308310
`Test in progress. ` +
309-
`Action: ${action === 1 ? '' : ' '} (Step ${steps})`);
311+
`Action: ${action === 1 ? '<--' : ' -->'} (Step ${steps})`);
310312
isDone = cartPole.update(action);
311313
renderCartPole(cartPole, cartPoleCanvas);
312314
});

0 commit comments

Comments
 (0)