ValueError:尺寸必须相等,输入形状



我已经编写了这段代码。我的输入形状是(100 x100 X3)。我是深度学习的新手。我花了很多时间在这个问题上,但是无法解决这个问题。如有任何帮助,不胜感激。

init = tf.random_normal_initializer(mean=0.0, stddev=0.05, seed=None)
input_image=Input(shape=image_shape)

# input: 100x100 images with 3 channels -> (3, 100, 100) tensors.
# this applies 32 convolution filters of size 3x3 each.
model=Sequential()
model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
padding='same', input_shape=(3,100,100)))
model.add(Activation('relu'))
model.add(Conv2D(filters=32,kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding="same"))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32,kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(Conv2D(filters=32, kernel_size=(3, 3),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding="same"))
model.add(Dropout(0.25))
model.add(Flatten())
# Note: Keras does automatic shape inference.
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
len(model.weights)
model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])

错误:In [15]: runfile('/user/Project/SM/src/ann_algo_keras.py', wdir='/user/Project/SM/src')随机起始突触权重:模型:"sequential_3">


图层(类型)输出形状参数#

conv2d_12 (Conv2D) (None, 3,100,16) 14416


activation_18 (Activation) (None, 3,100, 16) 0


conv2d_13 (Conv2D) (None, 3,100,32) 4640


activation_19 (Activation) (None, 3,100,32) 0


max_pooling2d_6 (MaxPooling2 (None, 2,50,32) 0


dropout_9 (Dropout) (None, 2,50,32) 0


conv2d_14 (Conv2D) (None, 2,50,32) 9248


activation_20 (Activation) (None, 2,50,32) 0


conv2d_15 (Conv2D) (None, 2,50,32) 9248


activation_21 (Activation) (None, 2,50,32) 0


max_pooling2d_7 (MaxPooling2 (None, 1,25,32) 0


dropout_10 (Dropout) (None, 1,25,32) 0


flat_3 (Flatten) (None, 800) 0


dense_6 (Dense) (None, 256) 205056


activation_22 (Activation) (None, 256) 0


dropout_11 (Dropout) (None, 256) 0


dense_7 (Dense) (None, 10) 2570


activation_23 (Activation) (None, 10) 0

总参数:245,178可训练参数:245,178不可训练参数:0


时代1/2000回溯(最近一次调用):

File "/user/Project/SM/src/ann_algo_keras.py&quot火车(输入、输出、image_shape)

File "/user/Project/SM/src/ann_algo_keras.py",第204行,in train模型。fit(X_test, y_train, batch_size, epochs, validation_data=(X_test, y_test), use_multiprocessing=True)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py",第108行,_method_wrapper .py&quot返回方法(self, *args, **kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py",第1098行,in fitTmp_logs = train_function(iterator)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",第780行,在中调用结果=自我。* * kwds _call (* args)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",第823行,在_call自我。_initialize(args, kwds, add_initializers_to=initializers)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",第696行,在_initializeself._stateful_fn。_get_concrete_function_internal_garbage_collect (# pylint: disable=protected-access

)File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py",第2855行,在_get_concrete_function_internal_garbage_collectedGraph_function, _, _ = self。_maybe_define_function (args, kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py",第3213行,在_maybe_define_functionGraph_function = self。_create_graph_function (args, kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py",第3065行,在_create_graph_functionfunc_graph_module.func_graph_from_py_func (

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py",第986行,在func_graph_from_py_funcFunc_outputs = python_func(*func_args, **func_kwargs)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",第600行,在wrapped_fn包装返回weak_wrapped_fn()。(* * kwds * args)

File "/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py",第973行,在wrapper中提高e.ag_error_metadata.to_exception (e)

ValueError: in user code:

/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
return step_function(self, iterator)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
outputs = model.train_step(data)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:748 train_step
loss = self.compiled_loss(
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/home/catherin/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:149 __call__
losses = ag_call(y_true, y_pred)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:253 call  **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/keras/losses.py:1195 mean_squared_error
return K.mean(math_ops.squared_difference(y_pred, y_true), axis=-1)
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py:10398 squared_difference
_, _, _op, _outputs = _op_def_library._apply_op_helper(
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:742 _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:591 _create_op_internal
return super(FuncGraph, self)._create_op_internal(  # pylint: disable=protected-access
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:3477 _create_op_internal
ret = Operation(
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1974 __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
/home/user/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 10 and 10000 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](sequential_3/activation_23/Softmax, IteratorGetNext:1)' with input shapes: [?,10], [?,1,10000].

只是把通道在输入形状中的位置搞混了。在Keras中,输入形状应该是HxWxC,而不是PyTorch中的CxHxW

model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
padding='same', input_shape=(100,100,3)))

您的输入顺序不对,通道应该在最后。

model.add(Conv2D(filters=16, kernel_size=(3, 3),kernel_initializer=init,
padding='same', input_shape=(100,100,3)))

我还假设您正在尝试进行分类。还有一些指标是用于回归的,比如"mae"。您可以将它们更改为:

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["acc"])

相关内容

  • 没有找到相关文章

最新更新