我的数据具有以下形状:
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)