不能得到简单的张量流逻辑回归程序来使用sigmoid激活函数。

不能得到简单的张量流逻辑回归程序来使用sigmoid激活函数。

问题描述:

我有一个非常简单的逻辑回归tensorflow程序是这样的:不能得到简单的张量流逻辑回归程序来使用sigmoid激活函数。

#!/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})) 

这是一个相当简单的二元分类Logistic回归程序,需要100行的样本数据中有两列,性别有一个值0(女)或1(男)。 身高以厘米为单位。

我试图做一个基于高度的性别预测,但损失值似乎并没有收敛到最低限度,另外,成本值和准确性从一次运行到下一次大幅变化,尽管正在查看的数据是相同的数据。

我可以有一个运行在精度为0.8,下次运行精度 为0.2

而且,我注意到一些原因,第一损耗值始终是: 损失= 0.693147 但是,对于例如损失计算的其余部分可以是这样的: 损失= 1.397364

损失= 1.397516

损失= 1.397514

个损失= 1.397515

损失= 1.397514 ...

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

此外,有人可以建议一个简单的二分类问题的例子使用logistic回归和tensorflow 使用mnist或虹膜数据库?

任何帮助表示赞赏。

由于

两者的x和y应该形状的[无,1]和你的W¯¯简单[1,1]。你的输入和输出都是单维的。

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

+0

谢谢。我放弃了矩阵符号,它似乎让事情变得更好,但是,当我尝试打印精确度时,我现在遇到了一个问题。 – redmage123

+0

Gah。忘了完成。我得到的错误是InvalidArgumentError(请参阅上面的回溯):预期的范围在[-1,1)范围内,但得到了1 这发生在我运行accuracy.eval的行中,以打印出精确度。 – redmage123