我的神经网络正在尝试预测一个人是否患有糖尿病,这是我的数据集
kaggle.com/uciml/pima-indians-diabetes-database。
我使用的是3层神经网络,准确度是65%。
对于提高准确性的任何帮助将不胜感激。
这是我的代码------------------------------------------- ---------------
import numpy as np
import tensorflow as tf
import pandas as pd
df=pd.read_csv(r'C:\Users\manas\Downloads\diabetes.csv')
actualY=df['Outcome']
actualX=df.drop(['Outcome'],axis=1)
actualX=np.array(np.reshape(actualX,newshape=[768,8]))
actualY=np.array(np.reshape(actualY,newshape=[768,1]))
#Y=[768,1]
#X=[768,8]
x=tf.placeholder(dtype=tf.float64,shape=[768,8])
W1=tf.Variable(dtype=np.float64,initial_value=np.random.random((8,500)))
B1=tf.Variable(dtype=np.float64,initial_value=np.random.random((1,1)))
y_prediction1=((tf.add(tf.matmul(x,W1),B1)))
output1=tf.nn.sigmoid(y_prediction1)
W2=tf.Variable(dtype=np.float64,initial_value=np.random.random((500,600)))
B2=tf.Variable(dtype=np.float64,initial_value=np.random.random((1,1)))
y_prediction2=((tf.add(tf.matmul(output1,W2),B2)))
output2=tf.nn.sigmoid(y_prediction2)
W3=tf.Variable(dtype=np.float64,initial_value=np.random.random((600,1)))
B3=tf.Variable(dtype=np.float64,initial_value=np.random.random((1,1)))
y_prediction3=((tf.add(tf.matmul(output2,W3),B3)))
y_true=tf.placeholder(dtype=tf.float64,shape=[768,1])
loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_prediction3,labels=y_true))
optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(loss)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(200):
(sess.run(optimizer,feed_dict={x:actualX,y_true:actualY}))
print(i,sess.run(loss, feed_dict={x: actualX, y_true: actualY}))
print(i)
prediction = tf.round(tf.sigmoid((y_prediction3)))
correct = tf.cast(tf.equal(prediction, y_true), dtype=np.float64)
accuracy = tf.reduce_mean(correct)
print(sess.run(accuracy,feed_dict={x: actualX, y_true: actualY}))
答案 0 :(得分:0)
数据集非常小,选择模型参数时一定要小心。
我的代码中有些东西我不明白。这是二进制分类,对吗?如果是这样,您应该有一个一键编码的输出,GT的大小为2。在您的代码中,每个示例似乎只有一个输出!如果要保留GT作为矢量,则应改用tf.nn.sparse_softmax_cross_entropy_with_logits
。
在与argmax
比较之前,您不应该在y_true
上使用softmax和prediction
吗?还要检查那部分。
如果您的代码一切正常,我也会考虑以下建议:
B1 = tf.Variable(tf.random_normal([500]))
中)无论如何,有了这个小的数据集,您应该很容易过拟合训练集。 200个纪元后获得65%的分数清楚地表明您的模型有问题。仔细检查您的代码,确保其中没有错误。您可以使用此code作为示例。
UPDATE1: 您必须具有一键编码输出!在您的情况下,它的尺寸为2。检查此代码,我对您的代码进行了少量修改,但未运行。解决任何可能的错误并尝试使其运行:
import numpy as np
import tensorflow as tf
import pandas as pd
df=pd.read_csv(r'C:\Users\manas\Downloads\diabetes.csv')
actualY=df['Outcome']
actualX=df.drop(['Outcome'],axis=1)
actualX=np.array(np.reshape(actualX,newshape=[768,8]))
actualY=np.array(np.reshape(actualY,newshape=[768,1]))
#Y=[768,1]
#X=[768,8]
x = tf.placeholder('float',,shape=[None,8])
y_true = tf.placeholder('float',,shape=[None,1])
W1 = tf.get_variable("W1", shape=[8, 128],initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable("b1", shape=[128],initializer=initializer=tf.zeros_initializer())
h1 = tf.nn.relu(tf.add(tf.matmul(x,W1),b1))
W2 = tf.get_variable("W2", shape=[128, 128],initializer=tf.contrib.layers.xavier_initializer())
b2 = tf.get_variable("b2", shape=[128],initializer=initializer=tf.zeros_initializer())
h2 = tf.nn.relu(tf.add(tf.matmul(h1,W2),b2))
W3 = tf.get_variable("W3", shape=[128, 2],initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.get_variable("b3", shape=[2],initializer=initializer=tf.zeros_initializer())
logits = tf.add(tf.matmul(h2,W3),b3)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits,labels=tf.one_hot(y_true)))
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(200):
sess.run(optimizer,feed_dict={x:actualX,y_true:actualY})
print(i,sess.run(loss, feed_dict={x: actualX, y_true: actualY}))
print(i)
pred = tf.nn.softmax(logits)
correct_prediction = tf.equal(tf.argmax(pred, 1), y_true)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy,feed_dict={x: actualX, y_true: actualY}))
答案 1 :(得分:0)
这是我运行的代码
import numpy as np
import tensorflow as tf
import pandas as pd
df=pd.read_csv(r'C:\Users\manas\Downloads\diabetes.csv')
actualY=df['Outcome']
actualX=df.drop(['Outcome'],axis=1)
actualX=np.array(np.reshape(actualX,newshape=[768,8]))
actualY=np.array(np.reshape(actualY,newshape=[768,1]),dtype=int)
#Y=[768,1]
#X=[768,8]
x = tf.placeholder('float',shape=[None,8])
y_true = tf.placeholder(shape=[None,1],dtype=tf.int64)
W1 = tf.get_variable("W1", shape=[8,
128],initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable("b1", shape=[128],initializer=tf.zeros_initializer())
h1 = tf.nn.relu(tf.add(tf.matmul(x,W1),b1))
W2 = tf.get_variable("W2", shape=[128,
128],initializer=tf.contrib.layers.xavier_initializer())
b2 = tf.get_variable("b2", shape=[128],initializer=tf.zeros_initializer())
h2 = tf.nn.relu(tf.add(tf.matmul(h1,W2),b2))
W3 = tf.get_variable("W3", shape=[128,
2],initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.get_variable("b3", shape=[2],initializer=tf.zeros_initializer())
logits = tf.add(tf.matmul(h2,W3),b3)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=tf.one_hot(y_true,depth=2)))
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(3000):
sess.run(optimizer,feed_dict={x:actualX,y_true:actualY})
print(i,sess.run(loss, feed_dict={x: actualX, y_true: actualY}))
print(i)
pred = tf.nn.softmax(logits)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.arg_max(y_true,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(tf.round(tf.nn.softmax(logits)),feed_dict={x:actualX,y_true:actualY}))
print(sess.run(accuracy,feed_dict={x: actualX, y_true: actualY}))
尽管这损失了0.14772553
,但准确性却是64.564555 %
。
所以我真的不知道问题出在哪里。