python - 为向量构建 keras CNN
问题描述
我是 ML 的初学者,正在尝试解决问题。我得到N
向量作为输入。每个向量的长度为10
。我得到了N
0/1 的标签。我需要在这些数据上训练一个 CNN(按提到的顺序):
- 卷积层
- 最大池化
- 致密层
- 获取二进制输出的 Softmax 激活
我做了:
def get_model(inputShape, filters = 32, kernel_size = 3, pool_size = 4, strides = 1):
model = models.Sequential()
model.add(layers.Conv1D(filters, kernel_size))
model.add(layers.MaxPooling1D(pool_size, strides))
model.add(layers.Dense(1))
model.add(layers.Activation(activation='softmax'))
model.build(inputShape)
print(model.summary())
return model
model = get_model((None, None, 10))
然后,我测试我的模型是否正确构建,我这样做:(未经培训)
x = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
x = x.reshape(1, x.shape[0], x.shape[1])
print(model.predict(x))
我得到这个输出/错误:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, None, 32) 992
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, None, 32) 0
_________________________________________________________________
dense_1 (Dense) (None, None, 1) 33
_________________________________________________________________
activation_1 (Activation) (None, None, 1) 0
=================================================================
Total params: 1,025
Trainable params: 1,025
Non-trainable params: 0
_________________________________________________________________
None
2020-06-30 19:44:01.763782: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at pooling_ops_common.cc:91 : Invalid argument: Computed output size would be negative: -3 [input_size: 0, effective_filter_size: 4, stride: 1]
Traceback (most recent call last):
File "model_predict.py", line 41, in <module>
model.predict(x)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1458, in predict
return training_arrays.predict_loop(self, f, ins,
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training_arrays.py", line 324, in predict_loop
batch_outs = f(ins_batch)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/backend.py", line 3792, in __call__
outputs = self._graph_fn(*converted_inputs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/function.py", line 1605, in __call__
return self._call_impl(args, kwargs)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/function.py", line 1645, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/function.py", line 1745, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/function.py", line 593, in call
outputs = execute.execute(
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Computed output size would be negative: -3 [input_size: 0, effective_filter_size: 4, stride: 1]
[[node max_pooling1d_1/MaxPool (defined at /usr/local/lib/python3.8/dist-packages/keras/backend/tensorflow_backend.py:3007) ]] [Op:__inference_keras_scratch_graph_136]
Function call stack:
keras_scratch_graph
请建议我如何纠正这个问题。
解决方案
问题在于x
. Conv1D(kernel_size=3)
它对于和来说太小了MaxPooling1D(pool_size=4)
。
x
我为and reduce kernel_size
and添加了一个维度pool_size
:
x = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=np.float32)
x = x.reshape(1, x.shape[0], x.shape[1])
model = Sequential()
model.add(layers.Conv1D(filters=10, kernel_size=2))
model.add(layers.MaxPooling1D(pool_size=2, strides=1))
model.add(layers.Dense(1))
model.add(layers.Activation(activation='softmax'))
model.build((None, None, 10))
model.predict(x)
如果您需要,我可以分享指向 google colab 文件的链接。
推荐阅读
- r - 通过比较 R 中的其他两列来选择组
- graphql - 在 GraphQL 中对 Join 表进行批量更新的最佳方法是什么?
- reactjs - 如何加快组件和类的 npm 库的 webpack 重建
- r - 在 Windows [rstudio] [r] [windows] 中使用 R(或特别是 R Studio)在指定文件夹中打开文件资源管理器
- c - 为什么在 http 服务器中需要异步?
- dns - 无法连接到在 AWS lightail windows server 2019 上设置的域控制器
- makefile - 配置make在不同目录下为不同系统构建对象
- python - 如何在 Python 上的 3d 数组上应用高斯分布
- php - 如何从数据库中加载 user_email 并将其发布到 Php wp_mail() 函数的 $to 中
- javascript - 下拉列表填充相同的值