getting ValueError: Outputs of true_fn and false_fn must have the same type: int32, float32 while using tf.histogram_fixed_width_bins

getting ValueError: Outputs of true_fn and false_fn must have the same type: int32, float32 while using tf.histogram_fixed_width_bins

希望有人能帮我解决这个问题或指出一些问题hint/ideas我可以解决这个错误。

我正在尝试在 SeqtoSeq 中创建自定义层 model.I 需要在我的部分代码中调用直方图。但是,当它触及这行代码时会引发错误:

ValueError: Outputs of true_fn and false_fn must have the same type: int32, float32

这是我的图层代码:

class entropy_measure(Layer):

    def __init__(self, beta,batch, **kwargs):
        self.beta = beta
        self.batch = batch
        self.uses_learning_phase = True
        self.supports_masking = True
        super(entropy_measure, self).__init__(**kwargs)

    def call(self, x):
        return K.in_train_phase(self.rev_entropy(x, self.beta,self.batch), x)

    def get_config(self):
        config = {'beta': self.beta}
        base_config = super(entropy_measure, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def rev_entropy(self, x, beta,batch):

        value_ranges = [0.0, 10.0]
        nbins = 5   
        converted_x = tf.cast(x,tf.float32)
        new_f_w_t = tf.histogram_fixed_width_bins(converted_x, value_ranges, nbins)

        return new_f_w_t

我调用这个层使用:

encoded = entropy_measure(beta=0.08,batch=BATCH_SIZE)(encoded)

此代码使用keras tensorflow后端编写。

知道错误的根源是什么吗?

在这种情况下,

K.in_train_phase 要求 self.rev_entropy(x, self.beta,self.batch)x 必须具有相同的类型。但是 tf.histogram_fixed_width_bins returns int32 当你的 xfloat32 时。所以你需要改变类型。

new_f_w_t = tf.cast(new_f_w_t, tf.float32)