Skip to content

Enhance InputLayer to support default values and update tests #948

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

Merged
merged 1 commit into from
Apr 12, 2025
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
2 changes: 1 addition & 1 deletion lib/model/nns/layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export { default as VarLayer } from './variance.js'
* { type: 'huber' } |
* { type: 'identity' } |
* { type: 'include', net: NeuralNetwork | object[], input_to?: string, train?: boolean } |
* { type: 'input', name?: string, size?: number[] } |
* { type: 'input', name?: string, size?: number[], value?: number | number[] | number[][] | nunber[][][] | number[][][][] | Matrix | Tensor } |
* { type: 'is_inf' } |
* { type: 'is_nan' } |
* { type: 'isigmoid', a?: number, alpha?: number } |
Expand Down
8 changes: 7 additions & 1 deletion lib/model/nns/layer/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export default class InputLayer extends Layer {
* @param {object} config object
* @param {string} [config.name] Name of the layer
* @param {number[]} [config.size] Size of the layer
* @param {number | number[] | number[][] | nunber[][][] | number[][][][] | Matrix | Tensor} [config.value] Default value
*/
constructor({ name = null, size = null, ...rest }) {
constructor({ name = null, size = null, value, ...rest }) {
super(rest)
this._name = name
this._size = size
this._value = value
}

bind({ input }) {
Expand All @@ -27,6 +29,9 @@ export default class InputLayer extends Layer {
) {
input = input[this._name]
}
if (input == null) {
input = this._value
}
if (Array.isArray(input)) {
this._o = Tensor.fromArray(input)
if (this._o.dimension === 2) {
Expand Down Expand Up @@ -57,6 +62,7 @@ export default class InputLayer extends Layer {
type: 'input',
name: this._name,
size: this._size?.concat(),
value: this._value instanceof Matrix || this._value instanceof Tensor ? this._value.toArray() : this._value,
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/model/nns/layer/input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ describe('layer', () => {
}
}
})

test('default value', () => {
const x = 1
const layer = new InputLayer({ value: x })

layer.bind({})
const y = layer.calc()
expect(y.sizes).toEqual([1, 1])
expect(y.at(0, 0)).toBe(1)
})

test('default value and bind value', () => {
const x = 1
const layer = new InputLayer({ value: x })

layer.bind({ input: 2 })
const y = layer.calc()
expect(y.sizes).toEqual([1, 1])
expect(y.at(0, 0)).toBe(2)
})
})

describe('grad', () => {
Expand Down Expand Up @@ -135,6 +155,14 @@ describe('layer', () => {
const obj = layer.toObject()
expect(obj).toEqual({ type: 'input', name: 'in', size: [null, 10] })
})

test('matrix value', () => {
const mat = Matrix.randn(1, 10)
const layer = new InputLayer({ name: 'in', size: [null, 10], value: mat })

const obj = layer.toObject()
expect(obj).toEqual({ type: 'input', name: 'in', size: [null, 10], value: mat.toArray() })
})
})

test('fromObject', () => {
Expand Down