CNN 不兼容

我的数据具有以下形状:

 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)    print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)(942, 32, 32, 1) (236, 32, 32, 1) (942, 3, 3) (236, 3, 3)

每当我尝试运行我的CNN时,我都会得到以下错误:

from tensorflow.keras import layersfrom tensorflow.keras import Modelimg_input = layers.Input(shape=(32, 32, 1))x = layers.Conv2D(16, (3,3), activation='relu', strides = 1, padding = 'same')(img_input)x = layers.Conv2D(32, (3,3), activation='relu', strides = 2)(x)x = layers.Conv2D(128, (3,3), activation='relu', strides = 2)(x)x = layers.MaxPool2D(pool_size=2)(x)x = layers.Conv2D(3, 3, activation='linear', strides = 2)(x)output = layers.Flatten()(x)model = Model(img_input, output)model.summary()model.compile(loss='mean_squared_error',optimizer= 'adam', metrics=['mse'])history = model.fit(X_train,Y_train,validation_data=(X_test, Y_test), epochs = 100,verbose=1)

错误:

InvalidArgumentError:  Incompatible shapes: [32,3] vs. [32,3,3]     [[node BroadcastGradientArgs_2 (defined at /usr/local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_distributed_function_7567]Function call stack:distributed_function

我在这里遗漏了什么?


回答:

你没有在网络内部正确处理维度。首先扩展你的y的维度,以便它们采用这种格式(n_sample, 3, 3, 1)。在这一点上调整网络(我移除了flatten和max pooling,并调整了最后的conv输出)

# create dummy datan_sample = 10X = np.random.uniform(0,1, (n_sample, 32, 32, 1))y = np.random.uniform(0,1, (n_sample, 3, 3))# expand y dimy = y[...,np.newaxis]print(X.shape, y.shape)img_input = Input(shape=(32, 32, 1))x = Conv2D(16, (3,3), activation='relu', strides = 1, padding = 'same')(img_input)x = Conv2D(32, (3,3), activation='relu', strides = 2)(x)x = Conv2D(128, (3,3), activation='relu', strides = 2)(x)x = Conv2D(1, (3,3), activation='linear', strides = 2)(x)model = Model(img_input, x)model.summary()model.compile(loss='mean_squared_error',optimizer= 'adam', metrics=['mse'])model.fit(X,y, epochs=3)

Related Posts

神经网络反向传播代码不工作

我需要编写一个简单的由1个输出节点、1个包含3个节点的…

值错误:y 包含先前未见过的标签:

我使用了 决策树分类器,我想将我的 输入 作为 字符串…

使用不平衡数据集进行特征选择时遇到的问题

我正在使用不平衡数据集(54:38:7%)进行特征选择…

广义随机森林/因果森林在Python上的应用

我在寻找Python上的广义随机森林/因果森林算法,但…

如何用PyTorch仅用标量损失来训练神经网络?

假设我们有一个神经网络,我们希望它能根据输入预测三个值…

什么是RNN中间隐藏状态的良好用途?

我已经以三种不同的方式使用了RNN/LSTM: 多对多…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注