Mnist example without flatten layer

129 views
Skip to first unread message

Kids Wong

unread,
Jul 31, 2018, 2:44:48 AM7/31/18
to TensorFlow.js Discussion
Hi, 

Because I want to export the model to CMSIS-NN, I can't use the flatten layer.

In the caffe tutorial, two fully connected layers are used.
So I want to implement the code in tf.js.

this is my code:

const model = tf.sequential();
model.add(tf.layers.conv2d({
      inputShape: [setting.width, setting.height, setting.depth],
      kernelSize: 5,
      filters: 8,
      strides: 1,
      activation: 'relu',
      kernelInitializer: 'varianceScaling'
    }));
model.add(tf.layers.maxPooling2d({poolSize: [2, 2], strides: [2, 2]}));

model.add(tf.layers.conv2d({
        kernelSize: 5,
        filters: 16,
        strides: 1,
        activation: 'relu',
        kernelInitializer: 'varianceScaling'
        }));
model.add(tf.layers.maxPooling2d({poolSize: [2, 2], strides: [2, 2]}));

  
//model.add(tf.layers.flatten());

model.add(tf.layers.dense(
        {units: 256, kernelInitializer: 'varianceScaling', activation: 'relu'}));

model.add(tf.layers.dense(
      {units: 10, kernelInitializer: 'varianceScaling', activation: 'softmax'}));

const optimizer = tf.train.sgd(0.15);
model.compile({
      optimizer: optimizer,
      loss: 'categoricalCrossentropy',
      metrics: ['accuracy'],
    });

I just remove flatten layer and add another dense layer. But the console error:

errors.ts:49 Uncaught (in promise) Error: Error when checking target: expected dense_Dense2 to have 4 dimension(s). but got array with shape 64,10
    at new ValueError (errors.ts:49)
    at standardizeInputData (training.ts:155)
    at Model.standardizeUserData (training.ts:1273)
    at Model.<anonymous> (training.ts:1629)
    at step (/Users/wangkaizhi/Desktop/trainer/trainer/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:42)
    at Object.next (/Users/wangkaizhi/Desktop/trainer/trainer/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:23)
    at /Users/wangkaizhi/Desktop/trainer/trainer/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:17
    at new Promise (<anonymous>)
    at __awaiter (/Users/wangkaizhi/Desktop/trainer/trainer/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:13)
    at Model.fit (training.ts:1624)

How can I solve this problem?

kyuHyun Cho

unread,
Jul 31, 2018, 3:00:10 AM7/31/18
to TensorFlow.js Discussion
Flatten layer means that unfolding in one dimension.
Output shape in the previous layer of the commented layer is 2-dimension. But, It is must 1-dimension to use next dense layer. So, Flatten layer is located cond2d between dense. If you really don't want to use dense layer, separate your model in order to reshape first model's output to 1-d tensor and input second model's input.
Reply all
Reply to author
Forward
0 new messages