Skip to content
Merged
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
9 changes: 8 additions & 1 deletion js/view/gan.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export default function (platform) {
{ name: 'dis_rate', title: 'D', value: 0.5 },
]) {
const grd = ganRatesDiv.div()
rateElms[v.name] = grd.input.number({ label: v.title, name: v.name, min: 0, max: 100, step: 0.01, value: v.value })
rateElms[v.name] = grd.input.number({
label: v.title,
name: v.name,
min: 0,
max: 100,
step: 0.01,
value: v.value,
})
}
const batch = controller.input.number({ label: ' Batch size ', min: 1, max: 100, value: 10 })
let threshold = null
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/model/categorical_naive_bayes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,32 @@ test('predict', () => {
const acc = accuracy(y, t)
expect(acc).toBeGreaterThan(0.95)
})

test('predict fit twice', () => {
const model = new CategoricalNaiveBayes()
const x = [['a']]
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = 'x'
}

model.fit(x, t)
model.fit(x, t)

const y = model.predict([['a']])
expect(y).toEqual(['x'])
})

test('predict unknown data label', () => {
const model = new CategoricalNaiveBayes()
const x = [['a']]
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = 'x'
}

model.fit(x, t)

const y = model.predict([['b']])
expect(y).toEqual([null])
})
50 changes: 31 additions & 19 deletions tests/lib/model/catmull_rom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,35 @@ test('CatmullRomSplines', () => {
expect(err).toBeLessThan(0.1)
})

test('CentripetalCatmullRomSplines', () => {
const model = new CentripetalCatmullRomSplines()
const x = Matrix.random(20, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)

const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}

const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
describe('CentripetalCatmullRomSplines', () => {
test('default', () => {
const model = new CentripetalCatmullRomSplines()
const x = Matrix.random(20, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)

const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}

const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
})

test('predict second largest value', () => {
const model = new CentripetalCatmullRomSplines()
const x = [0, 2]
const t = [0, 1]
model.fit(x, t)

const y = model.predict([1])
expect(y[0]).toBeCloseTo(0.5)
})
})
25 changes: 18 additions & 7 deletions tests/lib/model/change_finder.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import Matrix from '../../../lib/util/matrix.js'
import ChangeFinder from '../../../lib/model/change_finder.js'

test('anomaly detection', () => {
const model = new ChangeFinder(5)
const n = 50
const x = Matrix.concat(Matrix.random(n, 1, 0, 1), Matrix.random(n, 1, 10, 11)).value
model.fit(x)
const p = model.predict()
expect(p).toHaveLength(100)
describe('anomaly detection', () => {
test('default', () => {
const model = new ChangeFinder()
const n = 50
const x = Matrix.concat(Matrix.random(n, 1, 0, 1), Matrix.random(n, 1, 10, 11)).value
model.fit(x)
const p = model.predict()
expect(p).toHaveLength(100)
})

test('with params', () => {
const model = new ChangeFinder(5)
const n = 50
const x = Matrix.concat(Matrix.random(n, 1, 0, 1), Matrix.random(n, 1, 10, 11)).value
model.fit(x)
const p = model.predict()
expect(p).toHaveLength(100)
})
})
30 changes: 30 additions & 0 deletions tests/lib/model/co_training.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,33 @@ test('semi-classifier', () => {
const acc = accuracy(y, t_org)
expect(acc).toBeGreaterThan(0.95)
})

test('semi-classifier not categorized', () => {
const model = new CoTraining(
{
fit(x, y) {},
predict(x) {
return x.map(v => ({ category: 0, score: 0 }))
},
threshold: 0.5,
},
{
fit(x, y) {},
predict(x) {
return x.map(() => ({ category: 0, score: 0 }))
},
threshold: 0.5,
}
)
const x = [
[0, 0],
[1, 1],
]
const t = [0, null]
model.init(x, t)
for (let i = 0; i < 1; i++) {
model.fit()
}
const y = model.predict(x)
expect(y).toEqual([0, null])
})
40 changes: 28 additions & 12 deletions tests/lib/model/confidence_weighted.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,34 @@ import { ConfidenceWeighted, SoftConfidenceWeighted } from '../../../lib/model/c

import { accuracy } from '../../../lib/evaluate/classification.js'

test('ConfidenceWeighted', () => {
const model = new ConfidenceWeighted(0.9)
const x = Matrix.concat(Matrix.randn(50, 2, 0, 0.2), Matrix.randn(50, 2, 5, 0.2)).toArray()
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.floor(i / 50) * 2 - 1
}
model.init(x, t)
model.fit()
const y = model.predict(x)
const acc = accuracy(y, t)
expect(acc).toBeGreaterThan(0.95)
describe('ConfidenceWeighted', () => {
test('normal eta', () => {
const model = new ConfidenceWeighted(0.9)
const x = Matrix.concat(Matrix.randn(50, 2, 0, 0.2), Matrix.randn(50, 2, 5, 0.2)).toArray()
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.floor(i / 50) * 2 - 1
}
model.init(x, t)
model.fit()
const y = model.predict(x)
const acc = accuracy(y, t)
expect(acc).toBeGreaterThan(0.95)
})

test.each([1, 0.5, 0])('eta %p', eta => {
const model = new ConfidenceWeighted(eta)
const x = Matrix.concat(Matrix.randn(50, 2, 0, 0.2), Matrix.randn(50, 2, 5, 0.2)).toArray()
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.floor(i / 50) * 2 - 1
}
model.init(x, t)
model.fit()
const y = model.predict(x)
const acc = accuracy(y, t)
expect(acc).toBeCloseTo(0.5)
})
})

test.each([1, 2])('SoftConfidenceWeighted %d', version => {
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/model/crf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@ test('fit', () => {
expect(prob).toBeGreaterThan(0.4)
expect(prob).toBeLessThanOrEqual(1)
})

test('fit unknown predict label', () => {
const model = new CRF()
const x = [['a', 'b', 'c']]
const y = [[2, 0, 1]]

for (let i = 0; i < 100; i++) {
model.fit(x, y)
}

const tx = [['a', 'd', 'c']]
const ty = [[2, 0, 1]]
const p = model.predict(tx)
expect(p).toEqual(ty)
const prob = model.probability(tx[0], ty[0])
expect(prob).toBeGreaterThan(0.4)
expect(prob).toBeLessThanOrEqual(1)
})
46 changes: 29 additions & 17 deletions tests/lib/model/cubic_hermite_spline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ import CubicHermiteSpline from '../../../lib/model/cubic_hermite_spline.js'

import { rmse } from '../../../lib/evaluate/regression.js'

test('interpolation', () => {
const model = new CubicHermiteSpline(0, 0)
const x = Matrix.random(50, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)
describe('interpolation', () => {
test('default', () => {
const model = new CubicHermiteSpline(0, 0)
const x = Matrix.random(50, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)

const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}
const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}

const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
})

test('predict second largest value', () => {
const model = new CubicHermiteSpline(0, 0)
const x = [0, 2]
const t = [0, 1]
model.fit(x, t)

const y = model.predict([1])
expect(y[0]).toBeCloseTo(0.5)
})
})
46 changes: 29 additions & 17 deletions tests/lib/model/cubic_interpolation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ import CubicInterpolation from '../../../lib/model/cubic_interpolation.js'

import { rmse } from '../../../lib/evaluate/regression.js'

test('interpolation', () => {
const model = new CubicInterpolation()
const x = Matrix.random(50, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)
describe('interpolation', () => {
test('default', () => {
const model = new CubicInterpolation()
const x = Matrix.random(50, 1, -2, 2).value
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.sin(x[i])
}
model.fit(x, t)

const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}
const y = model.predict(x)
expect(y).toHaveLength(x.length)
for (let i = 0; i < y.length; i++) {
expect(y[i]).toBeCloseTo(t[i])
}

const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
const x0 = Matrix.random(100, 1, -2, 2).value
const y0 = model.predict(x0)
const err = rmse(y0, x0.map(Math.sin))
expect(err).toBeLessThan(0.1)
})

test('predict second largest value', () => {
const model = new CubicInterpolation()
const x = [0, 2]
const t = [0, 1]
model.fit(x, t)

const y = model.predict([1])
expect(y[0]).toBeCloseTo(0.5)
})
})
13 changes: 13 additions & 0 deletions tests/lib/model/dann.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ describe('classification', () => {
const acc = accuracy(y, t)
expect(acc).toBeGreaterThan(0.95)
})

test('same number of class choice', () => {
const model = new DiscriminantAdaptiveNearestNeighbor(2)
const x = [
[-1, -1],
[1, 1],
]
const t = ['a', 'b']

model.fit(x, t)
const y = model.predict([[-1, -1]])
expect(y).toEqual(['a'])
})
})
16 changes: 16 additions & 0 deletions tests/lib/model/denclue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ describe('clustering', () => {
const ri = randIndex(y, t)
expect(ri).toBeGreaterThan(0.9)
})

test('large h', () => {
const model = new DENCLUE(1e6)
const n = 50
const x = [
[0, 0],
[1, 1],
]

model.init(x)
for (let i = 0; i < 10; i++) {
model.fit()
}
const y = model.predict()
expect(y).toEqual([-1, -1])
})
})
Loading