Skip to content

Custom Layer incompatible dimensions #8676

@weingaunity

Description

@weingaunity

Hi, i try to realize a custom layer (a dense layer with bias and relu activation function only). I implemented the demonstration setup in a single page web-app and end up with following error when i start training:

Uncaught (in promise) Error: Input 0 is incompatible with layer dense_Dense1: expected min_ndim=2, found ndim=1.

Can you give me some advice or resources where different kind of custom layers are implemented in javascript (not typescript).

Here the complete web-app: index.html

Here is the custom layer i created:

class FixedRampLayer extends tf.layers.Layer {
  constructor(config) {
    super(config);
    this.units = config.units;
  }
  computeOutputShape(inputShape) {
    return [inputShape[0], this.units];
  }

  build(inputShape) {
    this.bias = this.addWeight(
      'bias',
      [this.units],
      'float32',
      tf.initializers.randomUniform({minval: -1, maxval: 1})
    );
  }

  call(inputs) {
    return tf.relu(tf.add(inputs, this.bias.read()));
  }

  static get className() {
    return 'FixedRampLayer';
  }
}

Here the model:

const model = tf.sequential();
model.add(new FixedRampLayer({units: 10, inputShape: [1]}));             
model.add(tf.layers.dense({units: 1, useBias:true}));

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions