Accuracy bad even after implementing deep neural net

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Accuracy bad even after implementing deep neural net



my neural net is trying to predict whether a person has diabetes or not, This is my data set
kaggle.com/uciml/pima-indians-diabetes-database.
I am using a 3 layered neural network and my accuracy is 65%.

Any help for increasing the accuracy will be appreciated.



This is my code----------------------------------------------------------


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

df=pd.read_csv(r'C:UsersmanasDownloadsdiabetes.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))




2 Answers
2



The dataset is quite small, you have to be carful while choosing the model parameters.



there are few things I don't understand from your code. It is a binary classification, right? if so, you should have a one-hot encoded output and GT with size of 2. In your code it seems you have only one output for every example! if you want to keep the GT as a vector, you should use tf.nn.sparse_softmax_cross_entropy_with_logits instead.
Shouldn't you use softmax and argmax on y_true before comparing it to prediction? check that part as well.


tf.nn.sparse_softmax_cross_entropy_with_logits


argmax


y_true


prediction



if everything is fine with your code, I would consider these suggestions as well:


B1 = tf.Variable(tf.random_normal([500]))



Anyways, with this small dataset you should easily overfit the training set. Getting 65% after 200 epoch clearly shows that there is something wrong with your model. Double check your code and make sure there is nothing wrong in it. You can use this code as an example.



UPDATE1:
You have to have one-hot encoded output! in your case it is with dimension 2. Check this code, I've made few modification on your code, but I didn't run it. Solve any possible bug and try to make it run:


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

df=pd.read_csv(r'C:UsersmanasDownloadsdiabetes.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))





y_true is a 768,1 matrix which has only one value,(0 or 1) and sigmoid of y_prediction3 also gives only one value (0 or 1)... so isn't it fine that I use sigmoid_cross_entropy...(PS:I am a beginner in this so can you help me to implement tf.nn.sparse_softmax_cross_entropy_with_logits to solve the problem)
– Manas Bam - Grade 8
Aug 12 at 13:09





1:I reduced no of hidden units
– Manas Bam - Grade 8
Aug 12 at 13:10





2:I changed the bias dimension
– Manas Bam - Grade 8
Aug 12 at 13:10





3:I tried using adamoptimizer and relu
– Manas Bam - Grade 8
Aug 12 at 13:11





Loss seems to be decreasing but after a point it becomes a constant 0.6412223
– Manas Bam - Grade 8
Aug 12 at 13:11



This is the code that i ran


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

df=pd.read_csv(r'C:UsersmanasDownloadsdiabetes.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))



Although this gives a loss of 0.14772553, it gives an accuracy of 64.564555 %.


0.14772553


64.564555 %



So I really don't know what the problem is.





can you add the loss and accuracy plots?
– Ehab AlBadawy
Aug 15 at 8:37





I don't know how to do that
– Manas Bam - Grade 8
Aug 16 at 4:05





For each epoch save the loss and accuracy values and plot them after the training is done.
– Ehab AlBadawy
Aug 16 at 6:11






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS