1
0
Fork 0
mirror of https://github.com/Findus23/nn_evaluate.git synced 2024-09-19 14:53:44 +02:00

add typescript version

This commit is contained in:
Lukas Winkler 2021-03-21 19:11:25 +01:00
parent e4b82cf4d4
commit fbd789d92e
Signed by: lukas
GPG key ID: 54DE4D798D244853
11 changed files with 248 additions and 0 deletions

3
typescript/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
public
!public/index.html

33
typescript/build.js Normal file
View file

@ -0,0 +1,33 @@
const esbuild = require('esbuild')
switch (process.argv[2]) {
case "build":
esbuild.build({
entryPoints: ["main.ts"],
target: "es2020",
bundle: true,
sourcemap: true,
minify: true,
color: true,
outdir: "public",
})
break
case "serve":
esbuild.serve({
port: 1234,
servedir: "public"
}, {
entryPoints: ['main.ts'],
bundle: true,
target: "es2020",
outdir: 'public',
sourcemap: true,
format: "esm", splitting: true
// minify:true
}).catch(() => process.exit(1))
break
default:
console.log(process.argv)
}

63
typescript/calc.ts Normal file
View file

@ -0,0 +1,63 @@
import {
hiddenBias,
hiddenLayerSize,
hiddenWeight,
means,
outputBias,
outputLayerSize,
outputWeight,
stds
} from "./data";
import {relu, sigmoid} from "./utils";
function calculate_layer(layerSize: number,
parentLayer: number[],
weight: number[][],
bias: number[]): number[] {
const new_layer: number[] = []
for (let hl = 0; hl < layerSize; hl++) {
let node = 0
for (let parent = 0; parent < parentLayer.length; parent++) {
node += parentLayer[parent] * weight[hl][parent]
}
node += bias[hl]
new_layer.push(node)
}
return new_layer
}
function evaluate(input: number[]): number[] {
const scaled_input = scale_input(input)
let hidden_layer = calculate_layer(hiddenLayerSize, scaled_input, hiddenWeight, hiddenBias)
hidden_layer = hidden_layer.map(i => relu(i))
let output_layer = calculate_layer(outputLayerSize, hidden_layer, outputWeight, outputBias)
output_layer = output_layer.map(i => sigmoid(i))
return output_layer
}
function scale_input(input: number[]): number[] {
return input.map((val, idx) =>
((val - means[idx]) / stds[idx])
)
}
export function calculate_grid(mass: number, gamma: number, wp: number, wt: number, resolution: number, mode: number): number[] {
const start = performance.now()
const datalist: number[] = []
for (let j = 0; j < resolution; j++) {
for (let i = 0; i < resolution; i++) {
const entry = [
i / resolution * 60,
j / resolution * 5.5,
mass,
gamma,
wt,
wp
];
datalist.push(evaluate(entry)[mode])
}
}
const end = performance.now()
console.info((end - start) + " ms")
return datalist
}

21
typescript/data.ts Normal file
View file

@ -0,0 +1,21 @@
import data from "../pytorch_model.json"
export const means: number[] = data.means
export const stds: number[] = data.stds
export const hiddenWeight: number[][] = data["hidden.weight"]
export const hiddenBias: number[] = data["hidden.bias"]
export const outputWeight: number[][] = data["output.weight"]
export const outputBias: number[] = data["output.bias"]
export const hiddenLayerSize = hiddenBias.length
export const inputLayerSize = means.length
export const outputLayerSize = outputBias.length
const ang = 30
const v = 2
const m = 1e24
const gamma = 0.6
const wp = 1e-4
const wt = wp
export const input = [ang, v, m, gamma, wt, wp]

3
typescript/main.ts Normal file
View file

@ -0,0 +1,3 @@
import {init} from "./ui";
init();

10
typescript/package.json Normal file
View file

@ -0,0 +1,10 @@
{
"devDependencies": {
"@types/lodash": "^4.14.168",
"esbuild": "^0.9.4",
"typescript": "^4.2.3"
},
"dependencies": {
"lodash": "^4.17.21"
}
}

3
typescript/publish.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
rsync -aP public/* lw1.at:/var/www/nn/

14
typescript/tsconfig.json Normal file
View file

@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": [
"es2017",
"dom"
],
"noImplicitAny": true,
"noEmit": true,
"watch": true,
"isolatedModules": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}

62
typescript/ui.ts Normal file
View file

@ -0,0 +1,62 @@
import {calculate_grid} from "./calc";
import {valuesToImage} from "./utils";
import throttle from "lodash/throttle"
export const massExpEl = <HTMLInputElement>document.getElementById("massExp")
export const gammaPercentEl = <HTMLInputElement>document.getElementById("gammaPercent")
export const wtFractionEl = <HTMLInputElement>document.getElementById("wtFraction")
export const wpFractionEl = <HTMLInputElement>document.getElementById("wpFraction")
export const resolutionEl = <HTMLInputElement>document.getElementById("resolution")
export const smoothEl = <HTMLInputElement>document.getElementById("smooth")
export const modeEl = <HTMLSelectElement>document.getElementById("mode")
export const massExpLabel = document.getElementById("massExpLabel")
export const gammaPercentLabel = document.getElementById("gammaPercentLabel")
export const resolutionLabel = document.getElementById("resolutionLabel")
export const wtFractionLabel = document.getElementById("wtFractionLabel")
export const wpFractionLabel = document.getElementById("wpFractionLabel")
export const canvas = <HTMLCanvasElement>document.getElementById("outputCanvas")
function update(): void {
if (smoothEl.checked) {
canvas.classList.remove("crisp")
} else {
canvas.classList.add("crisp")
}
const mass = Math.pow(10, Number(massExpEl.value))
massExpLabel.innerText = String(mass) + " kg"
const gamma = Number(gammaPercentEl.value) / 100
gammaPercentLabel.innerText = String(gamma)
const wtFraction = Math.pow(10, Number(wtFractionEl.value))
wtFractionLabel.innerText = String(wtFraction)
const wpFraction = Math.pow(10, Number(wpFractionEl.value))
wpFractionLabel.innerText = String(wpFraction)
const resolution = Number(resolutionEl.value)
resolutionLabel.innerText = String(resolution) + " px"
const mode = Number(modeEl.value)
const output = calculate_grid(mass, gamma, wpFraction, wtFraction, resolution, mode)
const image = new ImageData(valuesToImage(output), resolution, resolution)
const context = canvas.getContext('2d')
context.canvas.width = resolution;
context.canvas.height = resolution;
context.putImageData(image, 0, 0);
}
export function init(): void {
const start = performance.now()
update()
const end = performance.now()
const testTime = end - start
console.info(`initial run took ${testTime}ms`)
const throttled = throttle(update, testTime)
const type = (testTime > 500) ? "change" : "input"
massExpEl.addEventListener(type, throttled)
gammaPercentEl.addEventListener(type, throttled)
wtFractionEl.addEventListener(type, throttled)
wpFractionEl.addEventListener(type, throttled)
resolutionEl.addEventListener(type, throttled)
smoothEl.addEventListener("change", throttled)
modeEl.addEventListener("change", throttled)
}

13
typescript/utils.ts Normal file
View file

@ -0,0 +1,13 @@
export const relu = (x: number) => Math.max(0, x);
export const sigmoid = (x: number) => 1 / (1 + Math.exp(-x));
export function valuesToImage(values: number[]): Uint8ClampedArray {
const imagedata: number[] = [];
for (let i = 0; i < values.length; i++) {
const color = 255 - Math.round(values[i] * 255);
imagedata.push(color, color, color / 2 + 127, 255)
}
return new Uint8ClampedArray(imagedata)
}

23
typescript/yarn.lock Normal file
View file

@ -0,0 +1,23 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/lodash@^4.14.168":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
esbuild@^0.9.4:
version "0.9.4"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.9.4.tgz#4480ffc4c1e5d5bb25958f889b5de0279bfb2d6f"
integrity sha512-bF6laCiYE5+iAfZsX+v6Lwvi5QbvKN3tThxDIR2WLyLYzTzNn0ijdpqkvTVsafmRZjic2Nq1nkSf5RSWySDTjA==
lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
typescript@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==