逻辑回归调试张量流

时间:2016-11-10 20:29:31

标签: python tensorflow logistic-regression

我正在尝试学习张量流,目前正在尝试做一个简单的逻辑回归模型。这是我的代码,我从我能找到的不同例子中拼凑而成。

with tf.Session() as sess:
    # Training data
    input = tf.constant(tra)
    target = tf.constant(np.transpose(data[:,1]).astype(np.float64))

    # Set model weights
    W = tf.Variable(np.random.randn(10, 1).astype(np.float64))

    # Construct model
    mat=tf.matmul(input,W)
    pred = tf.sigmoid(mat)

    # Compute the error
    yerror = tf.sub(pred, target)
    # We are going to minimize the L2 loss. The L2 loss is the sum of the
    # squared error for all our estimates of y. This penalizes large errors
    # a lot, but small errors only a little.
    loss = tf.nn.l2_loss(yerror)

    # Gradient Descent
    update_weights = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

    # Initializing the variables
    tf.initialize_all_variables().run()

    for _ in range(50):
        # Repeatedly run the operations, updating the TensorFlow variable.
        sess.run(update_weights)

        print(loss.eval())

所以代码运行但是每次&sess.run(update_weights)'之后错误没有改善。 itteration和我尝试过不同的步长。

我想知道设置是否正确?

我有点不确定如何调试它,因为所有内容的计算都是在run命令中完成的。培训数据很好。如果你们中的一些人能够在整个会话中看到我做错了什么,或者建议如何调试这个。

非常感谢。

3 个答案:

答案 0 :(得分:0)

虽然您正在做的事情对于回归来说是合理的事情,但这可能不适用于分类任务。此外,这不是通常用作逻辑回归的内容。对于逻辑回归,您可以最大化log prob(correct label|input)对所有数据样本求和。这可以通过使用softmax层来方便地完成。

它附带了有用的数学属性,有助于调试。例如,如果您输入的是完全随机的,则损失应为log (N)。如果将所有权重设置为零,那么这应该是损失。如果您在二进制分类中标记样本,说出1/3正数和2/3负数,那么不考虑任何输入的模型(只是偏差项)应该只返回{{1} ,所以你可以在某种程度上调试你得到的东西。

答案 1 :(得分:0)

好的,所以我做了一些测试,发现它的目标尺寸有问题'变量。我必须指定它是一个m x 1矩阵(其中m是训练示例的数量)这是通过将形状指定为常量变量来完成的:

target = tf.constant(np.transpose(data[:,1]).astype(np.float64), shape=[m,1])

在我对功能进行标准化之前,gradiant的表现并不好。

答案 2 :(得分:0)

TensorFlow现在附带一个名为 tfdbg 的内置调试器。它公开中间张量值并通过命令行界面显示它们。它应该使您更容易调试代码。请查看文档(主HEAD): https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/how_tos/debugger/index.md