无法获得简单的tensorflow逻辑回归程序来处理sigmoid激活函数。

时间:2017-10-12 02:31:27

标签: python tensorflow logistic-regression

我有一个非常简单的逻辑回归张量流程程序,如下所示:

 #!/usr/bin/env python3
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
import sys

gender_df = pd.read_csv('data/binary_data.csv')

# Shuffle our data
gender_df = gender_df.sample(frac=1).reset_index(drop=True)

print (gender_df.columns)

train_x,test_x, train_y, test_y = model_selection.train_test_split(gender_df['HEIGHT'],gender_df['GENDER'],test_size = 0.3)

tmp = np.asarray(train_x)
tmp.resize([train_x.shape[0],1])
train_x = tmp

tmp = np.asarray(train_y)
tmp = np.resize(tmp,[train_y.shape[0],2])
train_y = tmp

tmp = np.asarray(test_x)
tmp.resize([test_x.shape[0],1])
test_x = tmp

tmp = np.asarray(test_y)
tmp = np.resize(tmp,[test_y.shape[0],2])
test_y = tmp

n_samples = train_x.shape[0]

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

W = tf.Variable(tf.zeros([1,2]),dtype = tf.float32)
b = tf.Variable(tf.zeros([1]),dtype = tf.float32)

a = tf.nn.sigmoid((W * x) + b)

learning_rate = 0.001

cross_entropy = tf.reduce_mean(-(y*tf.log(a) + (1 - y) * tf.log(1-a)))
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(1000):
        _,l = sess.run([train_step, cross_entropy], feed_dict = {x: train_x, y:train_y})
        if epoch % 50 == 0:
            print ('loss = %f' %(l))

    correct = tf.equal(tf.argmax(a,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct,'float'))
    print ('Accuracy: ', accuracy.eval({x: test_x, y:test_y}))

这是一个相当简单的二元分类逻辑回归程序,它采用100行具有两列的样本数据,Gender的值为0(女性)或1(男性)。 高度以厘米为单位。

我正在尝试根据身高进行性别预测,但损失值似乎没有收敛到最低限度,此外,成本值和准确度从一次运行到下一次运行都有很大差异,即使正在查看的数据是相同的数据。

我可以进行精度为0.8的运行,然后运行精度 是0.2

此外,我注意到由于某种原因,第一个损失值总是: 损失= 0.693147 但是,例如其余的损失计算可能如下所示: 损失= 1.397364

损失= 1.397516

损失= 1.397514

损失= 1.397515

损失= 1.397514 ...

我对发生的事情感到困惑 我使用正确的sigmoid功能吗?从我的理解,我 当我有多个类的逻辑回归问题时,只需要使用softmax,对于简单的二进制分类,我可以使用tf.sigmoid()。另外,我是否需要在sigmoid函数中添加'b'参数?我应该将其设置为随机值而不是零吗?

此外,有人可以使用逻辑回归和张量流建议一个简单的二进制分类问题示例使用mnist或iris数据库吗?

任何帮助表示感谢。

由于

2 个答案:

答案 0 :(得分:1)

你的x和y都应该是[None,1]的形状,你的W应该只是[1,1]。输入和输出都是单维的。

您甚至可以删除矩阵表示法,并在此示例中使用简单的向量。

答案 1 :(得分:0)

这又是从另一个TensorFlow落后者的角度来看,但我设法执行了这个。但我无法提供合适的解释。

代码现在正在使用一个热门的'表示和准确度为0.84。我刚刚使用了另一个数据集,但我不知道有多少特征是最佳的。我相信它也应该适用于原始数据集,但是当它工作时我已经切换了数据集。 功能是数字。

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn import preprocessing


data = pd.read_csv('D:/Development_Avector/PycharmProjects/TensorFlow/anes_dataset.csv')
training_features = ['TVnews', 'PID', 'age', 'educ', 'income']
target = 'vote'

print (data.columns)

X = data.loc[:, ['TVnews', 'PID', 'age', 'educ', 'income']]
# y is our labels
y = data.loc[:, ['vote']]

oneHot = preprocessing.OneHotEncoder()
oneHot.fit(X)
X = oneHot.transform(X).toarray()
oneHot.fit(y)
y = oneHot.transform(y).toarray()
train_x,test_x, train_y, test_y = model_selection.train_test_split(X, y, test_size = 0.1, random_state=0)


n_samples = train_x.shape[0]
print ("There are " + str( n_samples) + " samples")
print ("Shape of train_x is " + str(train_x.shape))
print ("Shape of train_y is " + str(train_y.shape))
print (train_y)

x = tf.placeholder(tf.float32, [None,train_x.shape[1]])
y = tf.placeholder(tf.float32,[None,2])
print ("Shape of y is " + str(y.shape))


W = tf.Variable(np.zeros((train_x.shape[1], 2)),tf.float32,name="W")
b = tf.Variable(0.,dtype = tf.float32)


predicted_y1 = tf.add(tf.matmul(x,tf.cast(W,tf.float32) ), b)
print (predicted_y1.shape)
predicted_y = tf.nn.softmax(predicted_y1)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = predicted_y, labels = y))


optimizer = tf.train.AdamOptimizer(0.0003).minimize(cross_entropy)

s = tf.InteractiveSession()
s.run(tf.global_variables_initializer())

for i in range(40000):
    _, loss_i = s.run([optimizer,cross_entropy], {x: train_x, y: train_y})
    print("loss at iter %i:%.4f" % (i, loss_i))

accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(predicted_y,1), tf.argmax(y,1)), "float"))
accuracy_value = s.run(accuracy, feed_dict={x:test_x, y:test_y})
print (accuracy_value)


loss at iter 0:0.6931
loss at iter 1:0.6929
loss at iter 2:0.6927
loss at iter 3:0.6924
-------------------
loss at iter 39997:0.3599
loss at iter 39998:0.3599
loss at iter 39999:0.3599
0.84210527