Python Tensorflow多变量逻辑回归不起作用

Python Tensorflow多变量逻辑回归不起作用,python,machine-learning,tensorflow,logistic-regression,Python,Machine Learning,Tensorflow,Logistic Regression,我正在尝试创建一个程序,该程序将使用将点分类为1或0。我试图在这张图的中心周围创建一个椭圆形,蓝色的圆点是: 椭圆形中的所有内容都应归类为1,其他所有内容都应归类为0。在上图中,蓝色点为1s,红色x为0s 然而,每次我尝试对一个点进行分类时,它总是选择1,即使它是我训练它的一个点,说它是0 我的问题很简单:为什么猜测总是1,我做错了什么,或者应该做些什么来解决这个问题?这是我在没有教程的情况下尝试的第一个机器学习问题,所以我对这方面真的不太了解 如果你能给我任何帮助,我将不胜感激,谢谢 这是我的

我正在尝试创建一个程序,该程序将使用将点分类为
1
0
。我试图在这张图的中心周围创建一个椭圆形,蓝色的圆点是:

椭圆形中的所有内容都应归类为
1
,其他所有内容都应归类为
0
。在上图中,蓝色点为
1
s,红色x为
0
s

然而,每次我尝试对一个点进行分类时,它总是选择
1
,即使它是我训练它的一个点,说它是
0

我的问题很简单:为什么猜测总是
1
,我做错了什么,或者应该做些什么来解决这个问题?这是我在没有教程的情况下尝试的第一个机器学习问题,所以我对这方面真的不太了解

如果你能给我任何帮助,我将不胜感激,谢谢

这是我的密码:

#!/usr/bin/env python3

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

training_in = numpy.array([[0, 0], [1, 1], [2, 0], [-2, 0], [-1, -1], [-1, 1], [-1.5, 1],   [3, 3], [3, 0], [-3, 0], [0, -3], [-1, 3], [1, -2], [-2, -1.5]])
training_out = numpy.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])

def transform_data(x):
    return [x[0], x[1], x[0]**2, x[1]**2, x[0]*x[1]]

new_training_in = numpy.apply_along_axis(transform_data, 1, training_in)

feature_count = new_training_in.shape[1]

x = tf.placeholder(tf.float32, [None, feature_count])
y = tf.placeholder(tf.float32, [None, 1])

W = tf.Variable(tf.zeros([feature_count, 1]))
b = tf.Variable(tf.zeros([1]))

guess = tf.nn.softmax(tf.matmul(x, W) + b)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(tf.matmul(x, W) + b, y))

opti = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    for (item_x, item_y) in zip(new_training_in, training_out):
        sess.run(opti, feed_dict={ x: [item_x], y: [[item_y]]})

print(sess.run(W))
print(sess.run(b))

plt.plot(training_in[:6, 0], training_in[:6, 1], 'bo')
plt.plot(training_in[6:, 0], training_in[6:, 1], 'rx')

results = sess.run(guess, feed_dict={ x: new_training_in })

for i in range(training_in.shape[0]):
    xx = [training_in[i:,0]]
    yy = [training_in[i:,1]]
    res = results[i]

    # this always prints `[ 1.]`
    print(res)

    # uncomment these lines to see the guesses
    # if res[0] == 0:
    #     plt.plot(xx, yy, 'c+')
    # else:
    #     plt.plot(xx, yy, 'g+')

plt.show()

使用时会出现问题。在您的具体案例中,
logits
labels
都应该具有形状
[批次大小,标签数量=2]

请注意,您的张量
logits=tf.matmul(x,W)+b
labels=y
具有形状
[batch\u size,1]
,因此Tensorflow假设
标签的数量=1
。这就是为什么你的猜测总是一样的

A)您可以通过将
training\u out
编码为一个热向量来解决此问题。我建议使用以下方法来实现:

training_out = [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
training_out = numpy.eye(2)[training_out]
然后,您需要进行以下更改:

y = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([feature_count, 2]))
b = tf.Variable(tf.zeros([2]))
...
for i in range(1000):
    for (item_x, item_y) in zip(new_training_in, training_out):
        sess.run(opti, feed_dict={x: [item_x], y: [item_y]})
...
results = sess.run(guess, feed_dict={x: new_training_in})[:,1]
B)或者,您可以使用,它允许
标签
具有形状
[batch\u size]
。我已经调整了您的代码,使其以这种方式工作:

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

training_in = numpy.array(
    [[0, 0], [1, 1], [2, 0], [-2, 0], [-1, -1], [-1, 1], [-1.5, 1], [3, 3], [3, 0], [-3, 0], [0, -3], [-1, 3], [1, -2],
     [-2, -1.5]])
training_out = numpy.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0])

def transform_data(x):
    return [x[0], x[1], x[0] ** 2, x[1] ** 2, x[0] * x[1]]

new_training_in = numpy.apply_along_axis(transform_data, 1, training_in)

feature_count = new_training_in.shape[1]

x = tf.placeholder(tf.float32, [None, feature_count])
y = tf.placeholder(tf.int32, [None])

W = tf.Variable(tf.zeros([feature_count, 2]))
b = tf.Variable(tf.zeros([2]))

guess = tf.nn.softmax(tf.matmul(x, W) + b)

cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(tf.matmul(x, W) + b, y))

opti = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    for (item_x, item_y) in zip(new_training_in, training_out):
        sess.run(opti, feed_dict={x: [item_x], y: [item_y]})

print(sess.run(W))
print(sess.run(b))

plt.plot(training_in[:6, 0], training_in[:6, 1], 'bo')
plt.plot(training_in[6:, 0], training_in[6:, 1], 'rx')

results = sess.run(guess, feed_dict={x: new_training_in})

for i in range(training_in.shape[0]):
    xx = [training_in[i:, 0]]
    yy = [training_in[i:, 1]]
    res = results[i]

    # this always prints `[ 1.]`
    print(res)

    # uncomment these lines to see the guesses
    if res[0] == 0:
        plt.plot(xx, yy, 'c+')
    else:
        plt.plot(xx, yy, 'g+')
plt.show()