如何解决 1 维 CNN 输入形状错误?
How to Solve 1 Dimensional CNN Input Shape Error?
我正在尝试训练一维 CNN。我的输入是一个有 476 行和 4 列的 numpy 数组。我不知道如何设置输入形状。我还尝试将输入重塑为 (476, 4, 1) 但仍然出现错误。 Train_labels的形状是(476, )。 O是train数据,0[0].shape是(4,)下面是代码、错误和数据。
ValueError: Input 0 of layer "sequential_17" is incompatible with the layer: expected shape=(None, 476, 4), found shape=(1, 4, 1)
Traceback (most recent call last)
<ipython-input-363-440de758fd68> in <module>()
10
11
---> 12 model.fit(o, train_labels, epochs=5, batch_size=1)
13 print(model.evaluate(o, train_labels))
1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
model=Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(476,4)))
model.add(Conv1D(filters=16, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))
下面是模型结构。
Model: "sequential_34"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_56 (Conv1D) (None, 474, 32) 416
conv1d_57 (Conv1D) (None, 472, 16) 1552
dropout_19 (Dropout) (None, 472, 16) 0
max_pooling1d_19 (MaxPoolin (None, 236, 16) 0
g1D)
flatten_19 (Flatten) (None, 3776) 0
dense_40 (Dense) (None, 50) 188850
dense_41 (Dense) (None, 2) 102
Total params: 190,920
Trainable params: 190,920
Non-trainable params: 0
我的数据是一个有 476 行和 4 列的 numpy 数组。它的形状是 (476,4).
[[0.35603836 0.6439616 0.49762452 0.5023755 ]
[0.12395032 0.87604964 0.49762452 0.5023755 ]
[0.5605615 0.43943852 0.49762452 0.5023755 ]
...
[0.6250699 0.37493005 0.48114303 0.51885694]
[0.6650569 0.33494312 0.48114303 0.51885694]
[0.53505033 0.46494964 0.48114303 0.51885694]]
我自己解决了这个问题,想为遇到同样问题的任何人提供答案。
这是我应用的步骤:
- 我使用了稀疏分类交叉熵。
- 我将输入形状更改为(属性数,1)
- 我将 CNN 内核大小从 3 更改为 2
model=Sequential()
model.add(Conv1D(filters=8, kernel_size=2, activation='relu', input_shape=(4,1)))
model.add(Conv1D(filters=4, kernel_size=2, activation='relu'))
model.add(Dropout(0.3))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(20, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))
我正在尝试训练一维 CNN。我的输入是一个有 476 行和 4 列的 numpy 数组。我不知道如何设置输入形状。我还尝试将输入重塑为 (476, 4, 1) 但仍然出现错误。 Train_labels的形状是(476, )。 O是train数据,0[0].shape是(4,)下面是代码、错误和数据。
ValueError: Input 0 of layer "sequential_17" is incompatible with the layer: expected shape=(None, 476, 4), found shape=(1, 4, 1)
Traceback (most recent call last)
<ipython-input-363-440de758fd68> in <module>()
10
11
---> 12 model.fit(o, train_labels, epochs=5, batch_size=1)
13 print(model.evaluate(o, train_labels))
1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
model=Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(476,4)))
model.add(Conv1D(filters=16, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))
下面是模型结构。
Model: "sequential_34"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_56 (Conv1D) (None, 474, 32) 416
conv1d_57 (Conv1D) (None, 472, 16) 1552
dropout_19 (Dropout) (None, 472, 16) 0
max_pooling1d_19 (MaxPoolin (None, 236, 16) 0
g1D)
flatten_19 (Flatten) (None, 3776) 0
dense_40 (Dense) (None, 50) 188850
dense_41 (Dense) (None, 2) 102
Total params: 190,920
Trainable params: 190,920
Non-trainable params: 0
我的数据是一个有 476 行和 4 列的 numpy 数组。它的形状是 (476,4).
[[0.35603836 0.6439616 0.49762452 0.5023755 ]
[0.12395032 0.87604964 0.49762452 0.5023755 ]
[0.5605615 0.43943852 0.49762452 0.5023755 ]
...
[0.6250699 0.37493005 0.48114303 0.51885694]
[0.6650569 0.33494312 0.48114303 0.51885694]
[0.53505033 0.46494964 0.48114303 0.51885694]]
我自己解决了这个问题,想为遇到同样问题的任何人提供答案。
这是我应用的步骤:
- 我使用了稀疏分类交叉熵。
- 我将输入形状更改为(属性数,1)
- 我将 CNN 内核大小从 3 更改为 2
model=Sequential()
model.add(Conv1D(filters=8, kernel_size=2, activation='relu', input_shape=(4,1)))
model.add(Conv1D(filters=4, kernel_size=2, activation='relu'))
model.add(Dropout(0.3))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(20, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(o, train_labels, epochs=5, batch_size=1)
print(model.evaluate(o, train_labels))