基于 2D CNN 的网络的合适输入形状是什么?
What is the appropriate input shape for a 2D CNN-based network?
我无法将适当的输入形状传递给具有 Conv2D 层的基于 CNN 的网络。
最初,这些是我的火车形状。我的火车数据被重塑为 windows:
X_train: (7,100,5185)= (number of features, window size, number of windows)
y_train= (5185, 100 ) = one labeled column that is also windowed
然后我根据这些数据计算一些重复图,然后我将得到这些形状:
X_train_rp= (5185, 100,100, 7), 100 * 100 referring to my images
y_train = (5185, 100 ), remains unchanged
我将这两个传递给基于 conv2D 的 CNN:
model.add(layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(100, 100, 7)))
我得到这个错误: Data cardinality is ambiguous: x sizes: 100, 100, 100 ......... y sizes: 5185 Make sure all arrays contain the same number of samples.
我尝试了很多形状的组合,但都是徒劳!我做错了什么??
编辑:
这是使用
的模型定义
import tensorflow as tf
X_train_rp = tf.zeros((10, 100,100, 7))
y_train = tf.zeros((10, 100))
#create model
model = tf.keras.Sequential() #add model layers
model.add(tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu',
data_format='channels_last', input_shape=(100, 100, 7)))
model.add(tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(2, activation='softmax'))
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train_rp, y_train_shaped, epochs=3)
model.predict(X_train_rp)
从使用的模块别名来看,我假设您使用带有顺序模型定义的 tensorflow keras 包。您对输入形状的假设实际上是正确的,这个代码片段改编自 keras documentation:
import tensorflow as tf
input_shape = (10, 100, 100, 7)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu', input_shape=input_shape[1:])(x)
print(y.shape)
>>> (10, 98, 98, 64)
这意味着问题出在您的顺序模型定义中。请更新您的问题并包含必要的代码。
编辑:
使用 OP 提供的模型定义并稍加修改即可产生有效的训练过程。问题在于密集层的定义,它将 output
节点作为第一个位置参数而不是输入维度。
为了计算成本,我将训练示例的数量从 (5185) 减少到 (10)...
import tensorflow as tf
X_train_rp = tf.zeros((10, 100,100, 7))
y_train = tf.zeros((10, 100))
#create model
model = tf.keras.Sequential() #add model layers
model.add(tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu',
data_format='channels_last', input_shape=(100, 100, 7)))
model.add(tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'))
model.add(tf.keras.layers.Flatten())
# Here comes the fix:
model.add(tf.keras.layers.Dense(100, activation='softmax'))
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train_rp, y_train, epochs=3)
我无法将适当的输入形状传递给具有 Conv2D 层的基于 CNN 的网络。 最初,这些是我的火车形状。我的火车数据被重塑为 windows:
X_train: (7,100,5185)= (number of features, window size, number of windows)
y_train= (5185, 100 ) = one labeled column that is also windowed
然后我根据这些数据计算一些重复图,然后我将得到这些形状:
X_train_rp= (5185, 100,100, 7), 100 * 100 referring to my images
y_train = (5185, 100 ), remains unchanged
我将这两个传递给基于 conv2D 的 CNN:
model.add(layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(100, 100, 7)))
我得到这个错误: Data cardinality is ambiguous: x sizes: 100, 100, 100 ......... y sizes: 5185 Make sure all arrays contain the same number of samples.
我尝试了很多形状的组合,但都是徒劳!我做错了什么??
编辑: 这是使用
的模型定义import tensorflow as tf
X_train_rp = tf.zeros((10, 100,100, 7))
y_train = tf.zeros((10, 100))
#create model
model = tf.keras.Sequential() #add model layers
model.add(tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu',
data_format='channels_last', input_shape=(100, 100, 7)))
model.add(tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(2, activation='softmax'))
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train_rp, y_train_shaped, epochs=3)
model.predict(X_train_rp)
从使用的模块别名来看,我假设您使用带有顺序模型定义的 tensorflow keras 包。您对输入形状的假设实际上是正确的,这个代码片段改编自 keras documentation:
import tensorflow as tf
input_shape = (10, 100, 100, 7)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu', input_shape=input_shape[1:])(x)
print(y.shape)
>>> (10, 98, 98, 64)
这意味着问题出在您的顺序模型定义中。请更新您的问题并包含必要的代码。
编辑:
使用 OP 提供的模型定义并稍加修改即可产生有效的训练过程。问题在于密集层的定义,它将 output
节点作为第一个位置参数而不是输入维度。
为了计算成本,我将训练示例的数量从 (5185) 减少到 (10)...
import tensorflow as tf
X_train_rp = tf.zeros((10, 100,100, 7))
y_train = tf.zeros((10, 100))
#create model
model = tf.keras.Sequential() #add model layers
model.add(tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu',
data_format='channels_last', input_shape=(100, 100, 7)))
model.add(tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'))
model.add(tf.keras.layers.Flatten())
# Here comes the fix:
model.add(tf.keras.layers.Dense(100, activation='softmax'))
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train_rp, y_train, epochs=3)