首页 文章

tensorflow:pow给出负输出

提问于
浏览
0

我正在使用tensorflow来构建一个简单的自动编码器模型,但是这个奇怪的错误我无法诊断我有一个看起来像这样的损失函数:

def loss_func(x,y):
        return 0.5 * tf.reduce_mean(tf.pow(x-y, 2 ) )

然后通过以下方式计算总损失:

return self.loss_func(x , input) + self.reg_fac * reg

现在的问题是,当设置 reg_fac0 时,损失返回为正数,模型似乎训练得很好,但是当增加reg_fac时,损失减少并达到负值并持续下降

对于使用的每个自动编码器, reg 计算如下:

return tf.reduce_mean(tf.pow(self.w1, 2)) + tf.reduce_mean(tf.pow(self.w2, 2))

其中 w1 是编码器权重, w2 是解码器权重 . 我知道's a stupid bug, but I can'找到它 .

我的完整代码上传到这里:https://github.com/javaWarrior/dltest

important files:
ae.py: autoencoders model,
sae.py: stacked autoencoders model,
mew.py: testing model on extracted features of nus_wide images using SIFT,
nus_wide.py: just an interface for nuswide

1 回答

  • 1

    我不确定您的错误来自哪里,但我相信您的自动编码器模型通常存在一些问题 . 简单模型应该看起来像这个例子取自the tensorflow models repo .

    # model
        x = tf.placeholder(tf.float32, [None, len_input])
        h = tf.nn.softplus(tf.matmul(x, w1) + b1)
        xHat = tf.matmul(h, w2) + b
    
        # cost
        cost = 0.5 * tf.reduce_sum(tf.pow(xHat - x, 2.0))
        optimizer = tf.train.AdamOptimizer().minimize(cost)
    

    As it pertains to the question ,关键区别可能是使用reduce_sum()而不是reduce_mean() . 我不确定你为什么要使用它 .

    此外,AdamOptimizer应该为您处理正则化 . 作为旁注,如果你想通过从头开始进行正规化来学习,我会推荐this tutorial .

相关问题