正则化器导致“ValueError:Shapes must be equal rank”

Posted

技术标签:

【中文标题】正则化器导致“ValueError:Shapes must be equal rank”【英文标题】:regularizer causes "ValueError: Shapes must be equal rank" 【发布时间】:2021-05-04 07:50:00 【问题描述】:

尝试运行时

import numpy as np
import keras

X = np.ones((100,20))
Y1 = np.ones((100,5))
Y2 = np.ones((100,4))

Input_1= keras.layers.Input(shape=X.shape[1])

x = keras.layers.Dense(100)(Input_1)
x = keras.layers.Dense(100)(x)

out1 = keras.layers.Dense(5, kernel_regularizer='l1')(x)
out2 = keras.layers.Dense(4)(x)

model = keras.models.Model(inputs=Input_1, outputs=[out1,out2])
model.compile(loss = 'mse', loss_weights=np.arange(2))

model.fit(X, [Y1, Y2], epochs=2)

我明白了

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 火车功能 * 返回 step_function(自我,迭代器) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function ** 输出 = model.distribute_strategy.run(run_step, args=(data,)) /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 跑步 返回 self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica 返回 fn(*args, **kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step ** 输出 = model.train_step(数据) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:756 train_step y, y_pred, sample_weight, regularization_losses=self.losses) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:236 致电 total_loss_metric_value = math_ops.add_n(loss_metric_values) /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py​​:201 包装 返回目标(*args,**kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3572 add_n 返回 gen_math_ops.add_n(输入,名称=名称) /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py:419 add_n “AddN”,输入 = 输入,名称 = 名称) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:750 _apply_op_helper attrs=attr_protos, op_def=op_def) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:592 _create_op_internal 计算设备) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:3536 _create_op_internal op_def=op_def) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:2016 初始化 控制输入​​操作,操作定义) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1856 _create_c_op 引发 ValueError(str(e))

ValueError: Shapes must be equal rank, but are 1 and 0
  From merging shape 1 with other shapes. for 'node AddN = AddN[N=3, T=DT_FLOAT](mul_2, mul_5, dense_199/kernel/Regularizer/mul)' with input shapes: [2], [2], [].

如果我省略正则化器,错误就会消失。

【问题讨论】:

【参考方案1】:

我 found 认为 loss_weights 必须是一个列表,而不是一个数组。

import numpy as np
import keras

X = np.ones((100,20))
Y1 = np.ones((100,5))
Y2 = np.ones((100,4))

Input_1= keras.layers.Input(shape=X.shape[1])

x = keras.layers.Dense(100)(Input_1)
x = keras.layers.Dense(100)(x)

out1 = keras.layers.Dense(5, kernel_regularizer='l1')(x)
out2 = keras.layers.Dense(4)(x)

model = keras.models.Model(inputs=Input_1, outputs=[out1,out2])
model.compile(loss = 'mse', loss_weights=list(np.arange(2)))

model.fit(X, [Y1, Y2], epochs=2)

【讨论】:

以上是关于正则化器导致“ValueError:Shapes must be equal rank”的主要内容,如果未能解决你的问题,请参考以下文章

Tensorflow 维度问题:ValueError: Shapes (3, 1) and (None, 3) is incompatible

如何修复'ValueError:shapes(1,3)和(1,1)未对齐:3(dim 1)!= 1(dim 0)'numpy中的错误

ValueError: Shapes must be equal rank, but are 1 and 0 从将形状 1 与其他形状合并。对于“损失/添加”

ValueError:形状(50,6)和(50,6)未对齐:6(dim 1)!= 50(dim 0)

Keras:内核和活动正则化器之间的区别

带有softmax的活动正则化器?