【问题标题】:tensorflow js: Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [28,28,1]tensorflow js:未捕获的错误:检查时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 [28,28,1] 的数组
【发布时间】:2020-05-13 10:39:47
【问题描述】:

我一直在尝试在网站中实现 MNIST 模型。我将第一个卷积层的 input_shape 指定为 (28,28,1) 形状,但是当我将相同形状的张量传递给模型时,我收到此错误,要求 input_shape 为 (null, 28, 28, 1)。 这是我的张量预处理代码:

 let tensor = tf.browser.fromPixels(img).resizeNearestNeighbor([28,28]).toFloat();
 const rgb = tf.tensor1d([0.2989, 0.587, 0.114])
 tensor = tf.sum(tensor.mul(rgb), 2).expandDims(2)

这里tensor.shape的值为[28,28,1]。生成的张量是:

t {isDisposedInternal: false, shape: Array(3), dtype: "float32", size: 784, strides: Array(2), …}

这是模型摘要:

    Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 12, 12, 64)        0         
_________________________________________________________________
dropout (Dropout)            (None, 12, 12, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 9216)              0         
_________________________________________________________________
dense (Dense)                (None, 128)               1179776   
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 129       
=================================================================
Total params: 1,198,721
Trainable params: 1,198,721
Non-trainable params: 0
___________________________________________________

这是我的 model.json 中的一行:

"class_name": "Conv2D",
        "config": {
          "name": "conv2d",
          "trainable": true,
          "batch_input_shape": [
            null,
            28,
            28,
            1
          ],

错误是:

errors.ts:48 Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [28,28,1]
at new e (errors.ts:48)
at Md (training.ts:312)
at e.predict (training.ts:1069)
at e.predict (models.ts:766)
at predict (index.js:68)
at HTMLButtonElement.onclick ((index):34)

【问题讨论】:

    标签: javascript python tensorflow conv-neural-network tensorflow.js


    【解决方案1】:

    张量应该是 4d 张量。通过第一次扩展张量的维度,输出是一个 3d 张量。求和后,输出为 2d。所以张量需要扩大两次或者告诉求和操作保持初始维度。

    tensor = tf.sum(tensor.mul(rgb), 2).expandDims(2).expandDims()
    // or
    tensor = tf.sum(tensor.mul(rgb), 2, true).expandDims()
    

    另一种解决方案是使用重塑

    tensor = tf.sum(tensor.mul(rgb), 2).reshape([1, 28, 28, 1])
    

    【讨论】:

      【解决方案2】:

      @edkeveked 添加到解决方案中,

      const rgb = tf.tensor1d([0.2989, 0.587, 0.114])
      

      参考:Wikipedia

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2020-09-06
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        相关资源
        最近更新 更多