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

时间:2017-04-22 16:07:45

标签: python machine-learning tensorflow neural-network deep-learning

我正在尝试编写一个双层神经网络来训练一个类贴标机。网络输入是一个包含大约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

3 个答案:

答案 0 :(得分:5)

我将优化程序更改为AdamOptimizer(在许多情况下,它的效果优于GradientDescentOptimizer)。

我也玩了一些参数。特别是,我采用较小的std进行变量初始化,降低了学习率(因为你的损失不稳定而且“跳过”)和增加了时代(因为我注意到你的损失继续减少)。

我还缩小了隐藏图层的大小。当您没有那么多数据时,很难训练具有大隐藏层的网络。

关于您的损失,最好在其上应用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

答案 1 :(得分:2)

除了Miriam Farber提供的答案之外,还有一个建议: 您使用多维输出标签([0,1。])进行分类。我建议使用softmax交叉熵tf.nn.softmax_cross_entropy_with_logits()而不是sigmoid交叉熵,因为你假设输出是不相交的softmax on Wikipedia。通过这个小修改,我实现了更快的收敛。 一旦您决定将输出维度从2增加到更高的数字,这也应该会改善您的表现。

答案 2 :(得分:0)

我猜你在这里遇到了一些问题: 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