简单的Tensorflow多层神经网络不学习



我正在尝试编写一个两层神经网络来训练类标签。网络的输入是大约1000个示例的150次列表;所有示例上的所有功能均已归一化。

我只有两个输出,它们应该是不相交的 - 我只是试图预测示例是一个还是零。

我的代码相对简单;我将输入数据馈送到隐藏层中,然后将隐藏层馈入输出。由于我真的只想看到此操作起作用,因此我正在对整个数据集进行培训。

我的代码在下面。根据我提到的其他NN实现,我认为该网络的性能应该随着时间的推移而改善。但是,无论我设定的时期数量如何,我的准确度约为20%。当更改步骤数时,准确性不会改变,因此我不相信我的权重和偏见正在更新。

我的模型有明显的东西吗?谢谢!

import numpy as np
import tensorflow as tf
sess = tf.InteractiveSession()
# generate data
np.random.seed(10)
inputs = np.random.normal(size=[1000,150]).astype('float32')*1.5
label = np.round(np.random.uniform(low=0,high=1,size=[1000,1])*0.8)
reverse_label = 1-label
labels = np.append(label,reverse_label,1)
# parameters
learn_rate = 0.01
epochs = 200
n_input = 150
n_hidden = 75
n_output = 2
# set weights/biases
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output])
b0 = tf.Variable(tf.truncated_normal([n_hidden]))
b1 = tf.Variable(tf.truncated_normal([n_output]))
w0 = tf.Variable(tf.truncated_normal([n_input,n_hidden]))
w1 = tf.Variable(tf.truncated_normal([n_hidden,n_output]))
# step function
def returnPred(x,w0,w1,b0,b1):
    z1 = tf.add(tf.matmul(x, w0), b0)
    a2 = tf.nn.relu(z1)
    z2 = tf.add(tf.matmul(a2, w1), b1)
    h = tf.nn.relu(z2)
    return h  #return the first response vector from the 
y_ = returnPred(x,w0,w1,b0,b1) # predict operation
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_,labels=y) # calculate loss between prediction and actual
model = tf.train.GradientDescentOptimizer(learning_rate=learn_rate).minimize(loss) # apply gradient descent based on loss
init = tf.global_variables_initializer() 
tf.Session = sess
sess.run(init) #initialize graph
for step in range(0,epochs):
    sess.run(model,feed_dict={x: inputs, y: labels }) #train model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: inputs, y: labels})) # print accuracy

我将您的优化器更改为adamoptimizer(在许多情况下,其性能优于GradientDescentOptimizer(。

我还使用参数玩了一些。特别是,我为您的可变初始化,学习率降低(由于您的损失不稳定并且"四处跳跃"(和增加时代(我注意到您的损失继续下降(。

我还减少了隐藏层的大小。当您没有太多数据时,用大型隐藏层进行训练网络更难。

关于您的损失,最好在其上应用tf.reduce_mean,以便损失是一个数字。此外,在ML4294的答案之后,我使用了SoftMax而不是Sigmoid,因此损失看起来像:

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y))

下面的代码在培训数据上达到约99.9%的准确性:

import numpy as np
import tensorflow as tf
sess = tf.InteractiveSession()
# generate data
np.random.seed(10)
inputs = np.random.normal(size=[1000,150]).astype('float32')*1.5
label = np.round(np.random.uniform(low=0,high=1,size=[1000,1])*0.8)
reverse_label = 1-label
labels = np.append(label,reverse_label,1)
# parameters
learn_rate = 0.002
epochs = 400
n_input = 150
n_hidden = 60
n_output = 2
# set weights/biases
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output])
b0 = tf.Variable(tf.truncated_normal([n_hidden],stddev=0.2,seed=0))
b1 = tf.Variable(tf.truncated_normal([n_output],stddev=0.2,seed=0))
w0 = tf.Variable(tf.truncated_normal([n_input,n_hidden],stddev=0.2,seed=0))
w1 = tf.Variable(tf.truncated_normal([n_hidden,n_output],stddev=0.2,seed=0))
# step function
def returnPred(x,w0,w1,b0,b1):
    z1 = tf.add(tf.matmul(x, w0), b0)
    a2 = tf.nn.relu(z1)
    z2 = tf.add(tf.matmul(a2, w1), b1)
    h = tf.nn.relu(z2)
    return h  #return the first response vector from the 
y_ = returnPred(x,w0,w1,b0,b1) # predict operation
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y)) # calculate loss between prediction and actual
model = tf.train.AdamOptimizer(learning_rate=learn_rate).minimize(loss) # apply gradient descent based on loss

init = tf.global_variables_initializer()
tf.Session = sess
sess.run(init) #initialize graph
for step in range(0,epochs):
    sess.run([model,loss],feed_dict={x: inputs, y: labels }) #train model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: inputs, y: labels})) # print accuracy

只是Miriam Farber提供的答案之外的一个建议:您使用多维输出标签([0.,1。](进行分类。我建议使用SoftMax交叉熵tf.nn.softmax_cross_entropy_with_logits()代替Sigmoid Cross熵,因为您假设输出是Wikipedia上的Softmax。通过这种小的修改,我实现了更快的收敛速度。一旦决定将输出维度从2增加到更高的数字,这也应该提高您的性能。

我想您在这里有一些问题:损失= tf.nn.sigmoid_cross_entropy_with_logits(logits = y_,labels = y(#计算预测与实际

之间的损失

看起来应该像这样:损失= tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = y_,labels = y((

观看您的代码并不多,因此,如果这无法解决,您可以检查Udacity深入学习课程或论坛,他们有一个好的样本,您想做的是。gl

最新更新