keras - 在 Keras 中的密集层之后重塑层
问题描述
我试图理解为什么密集层和重塑层之间存在不匹配的维度。这段代码不应该是正确的吗?Dense Layer输出的维数会是image_resize^2 * 128,为什么reshape有冲突?
input_shape = (28,28,1)
inputs = Input(shape=input_shape)
image_size = 28
image_resize = image_size // 4
x = Dense(image_resize * image_resize * 128)(inputs)
x = Reshape((image_resize, image_resize, 128))(x)
这是显示的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 474, in __call__
output_shape = self.compute_output_shape(input_shape)
File "/Users/venv/lib/python3.7/site-packages/keras/layers/core.py", line 398, in compute_output_shape
input_shape[1:], self.target_shape)
File "/Users/venv/lib/python3.7/site-packages/keras/layers/core.py", line 386, in _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged
解决方案
Dense
图层作用于输入数据的最后一个维度,如果要将图像输入给Dense
图层,则应首先将其展平:
x = Flatten()(x)
x = Dense(image_resize * image_resize * 128)(x)
x = Reshape((image_resize, image_resize, 128))(x)
然后Reshape
意志起作用。
推荐阅读
- laravel - nWidart/laravel-modules,如何将模块放在不同的文件夹下
- python - 用杂乱的数据替换列中的某些值
- java - 是否可以使用 Scanner 调用方法?
- assembly - 单个指令及其输入操作数如何存储在内存中?
- mongodb - 在 $lookup 结果字段中使用 $near 和 $geometry 搜索位置
- php - 在 Codeigniter 中使用哈希密码登录
- javascript - 如何根据 url 参数隐藏/显示元素
- pentaho - pentaho 和任务计划窗口的输出文件创建错误
- c# - PrecompiledAssemblyException:多个同名UnityEditor的预编译程序集
- python - 添加尾随“0”以使列表的每个部分都有 5 个字符