forked from ToonTalk/ToonTalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeeplearn.html
More file actions
86 lines (69 loc) · 2.73 KB
/
Copy pathdeeplearn.html
File metadata and controls
86 lines (69 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<script src="https://unpkg.com/deeplearn"></script>
<script>
var math = new deeplearn.NDArrayMathGPU();
const g = new deeplearn.Graph();
// Placeholders are input containers. This is the container for where we will
// feed an input NDArray when we execute the graph.
const inputShape = [3];
const inputTensor = g.placeholder('input', inputShape);
const labelShape = [1];
const labelTensor = g.placeholder('label', labelShape);
// Variables are containers that hold a value that can be updated from
// training.
// Here we initialize the multiplier variable randomly.
const multiplier = g.variable('multiplier', deeplearn.Array2D.randNormal([1, 3]));
// Top level graph methods take Tensors and return Tensors.
const outputTensor = g.matmul(multiplier, inputTensor);
const costTensor = g.meanSquaredCost(outputTensor, labelTensor);
const learningRate = .00001;
const batchSize = 3;
const session = new deeplearn.Session(g, math);
const optimizer = new deeplearn.SGDOptimizer(learningRate);
const inputs = [
deeplearn.Array1D.new([1.0, 2.0, 3.0]),
deeplearn.Array1D.new([10.0, 20.0, 30.0]),
deeplearn.Array1D.new([100.0, 200.0, 300.0])
];
const labels = [
deeplearn.Array1D.new([4.0]),
deeplearn.Array1D.new([40.0]),
deeplearn.Array1D.new([400.0])
];
// Shuffles inputs and labels and keeps them mutually in sync.
const shuffledInputProviderBuilder =
new deeplearn.InCPUMemoryShuffledInputProviderBuilder([inputs, labels]);
const [inputProvider, labelProvider] =
shuffledInputProviderBuilder.getInputProviders();
// Maps tensors to InputProviders.
const feedEntries = [
{tensor: inputTensor, data: inputProvider},
{tensor: labelTensor, data: labelProvider}
];
const NUM_BATCHES = 10;
for (let i = 0; i < NUM_BATCHES; i++) {
// Wrap session.train in a scope so the cost gets cleaned up automatically.
math.scope(() => {
// Train takes a cost tensor to minimize. Trains one batch. Returns the
// average cost as a Scalar.
const cost = session.train(
costTensor, feedEntries, batchSize, optimizer, deeplearn.CostReduction.MEAN);
console.log('last average cost (' + i + '): ' + cost.get());
});
}
// Wrap session.eval in a scope so the intermediate values get cleaned up
// automatically.
math.scope((keep, track) => {
const testInput = track(deeplearn.Array1D.new([0.1, 0.2, 0.3]));
// session.eval can take NDArrays as input data.
const testFeedEntries = [
{tensor: inputTensor, data: testInput}
];
const testOutput = session.eval(outputTensor, testFeedEntries);
console.log('---inference output---');
console.log('shape: ' + testOutput.shape);
console.log('value: ' + testOutput.get(0));
});
// Cleanup training data.
inputs.forEach(input => input.dispose());
labels.forEach(label => label.dispose());
</script>