Skip to content

deeplearn 15 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 52 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"deeplearn": "0.3.12",
"deeplearn": "0.3.15",
"react": "^16.1.1",
"react-dom": "^16.1.1",
"react-scripts": "1.0.17"
Expand Down
44 changes: 25 additions & 19 deletions src/neuralNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
Graph,
Session,
SGDOptimizer,
NDArrayMathGPU,
ENV,
NDArrayMath,
CostReduction,
} from 'deeplearn';

// Encapsulates math operations on the CPU and GPU.
const math = new NDArrayMathGPU();

class ColorAccessibilityModel {
// Encapsulates math operations on the CPU and GPU.
math = ENV.math;

// Runs training.
session;

Expand Down Expand Up @@ -47,27 +48,32 @@ class ColorAccessibilityModel {
this.predictionTensor = this.createFullyConnectedLayer(graph, fullyConnectedLayer, 3, 2);
this.costTensor = graph.meanSquaredCost(this.targetTensor, this.predictionTensor);

this.session = new Session(graph, math);
this.session = new Session(graph, this.math);

this.prepareTrainingSet(trainingSet);
}

prepareTrainingSet(trainingSet) {
math.scope(() => {
const { rawInputs, rawTargets } = trainingSet;
const oldMath = ENV.math;
const safeMode = false;
const math = new NDArrayMath('cpu', safeMode);
ENV.setMath(math);

const inputArray = rawInputs.map(v => Array1D.new(this.normalizeColor(v)));
const targetArray = rawTargets.map(v => Array1D.new(v));
const { rawInputs, rawTargets } = trainingSet;

const shuffledInputProviderBuilder = new InCPUMemoryShuffledInputProviderBuilder([ inputArray, targetArray ]);
const [ inputProvider, targetProvider ] = shuffledInputProviderBuilder.getInputProviders();
const inputArray = rawInputs.map(v => Array1D.new(this.normalizeColor(v)));
const targetArray = rawTargets.map(v => Array1D.new(v));

// Maps tensors to InputProviders.
this.feedEntries = [
{ tensor: this.inputTensor, data: inputProvider },
{ tensor: this.targetTensor, data: targetProvider },
];
});
const shuffledInputProviderBuilder = new InCPUMemoryShuffledInputProviderBuilder([ inputArray, targetArray ]);
const [ inputProvider, targetProvider ] = shuffledInputProviderBuilder.getInputProviders();

// Maps tensors to InputProviders.
this.feedEntries = [
{ tensor: this.inputTensor, data: inputProvider },
{ tensor: this.targetTensor, data: targetProvider },
];

ENV.setMath(oldMath);
}

train(step, computeCost) {
Expand All @@ -77,7 +83,7 @@ class ColorAccessibilityModel {

// Train one batch.
let costValue;
math.scope(() => {
this.math.scope(() => {
const cost = this.session.train(
this.costTensor,
this.feedEntries,
Expand All @@ -98,7 +104,7 @@ class ColorAccessibilityModel {
predict(rgb) {
let classifier = [];

math.scope(() => {
this.math.scope(() => {
const mapping = [{
tensor: this.inputTensor,
data: Array1D.new(this.normalizeColor(rgb)),
Expand Down