Skip to content

Commit

Permalink
Merge pull request #6 from nathanjzhao/policy-in-wasm
Browse files Browse the repository at this point in the history
changes
  • Loading branch information
nathanjzhao authored Aug 30, 2024
2 parents 39af631 + e5d80a5 commit 99e8516
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
13 changes: 8 additions & 5 deletions examples/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export class MuJoCoDemo {

switch (this.params.scene) {
case 'humanoid.xml':
modelPath = 'models/humanoid_stand_6_frams_noise_1e-4/model.json';
modelPath = 'models/humanoid_stand_4_frams_noise_1e-4/model.json';
this.getObservation = () => this.getObservationSkeleton(0, 10, 6);
break;
case 'blank':
Expand Down Expand Up @@ -544,10 +544,13 @@ export class MuJoCoDemo {

const [actorMean, logStd, criticValue] = prediction;

console.log('Actor Mean:', actorMean.arraySync());
console.log('Log Std:', logStd.arraySync());
console.log('Critic Value:', criticValue.arraySync());

// console.log('Actor Mean:', actorMean.arraySync());
// console.log('Log Std:', logStd.arraySync());
// console.log('Critic Value:', criticValue.arraySync());

// Update the critic value in the GUI
this.params.criticValue = criticValue.dataSync()[0];

// Use tf.tidy to automatically dispose of intermediate tensors
tf.tidy(() => {
const stdDev = tf.exp(logStd);
Expand Down
44 changes: 14 additions & 30 deletions examples/mujocoUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,14 @@ export function setupGUI(parentContext) {
dropdown.__onChange = onChangeFunc;
}

// function updateSceneDropdown(dropdown, scenes) {
// // Remove all options from the underlying select element
// if (dropdown.__select && dropdown.__select.options) {
// dropdown.__select.options.length = 0;
// }
// console.log("scenes", scenes)

// // Update the dropdown's __select property
// dropdown.__select = document.createElement('select');

// // Add new options
// for (let [name, file] of Object.entries(scenes)) {
// let option = document.createElement('option');
// option.text = name;
// option.value = file;
// dropdown.__select.add(option);
// }

// // Update the controller's object and property
// dropdown.object = scenes;
// dropdown.property = 'scene';

// // Rebuild the DOM elements
// dropdown.domElement.removeChild(dropdown.domElement.childNodes[0]);
// dropdown.domElement.appendChild(dropdown.__select);

// // Update the display
// dropdown.updateDisplay();
// dropdown.onChange(reload);
// }

// Add a folder for critic values
let criticFolder = parentContext.gui.addFolder("Critic Values");

// Add a display for critic values
parentContext.params.criticValue = 0;
let criticValueDisplay = criticFolder.add(parentContext.params, 'criticValue').name('Critic Value').listen();
criticValueDisplay.domElement.style.pointerEvents = 'none';

// Add a help menu.
// Parameters:
Expand Down Expand Up @@ -401,11 +379,17 @@ export function setupGUI(parentContext) {
modelEnabledText.innerHTML = 'Model enabled';
modelEnabledText.id = 'model-enabled-text';
parentContext.container.appendChild(modelEnabledText);

// Show critic folder when model is enabled
criticFolder.open();
} else {
const modelEnabledText = document.getElementById('model-enabled-text');
if (modelEnabledText) {
parentContext.container.removeChild(modelEnabledText);
}

// Hide critic folder when model is disabled
criticFolder.close();
}
});

Expand Down
17 changes: 5 additions & 12 deletions examples/scenes/humanoid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
-->

<mujoco model="Humanoid">
<option timestep="0.005" iterations="1" ls_iterations="4">
<flag eulerdamp="disable"/>
</option>
<option timestep="0.005"/>

<visual>
<map force="0.1" zfar="30"/>
Expand All @@ -28,7 +26,7 @@

<asset>
<texture type="skybox" builtin="gradient" rgb1=".3 .5 .7" rgb2="0 0 0" width="32" height="512"/>
<texture name="body" type="cube" builtin="flat" mark="cross" width="128" height="128" rgb1="0.8 0.6 0.4" rgb2="0.8 0.6 0.4" markrgb="1 1 1" random="0.01"/>
<texture name="body" type="cube" builtin="flat" mark="cross" width="127" height="1278" rgb1="0.8 0.6 0.4" rgb2="0.8 0.6 0.4" markrgb="1 1 1" random="0.01"/>
<material name="body" texture="body" texuniform="true" rgba="0.8 0.6 .4 1"/>
<texture name="grid" type="2d" builtin="checker" width="512" height="512" rgb1=".1 .2 .3" rgb2=".2 .3 .4"/>
<material name="grid" texture="grid" texrepeat="1 1" texuniform="true" reflectance=".2"/>
Expand All @@ -39,8 +37,7 @@
<default class="body">

<!-- geoms -->
<!-- TODO(robotics-simulation): support condim=1 for humanoid capsules. -->
<geom type="capsule" condim="3" friction=".7" solimp=".9 .99 .003" solref=".015 1" material="body" contype="0" conaffinity="0"/>
<geom type="capsule" condim="1" friction=".7" solimp=".9 .99 .003" solref=".015 1" material="body"/>
<default class="thigh">
<geom size=".06"/>
</default>
Expand Down Expand Up @@ -189,13 +186,9 @@
<contact>
<exclude body1="waist_lower" body2="thigh_right"/>
<exclude body1="waist_lower" body2="thigh_left"/>
<pair geom1="foot1_left" geom2="floor"/>
<pair geom1="foot1_right" geom2="floor"/>
<pair geom1="foot2_left" geom2="floor"/>
<pair geom1="foot2_right" geom2="floor"/>
</contact>

<!-- <tendon>
<tendon>
<fixed name="hamstring_right" limited="true" range="-0.3 2">
<joint joint="hip_y_right" coef=".5"/>
<joint joint="knee_right" coef="-.5"/>
Expand All @@ -204,7 +197,7 @@
<joint joint="hip_y_left" coef=".5"/>
<joint joint="knee_left" coef="-.5"/>
</fixed>
</tendon> -->
</tendon>

<actuator>
<motor name="abdomen_y" gear="40" joint="abdomen_y"/>
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions models/humanoid_stand_4_frams_noise_1e-4/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format": "layers-model", "generatedBy": "keras v2.16.0", "convertedBy": "TensorFlow.js Converter v4.20.0", "modelTopology": {"keras_version": "2.16.0", "backend": "tensorflow", "model_config": {"class_name": "Functional", "config": {"name": "model", "trainable": true, "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 338], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Dense", "config": {"name": "actor_dense1", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_dense1", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "actor_dense2", "trainable": true, "dtype": "float32", "units": 128, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_dense2", "inbound_nodes": [[["actor_dense1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_dense1", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_dense1", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "actor_mean", "trainable": true, "dtype": "float32", "units": 21, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 0.01, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_mean", "inbound_nodes": [[["actor_dense2", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_dense2", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_dense2", "inbound_nodes": [[["critic_dense1", 0, 0, {}]]]}, {"class_name": "LogStdLayer", "config": {"name": "log_std", "trainable": true, "dtype": "float32", "action_dim": 21}, "name": "log_std", "inbound_nodes": [[["actor_mean", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_output", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_output", "inbound_nodes": [[["critic_dense2", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["actor_mean", 0, 0], ["log_std", 0, 0], ["critic_output", 0, 0]]}}}, "weightsManifest": [{"paths": ["group1-shard1of1.bin"], "weights": [{"name": "actor_dense1/kernel", "shape": [338, 256], "dtype": "float32"}, {"name": "actor_dense1/bias", "shape": [256], "dtype": "float32"}, {"name": "actor_dense2/kernel", "shape": [256, 128], "dtype": "float32"}, {"name": "actor_dense2/bias", "shape": [128], "dtype": "float32"}, {"name": "actor_mean/kernel", "shape": [128, 21], "dtype": "float32"}, {"name": "actor_mean/bias", "shape": [21], "dtype": "float32"}, {"name": "critic_dense1/kernel", "shape": [338, 256], "dtype": "float32"}, {"name": "critic_dense1/bias", "shape": [256], "dtype": "float32"}, {"name": "critic_dense2/kernel", "shape": [256, 256], "dtype": "float32"}, {"name": "critic_dense2/bias", "shape": [256], "dtype": "float32"}, {"name": "critic_output/kernel", "shape": [256, 1], "dtype": "float32"}, {"name": "critic_output/bias", "shape": [1], "dtype": "float32"}, {"name": "log_std/log_std", "shape": [21], "dtype": "float32"}]}]}
Binary file added models/velocity-2-ctrl-0.1/group1-shard1of1.bin
Binary file not shown.
1 change: 1 addition & 0 deletions models/velocity-2-ctrl-0.1/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format": "layers-model", "generatedBy": "keras v2.16.0", "convertedBy": "TensorFlow.js Converter v4.20.0", "modelTopology": {"keras_version": "2.16.0", "backend": "tensorflow", "model_config": {"class_name": "Functional", "config": {"name": "model", "trainable": true, "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 338], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Dense", "config": {"name": "actor_dense1", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_dense1", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "actor_dense2", "trainable": true, "dtype": "float32", "units": 128, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_dense2", "inbound_nodes": [[["actor_dense1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_dense1", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_dense1", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "actor_mean", "trainable": true, "dtype": "float32", "units": 21, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 0.01, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "actor_mean", "inbound_nodes": [[["actor_dense2", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_dense2", "trainable": true, "dtype": "float32", "units": 256, "activation": "tanh", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.4142135623730951, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_dense2", "inbound_nodes": [[["critic_dense1", 0, 0, {}]]]}, {"class_name": "LogStdLayer", "config": {"name": "log_std", "trainable": true, "dtype": "float32", "action_dim": 21}, "name": "log_std", "inbound_nodes": [[["actor_mean", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "critic_output", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"module": "keras.initializers", "class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "registered_name": null}, "bias_initializer": {"module": "keras.initializers", "class_name": "Zeros", "config": {}, "registered_name": null}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "critic_output", "inbound_nodes": [[["critic_dense2", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["actor_mean", 0, 0], ["log_std", 0, 0], ["critic_output", 0, 0]]}}}, "weightsManifest": [{"paths": ["group1-shard1of1.bin"], "weights": [{"name": "actor_dense1/kernel", "shape": [338, 256], "dtype": "float32"}, {"name": "actor_dense1/bias", "shape": [256], "dtype": "float32"}, {"name": "actor_dense2/kernel", "shape": [256, 128], "dtype": "float32"}, {"name": "actor_dense2/bias", "shape": [128], "dtype": "float32"}, {"name": "actor_mean/kernel", "shape": [128, 21], "dtype": "float32"}, {"name": "actor_mean/bias", "shape": [21], "dtype": "float32"}, {"name": "critic_dense1/kernel", "shape": [338, 256], "dtype": "float32"}, {"name": "critic_dense1/bias", "shape": [256], "dtype": "float32"}, {"name": "critic_dense2/kernel", "shape": [256, 256], "dtype": "float32"}, {"name": "critic_dense2/bias", "shape": [256], "dtype": "float32"}, {"name": "critic_output/kernel", "shape": [256, 1], "dtype": "float32"}, {"name": "critic_output/bias", "shape": [1], "dtype": "float32"}, {"name": "log_std/log_std", "shape": [21], "dtype": "float32"}]}]}

0 comments on commit 99e8516

Please sign in to comment.