2016-08-24 136 views
2

我想創建一個程序,它將使用Tensorflow將點歸類爲10。我試圖圍繞這個陰謀的中心創建一個橢圓形狀,其中藍點是:Tensorflow多變量邏輯迴歸不起作用

橢圓形中的所有內容都應歸類爲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() 

回答

1

當您使用softmax_cross_entropy_with_logits出現問題。在具體情況下,logitslabels應該有形狀[batch_size, number_of_labels=2]

請注意,您的張量logits=tf.matmul(x, W) + blabels=y已形狀[batch_size, 1],所以Tensorflow假設number_of_labels=1。這就是爲什麼你的猜測總是一致的原因。

A)你可以通過編碼training_out作爲一個熱門的矢量來解決這個問題。我建議使用np.eye()以實現:

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)或者,你可以使用sparse_softmax_cross_entropy_with_logits,這使得labels有形狀[batch_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()