Skip to content

Commit 901a212

Browse files
committed
Change the file structure of the neural network
1 parent 20ec3f3 commit 901a212

File tree

119 files changed

+418
-412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+418
-412
lines changed

lib/model/neuralnetwork.js

Lines changed: 9 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import Matrix from '../util/matrix.js'
22
import Tensor from '../util/tensor.js'
3-
import { Layer, LossLayer } from './layer/index.js'
4-
export { Layer } from './layer/index.js'
3+
import { Layer, LossLayer } from './nns/layer/index.js'
4+
export { Layer } from './nns/layer/index.js'
55

6-
import InputLayer from './layer/input.js'
7-
import OutputLayer from './layer/output.js'
6+
import Graph from './nns/graph.js'
7+
export { default as Graph } from './nns/graph.js'
8+
9+
import { SGDOptimizer, MomentumOptimizer, RMSPropOptimizer, AdamOptimizer } from './nns/optimizer.js'
10+
11+
import InputLayer from './nns/layer/input.js'
12+
import OutputLayer from './nns/layer/output.js'
813

914
/**
1015
* Exception for neuralnetwork class
@@ -21,116 +26,6 @@ export class NeuralnetworkException extends Error {
2126
}
2227
}
2328

24-
/**
25-
* Graph for Neuralnetwork structure
26-
*/
27-
export class Graph {
28-
constructor() {
29-
this._nodes = []
30-
}
31-
32-
/**
33-
* Graph nodes
34-
*
35-
* @type {{layer: Layer, name: string, input?: string[], parents: {index: number, subscript?: number}[]}[]}
36-
*/
37-
get nodes() {
38-
return this._nodes
39-
}
40-
41-
/**
42-
* Number of nodes
43-
*
44-
* @type {number}
45-
*/
46-
get size() {
47-
return this._nodes.length
48-
}
49-
50-
/**
51-
* Returns Graph.
52-
*
53-
* @param {Record<string, *>[]} nodes
54-
* @returns {Graph}
55-
*/
56-
static fromObject(nodes) {
57-
const graph = new Graph()
58-
for (const l of nodes) {
59-
const cl = Layer.fromObject(l)
60-
if (typeof l.input === 'string') {
61-
l.input = [l.input]
62-
}
63-
graph.add(cl, l.name, l.input)
64-
}
65-
return graph
66-
}
67-
68-
/**
69-
* Returns object representation.
70-
*
71-
* @returns {Record<string, *>[]}
72-
*/
73-
toObject() {
74-
const s = []
75-
for (let i = 0; i < this._nodes.length; i++) {
76-
const node = this._nodes[i]
77-
const obj = node.layer.toObject()
78-
if (node.name) {
79-
obj.name = node.name
80-
}
81-
if (node.input) {
82-
obj.input = node.input
83-
}
84-
s.push(obj)
85-
}
86-
return s
87-
}
88-
89-
/**
90-
* Add a layer.
91-
*
92-
* @param {Layer} layer
93-
* @param {string} name
94-
* @param {string[] | string} [inputs]
95-
*/
96-
add(layer, name, inputs = undefined) {
97-
let parentinfos = []
98-
if (!inputs) {
99-
if (this._nodes.length > 0) {
100-
parentinfos.push({
101-
index: this._nodes.length - 1,
102-
subscript: null,
103-
})
104-
}
105-
} else {
106-
if (typeof inputs === 'string') {
107-
inputs = [inputs]
108-
}
109-
parentinfos = inputs.map(p => {
110-
const subscriptRegexp = /\[([0-9]+)\]$/
111-
const m = p && p.match(subscriptRegexp)
112-
const subscript = m ? +m[1] : null
113-
const pname = m ? p.slice(0, -m[0].length) : p
114-
for (let k = 0; k < this._nodes.length; k++) {
115-
if (this._nodes[k].name === pname) {
116-
return {
117-
index: k,
118-
subscript,
119-
}
120-
}
121-
}
122-
throw new NeuralnetworkException(`Unknown input name '${p}'.`)
123-
})
124-
}
125-
this._nodes.push({
126-
layer,
127-
name,
128-
input: inputs,
129-
parents: parentinfos,
130-
})
131-
}
132-
}
133-
13429
/**
13530
* Neuralnetwork
13631
*/
@@ -398,124 +293,3 @@ export default class NeuralNetwork {
398293
return this.calc(x).toArray()
399294
}
400295
}
401-
402-
class SGDOptimizer {
403-
constructor(lr) {
404-
this._learningrate = lr
405-
}
406-
407-
set learningRate(value) {
408-
this._learningrate = value
409-
}
410-
411-
manager() {
412-
const this_ = this
413-
return {
414-
get lr() {
415-
return this_._learningrate
416-
},
417-
delta(key, value) {
418-
return value.copyMult(this.lr)
419-
},
420-
}
421-
}
422-
}
423-
424-
class MomentumOptimizer {
425-
constructor(lr, beta = 0.9) {
426-
this._learningrate = lr
427-
this._beta = beta
428-
}
429-
430-
set learningRate(value) {
431-
this._learningrate = value
432-
}
433-
434-
manager() {
435-
const this_ = this
436-
return {
437-
get lr() {
438-
return this_._learningrate
439-
},
440-
params: {},
441-
delta(key, value) {
442-
if (!this.params[key]) {
443-
this.params[key] = value
444-
return value.copyMult(this.lr)
445-
}
446-
const v = this.params[key].copyMult(this_._beta)
447-
v.add(value.copyMult(1 - this_._beta))
448-
this.params[key] = v
449-
return v.copyMult(this.lr)
450-
},
451-
}
452-
}
453-
}
454-
455-
class RMSPropOptimizer {
456-
constructor(lr, beta = 0.999) {
457-
this._learningrate = lr
458-
this._beta = beta
459-
}
460-
461-
set learningRate(value) {
462-
this._learningrate = value
463-
}
464-
465-
manager() {
466-
const this_ = this
467-
return {
468-
get lr() {
469-
return this_._learningrate
470-
},
471-
params: {},
472-
delta(key, value) {
473-
if (!this.params[key]) {
474-
this.params[key] = value.copyMult(value)
475-
return value.copyMult(this.lr)
476-
}
477-
const v = this.params[key].copyMult(this_._beta)
478-
v.add(value.copyMap(x => (1 - this_._beta) * x * x))
479-
this.params[key] = v
480-
return value.copyMult(v.copyMap(x => this.lr / Math.sqrt(x + 1.0e-12)))
481-
},
482-
}
483-
}
484-
}
485-
486-
class AdamOptimizer {
487-
constructor(lr = 0.001, beta1 = 0.9, beta2 = 0.999) {
488-
this._learningrate = lr
489-
this._beta1 = beta1
490-
this._beta2 = beta2
491-
}
492-
493-
set learningRate(value) {
494-
this._learningrate = value
495-
}
496-
497-
manager() {
498-
const this_ = this
499-
return {
500-
get lr() {
501-
return this_._learningrate
502-
},
503-
params: {},
504-
delta(key, value) {
505-
if (!this.params[key]) {
506-
this.params[key] = {
507-
v: value,
508-
s: value.copyMult(value),
509-
}
510-
return value.copyMult(this.lr)
511-
}
512-
const v = this.params[key].v.copyMult(this_._beta1)
513-
v.add(value.copyMult(1 - this_._beta1))
514-
const s = this.params[key].s.copyMult(this_._beta2)
515-
s.add(value.copyMap(x => (1 - this_._beta2) * x * x))
516-
this.params[key] = { v, s }
517-
return v.copyMult(s.copyMap(x => this.lr / Math.sqrt(x + 1.0e-12)))
518-
},
519-
}
520-
}
521-
}

lib/model/nice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import NeuralNetwork from './neuralnetwork.js'
2-
import { FlowLayer } from './layer/base.js'
3-
import ReverseLayer from './layer/reverse.js'
2+
import { FlowLayer } from './nns/layer/base.js'
3+
import ReverseLayer from './nns/layer/reverse.js'
44
import Matrix from '../util/matrix.js'
55

66
/**

lib/model/nns/graph.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { NeuralnetworkException } from '../neuralnetwork.js'
2+
import { Layer } from './layer/index.js'
3+
4+
/**
5+
* Graph for Neuralnetwork structure
6+
*/
7+
export default class Graph {
8+
constructor() {
9+
this._nodes = []
10+
}
11+
12+
/**
13+
* Graph nodes
14+
*
15+
* @type {{layer: Layer, name: string, input?: string[], parents: {index: number, subscript?: number}[]}[]}
16+
*/
17+
get nodes() {
18+
return this._nodes
19+
}
20+
21+
/**
22+
* Number of nodes
23+
*
24+
* @type {number}
25+
*/
26+
get size() {
27+
return this._nodes.length
28+
}
29+
30+
/**
31+
* Returns Graph.
32+
*
33+
* @param {Record<string, *>[]} nodes
34+
* @returns {Graph}
35+
*/
36+
static fromObject(nodes) {
37+
const graph = new Graph()
38+
for (const l of nodes) {
39+
const cl = Layer.fromObject(l)
40+
if (typeof l.input === 'string') {
41+
l.input = [l.input]
42+
}
43+
graph.add(cl, l.name, l.input)
44+
}
45+
return graph
46+
}
47+
48+
/**
49+
* Returns object representation.
50+
*
51+
* @returns {Record<string, *>[]}
52+
*/
53+
toObject() {
54+
const s = []
55+
for (let i = 0; i < this._nodes.length; i++) {
56+
const node = this._nodes[i]
57+
const obj = node.layer.toObject()
58+
if (node.name) {
59+
obj.name = node.name
60+
}
61+
if (node.input) {
62+
obj.input = node.input
63+
}
64+
s.push(obj)
65+
}
66+
return s
67+
}
68+
69+
/**
70+
* Add a layer.
71+
*
72+
* @param {Layer} layer
73+
* @param {string} name
74+
* @param {string[] | string} [inputs]
75+
*/
76+
add(layer, name, inputs = undefined) {
77+
let parentinfos = []
78+
if (!inputs) {
79+
if (this._nodes.length > 0) {
80+
parentinfos.push({
81+
index: this._nodes.length - 1,
82+
subscript: null,
83+
})
84+
}
85+
} else {
86+
if (typeof inputs === 'string') {
87+
inputs = [inputs]
88+
}
89+
parentinfos = inputs.map(p => {
90+
const subscriptRegexp = /\[([0-9]+)\]$/
91+
const m = p && p.match(subscriptRegexp)
92+
const subscript = m ? +m[1] : null
93+
const pname = m ? p.slice(0, -m[0].length) : p
94+
for (let k = 0; k < this._nodes.length; k++) {
95+
if (this._nodes[k].name === pname) {
96+
return {
97+
index: k,
98+
subscript,
99+
}
100+
}
101+
}
102+
throw new NeuralnetworkException(`Unknown input name '${p}'.`)
103+
})
104+
}
105+
this._nodes.push({
106+
layer,
107+
name,
108+
input: inputs,
109+
parents: parentinfos,
110+
})
111+
}
112+
}
File renamed without changes.

lib/model/layer/add.js renamed to lib/model/nns/layer/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Layer from './base.js'
2-
import Matrix from '../../util/matrix.js'
2+
import Matrix from '../../../util/matrix.js'
33

44
export default class AddLayer extends Layer {
55
calc(...x) {

lib/model/layer/additive_coupling.js renamed to lib/model/nns/layer/additive_coupling.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Matrix from '../../util/matrix.js'
2-
import NeuralNetwork from '../neuralnetwork.js'
1+
import Matrix from '../../../util/matrix.js'
2+
import NeuralNetwork from '../../neuralnetwork.js'
33
import { FlowLayer } from './base.js'
44

55
export default class AdditiveCoupling extends FlowLayer {

0 commit comments

Comments
 (0)