Published
Edited
Dec 1, 2019
1 star
Insert cell
Insert cell
inputFeatureDim = 16
Insert cell
outputFeatureDim = 16
Insert cell
Insert cell
network = tidier(() => {
const model = tf.sequential()
model.add(tf.layers.dense({units: inputFeatureDim, inputShape: [inputFeatureDim]}))
model.add(tf.layers.dense({units: 16}))
model.add(tf.layers.dense({units: 16}))
model.add(tf.layers.dense({units: outputFeatureDim}))
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'})
return model
}, invalidation)
Insert cell
Insert cell
g = makeGraph([['a', 'b'], ['a', 'd'], ['d', 'a'], ['b', 'c']])
Insert cell
Insert cell
execGCN = (network, {normalizedAdjMatrix, nodeFeatures}, applyActivationToFinalOutput=false) => tf.tidy(() => {
const layers = network.layers
const adj = tf.tensor(normalizedAdjMatrix)
let x = tf.tensor(nodeFeatures)
for(const layer of network.layers) {
const res = layer.apply([x])
const connected = adj.matMul(res)
if(!applyActivationToFinalOutput && layer == layers[layers.length - 1]) return connected
x = tf.relu(connected)
}
return x
})
Insert cell
Insert cell
embeddings = tidier(() => execGCN(network, g).arraySync(), invalidation)
Insert cell
nodeEmbeddings = embeddings
.reduce((map, feature, i) => (map.set(g.nodes[i], feature), map), new Map)
Insert cell
Insert cell
Insert cell
symmetricalNormalization = adjMatrix => tf.tidy(() => {
const adjT = tf.tensor(adjMatrix)
const degree = adjT.sum(1)
return adjT.transpose().div(degree.sqrt()).transpose().div(degree.sqrt()).arraySync()
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
compose = (...fns) => arg => fns.reduce((x, fn) => fn(x), arg)
Insert cell
transpose = mat => mat.map((row, i) => row.map((col, j) => mat[j][i]))
Insert cell
oneHot = (i, length) => Array.from({length}, (_, j) => i === j ? 1 : 0)
Insert cell
unifRand = (length, gen) => Array.from({length}, () => gen())
Insert cell
memoryUsage = { now; return tf.memory()}
Insert cell
Insert cell
tf = require('@tensorflow/tfjs@1.3.2')
Insert cell
rand = require('seedrandom')
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