Skip to content

Instantly share code, notes, and snippets.

View nkreeger's full-sized avatar

Nick Kreeger nkreeger

  • Minneapolis / St. Paul, MN
View GitHub Profile
px pz sz_top sz_bot left_handed_batter is_strike
0.53911291447047 2.58721257326413 3.621919456338947 1.6849794648356244 1 1
0.185253995350261 1.5635593603608 3.7237799447316 1.8320068318667317 0 1
0.803388018408266 2.26077819347672 3.6227808116418068 1.6855622410347877 0 1
0.93011705400413 2.41483140448302 3.6523741032464905 1.777227990061938 1 1
0.627323597921344 2.40464482128051 3.5789796232592153 1.707844742390911 0 1
0.27398840848321 3.39084709206671 3.5627386518120403 1.641478162459645 1 1
0.63449599239141 1.98240225645648 3.5317091900453974 1.693213715035089 1 1
-1.00475955026255 2.65645601946872 3.580891057287076 1.771934367972129 1 1
0.182075023123555 2.24226087041535 3.3478216713055424 1.4576288613830712 0 1
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
px pz sz_top sz_bot left_handed_batter is_strike
0.53911291447047 2.58721257326413 3.621919456338947 1.6849794648356244 1 1
0.185253995350261 1.5635593603608 3.7237799447316 1.8320068318667317 0 1
0.803388018408266 2.26077819347672 3.6227808116418068 1.6855622410347877 0 1
0.93011705400413 2.41483140448302 3.6523741032464905 1.777227990061938 1 1
0.627323597921344 2.40464482128051 3.5789796232592153 1.707844742390911 0 1
0.27398840848321 3.39084709206671 3.5627386518120403 1.641478162459645 1 1
0.63449599239141 1.98240225645648 3.5317091900453974 1.693213715035089 1 1
const model = tf.sequential();
// Two fully connected layers with dropout between each:
model.add(tf.layers.dense({units: 24, activation: 'relu', inputShape: [5]}));
model.add(tf.layers.dropout({rate: 0.01}));
model.add(tf.layers.dense({units: 16, activation: 'relu'}));
model.add(tf.layers.dropout({rate: 0.01}));
// Only two classes: "strike" and "ball":
model.add(tf.layers.dense({units: 2, activation: 'softmax'}));
const data = [];
csvData.forEach((values) => {
// 'logit' data uses the 5 fields:
const x = [];
x.push(parseFloat(values.px));
x.push(parseFloat(values.pz));
x.push(parseFloat(values.sz_top));
x.push(parseFloat(values.sz_bot));
x.push(parseFloat(values.left_handed_batter));
// The label is simply 'is strike' or 'is ball':
// Trains and reports loss+accuracy for one batch of training data:
async function trainBatch(index) {
const history = await model.fit(batches[index].x, batches[index].y, {
epochs: 1,
shuffle: false,
validationData: [batches[index].x, batches[index].y],
batchSize: CONSTANTS.BATCH_SIZE
});
// Don't block the UI frame by using tf.nextFrame()
function predictZone() {
const predictions = model.predictOnBatch(predictionMatrix.data);
const values = predictions.dataSync();
// Sort each value so the higher prediction is the first element in the array:
const results = [];
let index = 0;
for (let i = 0; i < values.length; i++) {
let list = [];
list.push({value: values[index++], strike: 0});
function updateHeatmap() {
rects.data(generateHeatmapData());
rects
.attr('x', (coord) => { return scaleX(coord.x) * CONSTANTS.HEATMAP_SIZE; })
.attr('y', (coord) => { return scaleY(coord.y) * CONSTANTS.HEATMAP_SIZE; })
.attr('width', CONSTANTS.HEATMAP_SIZE)
.attr('height', CONSTANTS.HEATMAP_SIZE)
.style('fill', (coord) => {
if (coord.strike) {
return strikeColorScale(coord.value);
diff --git a/scripts/install.js b/scripts/install.js
index 6587991..3af5a6f 100644
--- a/scripts/install.js
+++ b/scripts/install.js
@@ -37,10 +37,11 @@ const CPU_DARWIN = 'libtensorflow_r1_10_darwin.tar.gz';
const CPU_LINUX = 'libtensorflow_r1_10_linux_cpu.tar.gz';
const GPU_LINUX = 'libtensorflow_r1_10_linux_gpu.tar.gz';
const CPU_WINDOWS = 'libtensorflow_r1_10_windows_cpu.zip';
+const GPU_WINDOWS = 'libtensorflow_r1_10_windows_gpu.zip';
@nkreeger
nkreeger / docker-compose.yml
Last active November 12, 2018 03:48
Mesa-build Docker compose config
version: '2'
services:
rpi_env:
build:
context: .
dockerfile: Dockerfile
image: rpi_env:cpu
env_file:
- .env
# devices: