在 Keras 的 Conv2D 和 Dense 期间数据形状如何变化?
How does data shape change during Conv2D and Dense in Keras?
正如标题所说。此代码仅适用于使用:
x = Flatten()(x)
在卷积层和密集层之间。
import numpy as np
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Input
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
# Generate dummy data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
#Build Model
input_layer = Input(shape=(100, 100, 3))
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
x = Dense(256, activation='relu')(x)
x = Dense(10, activation='softmax')(x)
model = Model(inputs=[input_layer],outputs=[x])
#compile network
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
#train network
model.fit(x_train, y_train, batch_size=32, epochs=10)
否则,我会收到此错误:
Traceback (most recent call last):
File "/home/michael/practice_example.py", line 44, in <module>
model.fit(x_train, y_train, batch_size=32, epochs=10)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1435, in fit
batch_size=batch_size)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1315, in _standardize_user_data
exception_prefix='target')
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 127, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (100, 10)
为什么没有 flatten()
层输出会有 4 个维度?
根据 keras 文档,
Conv2D Output shape
4D tensor with shape: (samples, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (samples, new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding.
由于您使用的是 channels_last
,图层输出的形状将是:
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Flatten()(x)
# shape=(100, row*col*32)
x = Dense(256, activation='relu')(x)
# shape=(100, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, 10)
错误解释(已编辑,感谢@Marcin)
使用 Dense
层将 4D 张量 (shape=(100, row, col, 32)) 链接到 2D 张量 (shape=(100, 256)) 仍将形成 4D 张量(shape =(100, row, col, 256)) 这不是你想要的。
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Dense(256, activation='relu')(x)
# shape=(100, row, col, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, row, col, 10)
并且当输出4D张量和目标2D张量不匹配时会出现错误。
这就是为什么您需要一个 Flatten
图层来将其从 4D 平面化为 2D 的原因。
参考
从 Dense
文档中可以读到,如果 Dense
的输入有两个以上的维度 - 它仅应用于最后一个维度 - 并保留所有其他维度:
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Dense(256, activation='relu')(x)
# shape=(100, row, col, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, row, col, 10)
这就是为什么要达到 4d
目标的原因。
正如标题所说。此代码仅适用于使用:
x = Flatten()(x)
在卷积层和密集层之间。
import numpy as np
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Input
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
# Generate dummy data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
#Build Model
input_layer = Input(shape=(100, 100, 3))
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
x = Dense(256, activation='relu')(x)
x = Dense(10, activation='softmax')(x)
model = Model(inputs=[input_layer],outputs=[x])
#compile network
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
#train network
model.fit(x_train, y_train, batch_size=32, epochs=10)
否则,我会收到此错误:
Traceback (most recent call last):
File "/home/michael/practice_example.py", line 44, in <module>
model.fit(x_train, y_train, batch_size=32, epochs=10)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1435, in fit
batch_size=batch_size)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1315, in _standardize_user_data
exception_prefix='target')
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 127, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (100, 10)
为什么没有 flatten()
层输出会有 4 个维度?
根据 keras 文档,
Conv2D Output shape
4D tensor with shape: (samples, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (samples, new_rows, new_cols, filters) if data_format='channels_last'. rows and cols values might have changed due to padding.
由于您使用的是 channels_last
,图层输出的形状将是:
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Flatten()(x)
# shape=(100, row*col*32)
x = Dense(256, activation='relu')(x)
# shape=(100, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, 10)
错误解释(已编辑,感谢@Marcin)
使用 Dense
层将 4D 张量 (shape=(100, row, col, 32)) 链接到 2D 张量 (shape=(100, 256)) 仍将形成 4D 张量(shape =(100, row, col, 256)) 这不是你想要的。
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Dense(256, activation='relu')(x)
# shape=(100, row, col, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, row, col, 10)
并且当输出4D张量和目标2D张量不匹配时会出现错误。
这就是为什么您需要一个 Flatten
图层来将其从 4D 平面化为 2D 的原因。
参考
从 Dense
文档中可以读到,如果 Dense
的输入有两个以上的维度 - 它仅应用于最后一个维度 - 并保留所有其他维度:
# shape=(100, 100, 100, 3)
x = Conv2D(32, (3, 3), activation='relu')(input_layer)
# shape=(100, row, col, 32)
x = Dense(256, activation='relu')(x)
# shape=(100, row, col, 256)
x = Dense(10, activation='softmax')(x)
# shape=(100, row, col, 10)
这就是为什么要达到 4d
目标的原因。