张量流简单逻辑回归

tensorflow simple logistic regression

你好

我只想尝试使用简单逻辑进行二元分类 regression.I 已将未标记的输出数据作为 {1,0} //(He/she 通过与否) 成本函数 returns (NaN)。怎么了?

learning_rate = 0.05
total_iterator = 1500
display_per = 100

data = numpy.loadtxt("ex2data1.txt",dtype=numpy.float32,delimiter=",");

training_X = numpy.asarray(data[:,[0,1]]) # 100 x 2

training_X 包含 100 x 2 矩阵作为考试 scores.e.g [98.771 4.817]

training_Y = numpy.asarray(data[:,[2]],dtype=numpy.int) # 100 x 1 

training_Y 包含 100x1 数组,[1] [0] [0] [1] 由于 Whosebug 格式,我无法逐行写入

m = data.shape[0]

x_i = tf.placeholder(tf.float32,[None,2]) # None x 2                        
y_i = tf.placeholder(tf.float32,[None,1]) # None x 1                       

W = tf.Variable(tf.zeros([2,1]))  # 2 x 1 
b = tf.Variable(tf.zeros([1]))  # 1 x 1 

h = tf.nn.softmax(tf.matmul(x_i,W)+b)

cost = tf.reduce_sum(tf.add(tf.multiply(y_i,tf.log(h)),tf.multiply(1-
y_i,tf.log(1-h)))) / -m

我尝试使用简单的逻辑成本 function.it 得到返回 'NaN'。我认为我的成本函数完全是垃圾,使用了 tensorflow 示例的成本函数:

 cost = tf.reduce_mean(-tf.reduce_sum(y_i*tf.log(h), reduction_indices=1))

但效果不佳。

initializer= tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    print("cost: ", sess.run(cost, feed_dict={x_i:training_X, 
    y_i:training_Y}), "w: ", sess.run(W),"b: ", sess.run(b))

函数 tf.nn.softmax 期望对数(最后一个维度)的数量等于 classes 的数量(在您的情况下为 2 {1,0})。由于您的情况的最后一个维度是 1,因此 softmax 将始终 return 1(在唯一可用的 class 中的概率始终为 1,因为没有其他 class 存在)。因此 h 是一个用 1 填充的张量,而 tf.log(1-h) 将 return 负无穷大。无穷大乘以零(1-y_i 在某些行中)returns NaN.

您应该将 tf.nn.softmax 替换为 tf.nn.sigmoid

可能的解决方法是:

h = tf.nn.sigmoid(tf.matmul(x_i,W)+b)
cost = tf.reduce_sum(tf.add(tf.multiply(y_i,tf.log(h)),tf.multiply(1-
y_i,tf.log(1-h)))) / -m

或更好,您可以使用 tf.sigmoid_cross_entropy_with_logits
在这种情况下,应该按如下方式进行:

h = tf.matmul(x_i,W)+b
cost = tf.reduce_mean(tf.sigmoid_cross_entropy_with_logits(labels=y_i, logits=h))

此函数在数值上比使用 tf.nn.sigmoid 后跟 cross_entropy 函数更稳定,后者可以 return 如果 tf.nn.sigmoid 接近 0 或 1,则由于float32 的不精确性。