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

我正在尝试编写一个双层神经网络来训练一个类标签。 网络输入是一个包含约1000个示例的150个特征列表; 所有例子的所有特征都被L2标准化了。

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

我的代码相对简单; 我将输入数据输入到隐藏层,然后将隐藏层输入到输出中。 由于我真的只想看到这一行动的实施,我正在对每一步的整个数据集进行培训。

我的代码如下。 基于我提到的其他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交叉熵,因为您假设输出在Wikipedia上是不相交的softmax。 通过这个小小的修改,我实现了更快的融合。 一旦您决定将输出维度从2增加到更高的数字,这也应该可以提高性能。


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

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

没有看到你的代码太多,所以如果这不行,你可以检查udacity深度学习课程或论坛,他们有你想要做的很好的样本。 GL

链接地址: http://www.djcxy.com/p/32079.html

上一篇: Simple Tensorflow Multilayer Neural Network Not Learning

下一篇: Pretrained embedding type error