python - ValueError:Dimensions must be equal,但在自制层中是64和32
问题描述
我在谷歌 colab 笔记本中使用 tensorflow 2.2.0。我有一些自制的激活层,但是当我尝试使用那个特定的激活层时问题就来了:
class MPELU(tf.keras.layers.Layer):
"""
Multiple Parametric Exponential Linear Units.
"""
def __init__(self, channel_wise=True, **kwargs):
super(MPELU, self).__init__(**kwargs)
self.channel_wise = channel_wise
def build(self, input_shape):
shape = [1]
if self.channel_wise:
shape = [int(input_shape[-1])] # Number of channels
self.alpha = self.add_weight(name='alpha', shape=shape, dtype=K.floatx(),
initializer=tf.keras.initializers.RandomUniform(minval=-1, maxval=1),
trainable=True)
self.beta = self.add_weight(name='beta', shape=shape, dtype=K.floatx(),
initializer=tf.keras.initializers.RandomUniform(minval=0.0, maxval=1),
trainable=True)
# Finish buildidng
super(MPELU, self).build(input_shape)
@tf.function
def call(self, inputs, **kwargs):
positive = tf.keras.activations.relu(inputs)
negative = self.alpha * (K.exp(-tf.keras.activations.relu(-inputs) * cons_greater_zero(self.beta)) - 1)
return positive + negative
def compute_output_shape(self, input_shape):
return input_shape
错误代码如下:
ValueError: Dimensions must be equal, but are 64 and 32 for '{{node mpelu/mul_10}} = Mul[T=DT_FLOAT](mpelu/Neg_11, mpelu/add_10)' with input shapes: [?,4,4,64], [32].
我使用打印功能在每次调用中打印形状,结果如下:
(None, 16, 16, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 7, 7, 32)
(None, 4, 4, 64)
解决方案
推荐阅读
- sql-server - 如何使用存储过程访问插入和删除的逻辑表
- java - 如何找出使 poi 损坏 xlsx / xlsm 文件的原因
- java - 资源编译器错误:尝试运行 JUnit 测试时拒绝访问
- sql - 嵌套多个相同的选择查询和重用,无需第二次往返数据库
- c# - 如何对使用 Console 类的类进行单元测试?
- javascript - 如何从 templateResult 获取对创建的 DOM 的引用(不使用自定义元素)
- c# - RadComboBox 如何改变 Selected Item 的外观?
- python - 通过将通用名称保留在没有 nan 的同一行上来连接数据框
- routes - 带有link_to的rails的奇怪行为
- javascript - Knoockout 计算的 observable 不更新