Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow getting ValueError:true\u fn和false\u fn的输出必须具有相同的类型:int32,float32,同时使用tf.histogram\u fixed\u width\u bin_Tensorflow_Keras_Deep Learning_Lstm_Stat - Fatal编程技术网

Tensorflow getting ValueError:true\u fn和false\u fn的输出必须具有相同的类型:int32,float32,同时使用tf.histogram\u fixed\u width\u bin

Tensorflow getting ValueError:true\u fn和false\u fn的输出必须具有相同的类型:int32,float32,同时使用tf.histogram\u fixed\u width\u bin,tensorflow,keras,deep-learning,lstm,stat,Tensorflow,Keras,Deep Learning,Lstm,Stat,希望有人能帮我解决这个问题,或者给我一些提示/想法,我可以修复这个错误 我正在尝试在SeqtoSeq模型中创建一个自定义层。我需要在部分代码中调用直方图。但是,当它触及这一行代码时,会引发一个错误: ValueError: Outputs of true_fn and false_fn must have the same type: int32, float32 这是我的图层代码: class entropy_measure(Layer): def __init__(self, b

希望有人能帮我解决这个问题,或者给我一些提示/想法,我可以修复这个错误

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

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.在训练阶段
要求
self.rev\u熵(x,self.beta,self.batch)
x在这种情况下必须具有相同的类型。但是
tf.histogram\u fixed\u width\u bin
x
float32
时返回
int32
。所以你需要改变类型

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

试试
new\u f\u w\u t=tf.cast(new\u f\u w\t,tf.float32)
@giser\u yugang谢谢你的回答。我不敢相信是这样的!!!!那么为什么它会在新的线路中引起错误呢??我的意思是在做柱状图时返回之前的行
K.在训练阶段
要求
self.rev\u熵(x,self.beta,self.batch)
x
为同一类型。但是
tf.histogram\u fixed\u width\u bin
x
float32
@giser\u yugang时返回
int32
,非常感谢您的澄清。你能把你的答案和解释一起添加吗?这样我就可以选择作为接受的答案。我还尝试填充参数dtype=tf.float32,这是用于返回类型的,但没有帮助。所以我完全不喜欢这可能是原因你知道为什么在直方图中设置dtypes=float32没有帮助吗?在文档中,它说这是返回张量的类型@我也是。它看起来像一只虫子。