Published
Edited
Jun 6, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
InputValues = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Insert cell
md `## Graph Plot Setting`
Insert cell
height =800
Insert cell
width =1000
Insert cell
maxLayer =d3.max(data.nodes, d => d.LayerNum)
Insert cell
maxwt = d3.max(data.links, d => Math.abs(d.weight))
Insert cell
maxnode = d3.max(data.nodes, d => Math.abs(d.value))
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.LayerNum);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
md `## Graph Generation`
Insert cell
NN = d3.json(model)
Insert cell
InputNames = ['phi_0', 'theta_0', 'x_99', 'y_99', 'phi_99', 'theta_99', 'xdot_0', 'ydot_0', 'phidot_0', 'thetadot_0']
Insert cell
Insert cell
function ComputeLayerValues2(input){
let nodes = [];
let links = [];
let connected = [];
for (let i=0; i<NN['weights'][0].length; i++){
let node = {'id': i, 'LayerNum': 0, 'NodeNum': InputNames[i], 'value': input[i], 'valueScaled': 2*input[i]}
nodes.push(node)
}
let currentLayer = [].concat(input)
for (let l=0; l<NN['weights'].length; l++){
const weight = NN['weights'][l]
let bias = [].concat(NN['bias'][l])
let nextLayer =[].concat(bias)
for (let i=0; i<weight.length; i++){
for (let j=0; j<weight[i].length; j++){
nextLayer[j] += weight[i][j]*currentLayer[i]
let link = {'source': l*NN['WidthLimit']+i, 'target': (l+1)*NN['WidthLimit']+j, 'weight': weight[i][j]}
if (Math.abs(link.weight) > 0.001 || NN.WidthLimit<=100){
links.push(link)
if (!connected.includes(link.source)){
connected.push(link.source)
}
if (!connected.includes(link.target)){
connected.push(link.target)
}
}
}
}
for (let k=0; k<nextLayer.length; k++){
let v = nextLayer[k]
if (l !== NN['weights'].length-1){
v = Math.tanh(nextLayer[k]) // apply activation function
let node = {'id': (l+1)*NN['WidthLimit']+k, 'LayerNum': l+1, 'NodeNum': k, 'value': v, 'valueScaled': v}
nodes.push(node)
nextLayer[k] = v
}
if (l === NN['weights'].length-1){
let node = {'id': (l+1)*NN['WidthLimit']+k, 'LayerNum': l+1, 'NodeNum': OutputNames[k], 'value': v, 'valueScaled': Math.tanh(v)}
nodes.push(node)
}
}
currentLayer = [].concat(nextLayer)
}
let nodesConnect = []
for (let n=0; n<nodes.length; n++){
if (connected.includes(nodes[n].id)){
nodesConnect.push(nodes[n])
}
}
return {"nodes": nodesConnect, "links": links};
}
Insert cell
function RecomputeLayerValues2(input){
let nodes = [];
let links = [];
let connected = [];
for (let i=0; i<NN['weights'][0].length; i++){
let node = {'id': i, 'LayerNum': 0, 'NodeNum': InputNames[i], 'value': input[i], 'valueScaled': 2*input[i]}
document.getElementById("node_"+i).setAttribute("r",Math.abs(2*input[i])*10+3);
document.getElementById("node_"+i).setAttribute("fill" , input[i]>0? "#FF8C00":"#00CED1");
document.getElementById("title_"+i).textContent ='layer: 0, node: '+ InputNames[i] + ', value: '+ input[i];
nodes.push(node)
}
let currentLayer = [].concat(input)
for (let l=0; l<NN['weights'].length; l++){
const weight = NN['weights'][l]
let bias = [].concat(NN['bias'][l])
let nextLayer =[].concat(bias)
for (let i=0; i<weight.length; i++){
for (let j=0; j<weight[i].length; j++){
nextLayer[j] += weight[i][j]*currentLayer[i]
let link = {'source': l*NN['WidthLimit']+i, 'target': (l+1)*NN['WidthLimit']+j, 'weight': weight[i][j]}
if (Math.abs(link.weight) > 0.001 || NN.WidthLimit<=100){
links.push(link)
if (!connected.includes(link.source)){
connected.push(link.source)
}
if (!connected.includes(link.target)){
connected.push(link.target)
}
}
}
}
for (let k=0; k<nextLayer.length; k++){
let v = nextLayer[k]
if (l !== NN['weights'].length-1){
v = Math.tanh(nextLayer[k]) // apply activation function
let node = {'id': (l+1)*NN['WidthLimit']+k, 'LayerNum': l+1, 'NodeNum': k, 'value': v, 'valueScaled': v}
var temp = (l+1)*NN['WidthLimit']+k
document.getElementById("node_"+temp.toString()).setAttribute("r" , Math.abs(Math.tanh(nextLayer[k]))*10+3);
document.getElementById("node_"+temp.toString()).setAttribute("fill" , v>0? "#FF8C00":"#00CED1");
document.getElementById("title_"+temp.toString()).textContent ="layer: "+ l+1 +", node: " + k + ', value: '+ Math.tanh(nextLayer[k]);
nodes.push(node)
nextLayer[k] = v
}
if (l === NN['weights'].length-1){
let node = {'id': (l+1)*NN['WidthLimit']+k, 'LayerNum': l+1, 'NodeNum': OutputNames[k], 'value': v, 'valueScaled': Math.tanh(v)}
var temp = (l+1)*NN['WidthLimit']+k
document.getElementById("node_"+temp.toString()).setAttribute("r" , Math.abs(Math.tanh(nextLayer[k]))*10+3);
document.getElementById("node_"+temp.toString()).setAttribute("fill" , v>0? "#FF8C00":"#00CED1");
document.getElementById("title_"+temp.toString()).textContent ="layer: "+ l+1 +", node: " + OutputNames[k] + ', value: '+ Math.tanh(nextLayer[k]);
nodes.push(node)
}
}
currentLayer = [].concat(nextLayer)
}
let nodesConnect = []
for (let n=0; n<nodes.length; n++){
if (connected.includes(nodes[n].id)){
nodesConnect.push(nodes[n])
}
}
return {"nodes": nodesConnect, "links": links};
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more