python - K.zeros_like(x) 的 K.int_shape
问题描述
这是我的自定义填充层:
class CustomZeroPadding2D(Layer):
def __init__(self, **kwargs):
super(CustomZeroPadding2D, self).__init__(**kwargs)
def build(self, input_shape):
super(CustomZeroPadding2D, self).build(input_shape)
def call(self, x):
print('K.int_shape(x)', K.int_shape(x))
print('K.int_shape(K.zeros_like(x))', K.int_shape(K.zeros_like(x)))
res = concatenate([x, K.zeros_like(x)], axis=-1)
return res
def compute_output_shape(self, input_shape):
output_shape = (input_shape[0], input_shape[1], input_shape[2]*2)
return output_shape
由于某些原因:
K.int_shape(x) (None, 128, 128, 7)
但
K.int_shape(K.zeros_like(x)) (None, None, None, 7)
在doc instantiates an all-zeros variable of the same shape as another tensor
中,有什么问题?
更新:
串联不起作用的问题:
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 128, 128, 7), (None, None, None, 7)]
解决方案
没有任何错误。
如果您能够与 连接axis=-1
,则可以确定所有三个第一个维度都是相等的。
现在,在 tensorflow 和/或 keras 中可能存在内部怪癖,也许是为了让事情变得更快,也许是为了让它们灵活地适应可变大小。没什么大不了的。
如果您想要当前值的真实形状,则需要评估 ( K.eval()
)K.shape(x)
张量。但是评估不能在层内进行。它必须像预测一样进行。
错误信息
您正在使用连接层。你应该使用keras.backend.concatenate([...], axis=-1)
推荐阅读
- python - 错误:编译 OpenCV 4.4 /usr/bin/ld 找不到 -ltrue, cuda 11.1, cudnn 8.0.5 gtx 950m
- javascript - 如何删除导航栏上图像幻灯片的重叠?
- ansible - Ansible 根据输出中的内容运行任务
- java - java.lang.IllegalArgumentException:比较方法违反了它的通用合同 Java 1.8
- scheduler - 当文件到达服务器时如何启动 DataStage Sequence 作业
- r - 如何在 R 中重用长链管道操作符的一部分?
- c# - 试图将 JSON 反序列化为字典
> 在 C# 中 - docker - 有没有没有命名空间和 cgroup 的容器运行?
- node.js - 未处理 Stripe Webhook 案例
- reactjs - 如何从 React Native 在 Menu Drawer 上设置 SafeAreaView?