【发布时间】: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