【问题标题】:Accuracy very bad in tensorflow logistic regression张量流逻辑回归的准确性非常差
【发布时间】:2018-10-27 09:54:39
【问题描述】:

我正在尝试编写一个程序来预测一个人是否患有恶性肿瘤或良性肿瘤

数据集来自:https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Prognostic%29

这是我的代码,我的准确率约为 65%,这并不比掷硬币好。任何帮助将不胜感激

import tensorflow as tf
import pandas as pd
import numpy as np


df = pd.read_csv(r'D:\wholedesktop\logisticReal.txt')
df.drop(['id'], axis=1, inplace=True)

x_data = np.array(df.drop(['class'], axis=1))
x_data = x_data.astype(np.float64)
y = df['class']
y.replace(2, 0, inplace=True)
y.replace(4, 1, inplace=True)
y_data = np.array(y)
# y shape = 681,1
# x shape = 681,9

x = tf.placeholder(name='x', dtype=np.float32)
y = tf.placeholder(name='y', dtype=np.float32)

w = tf.Variable(dtype=np.float32, initial_value=np.random.random((9, 1)))
b = tf.Variable(dtype=np.float32, initial_value=np.random.random((1, 1)))

y_ = (tf.add(tf.matmul(x, w), b))
error = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_, labels=y))
goal = tf.train.GradientDescentOptimizer(0.05).minimize(error)

prediction = tf.round(tf.sigmoid(y_))
correct = tf.cast(tf.equal(prediction, y), dtype=np.float64)
accuracy = tf.reduce_mean(correct)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(2000):
        sess.run(goal, feed_dict={x: x_data, y: y_data})
        print(i, sess.run(accuracy, feed_dict={x: x_data, y: y_data}))

    weight = sess.run(w)
    bias = sess.run(b)
    print(weight)
    print(bias)

【问题讨论】:

    标签: python tensorflow machine-learning logistic-regression


    【解决方案1】:

    您的神经网络只有一个层,因此它所能做的最好的事情就是为您的数据拟合一条直线,以分隔不同的类别。这对于一般(高维)数据集来说是远远不够的。 (深度)神经网络的强大之处在于多层神经元之间的连通性。在您的示例中,您可以通过将matmul 的输出传递给具有不同权重和偏差的新matmul 来手动添加更多层,或者您可以使用contrib.layers 集合使其更简洁:

    x = tf.placeholder(name='x', dtype=np.float32)
    fc1 = tf.contrib.layers.fully_connected(inputs=x, num_outputs=16, activation_fn=tf.nn.relu)
    fc2 = tf.contrib.layers.fully_connected(inputs=fc1, num_outputs=32, activation_fn=tf.nn.relu)
    fc3 = tf.contrib.layers.fully_connected(inputs=fc2, num_outputs=64, activation_fn=tf.nn.relu)
    

    诀窍是将一层的输出作为输入传递给下一层。随着你添加越来越多的层,你的准确率会上升(可能是因为过度拟合,使用dropout 来解决这个问题)。

    【讨论】:

    • 我对神经网络一无所知..我试着用逻辑回归来做你上面描述的
    猜你喜欢
    • 2019-12-22
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2016-07-21
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多