在训练神经网络时将损失值设为0

时间:2018-05-30 03:56:54

标签: python tensorflow neural-network loss

我不确定是否应该粘贴整个代码,但现在是:

import tensorflow as tf 
import numpy as np  
import requests 
from sklearn.model_selection import train_test_split

BATCH_SIZE = 20


#Get data
birthdata_url = 'http://springer.bme.gatech.edu/Ch17.Logistic/Logisticdat/lowbwt.dat'
birth_file = requests.get(birthdata_url)
birth_data = birth_file.text.split('\r\n')[5:]
birth_data = np.array([[x for x in y.split(' ') if len(x)>=1] for y in birth_data[1:] if len(y)>=1])

#Get x and y vals
y_vals = np.array([x[1] for x in birth_data]).reshape((-1,1))
x_vals = np.array([x[2:10] for x in birth_data])

#Split data
x_train, x_test, y_train, y_test = train_test_split(x_vals,y_vals,test_size=0.3)


#Placeholders
x_data = tf.placeholder(dtype=tf.float32,shape=[None,8])
y_data = tf.placeholder(dtype=tf.float32,shape=[None,1])


#Define our Neural Network

def init_weight(shape):
    return tf.Variable(tf.truncated_normal(shape=shape,stddev=0.1))

def init_bias(shape):
    return tf.Variable(tf.constant(0.1,shape=shape))

def fully_connected(inp_layer,weights,biases):
    return tf.nn.relu(tf.matmul(inp_layer,weights)+biases)

def nn(x):
    w1 = init_weight([8,25])
    b1 = init_bias([25])
    layer1 = fully_connected(x,w1,b1)

    w2 = init_weight([25,10])
    b2 = init_bias([10])
    layer2 = fully_connected(layer1,w2,b2)

    w3 = init_weight([10,3])
    b3 = init_bias([3])
    layer3 = fully_connected(layer2,w3,b3)

    w4 = init_weight([3,1])
    b4 = init_bias([1])
    final_output = fully_connected(layer3,w4,b4)


    return final_output




#Predicted values.
y_ = nn(x_data)


#Loss and training step.
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_data,logits=y_))
train_step = tf.train.AdamOptimizer(0.1).minimize(loss)

#Initalize session and global variables
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#Accuracy
def get_accuracy(logits,labels):
    batch_predicitons = np.argmax(logits,axis=1)
    num_correct = np.sum(np.equal(batch_predicitons,labels))
    return(100*num_correct/batch_predicitons.shape[0])



loss_vec = []
for i in range(500):
    #Get random indexes and create batches. 
    rand_index = np.random.choice(len(x_train),size=BATCH_SIZE)

    #x and y batch.
    rand_x = x_train[rand_index]
    rand_y = y_train[rand_index]

    #Run the training step.
    sess.run(train_step,feed_dict={x_data:rand_x,y_data:rand_y})

    #Get the current loss. 
    temp_loss = sess.run(loss,feed_dict={x_data:x_test,y_data:y_test})
    loss_vec.append(temp_loss)

    if(i+1)%20==0:
        print("Current Step is: {}, Loss: {}"
            .format((i+1),
            temp_loss))

    #print("-----Test Accuracy: {}-----".format(get_accuracy(logits=sess.run(y_,feed_dict={x_data:x_test}),labels=y_test)))

当我运行我的程序时,我总是在训练时将损失值设为0。我不确定问题可能在哪里。但我有一些想法。

1)这可能是我创建批量数据的方式吗?这似乎是一种不寻常的方式,但就我而言,它应该通过获取rand_index = np.random.choice(len(x_train),size=BATCH_SIZE)中的随机索引来工作。

2)这没有意义,但可能是因为数据是"小数据"?

3)代码中的任何简单错误?

4)或者我的确失败为0.(这是最不可能的情况)

如果你能在上面的代码中另外指出我应该避免做什么,我会非常高兴。

感谢。

1 个答案:

答案 0 :(得分:2)

我运行了你的代码,这些是我发现的错误。

  1. 您的输入数据看起来像是字符串。你应该将它转换为浮动。
  2. 不要在最后一层使用relu。它应该直接输入损失函数而没有非线性。
  3. 您应该使用sigmoid_cross_entropy_with_logits功能而不是softmax功能。 Sigmoid用于二元分类,softmax用于多类分类。
  4. 您的学习率可能过高。我会尝试下一个。