2

我想写一个两层神经网络来训练一个类标签。网络输入是一个包含约1000个示例的150个特征列表;所有例子的所有特征都被L2标准化了。简单Tensorflow多层神经网络不学习

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

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

我的代码如下。基于我提到的其他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 
+0

如果您可以制作一些“玩具”输入和标签(不一定是您的特定输入,您可以随意使用numpy),这样读者就能够运行您的代码 –

+0

Hi Miriam。我已更新我的代码,以根据您的请求包含一些“玩具”输入。谢谢! – newtensorflowguy

回答

4

我将优化器更改为AdamOptimizer(在许多情况下,它的性能比GradientDescentOptimizer好)。

我也玩过一些参数。特别是,我花了较小的标准进行变量初始化,降低了学习速度(因为你的损失是不稳定和“跳跃”)和增加的时代(因为我注意到你的损失持续减少)。

我也减小了隐藏层的大小。当你没有那么多数据时,很难训练带有大隐藏层的网络。

关于你的损失,最好是对其应用tf.reduce_mean,这样损失就是一个数字。此外,ml4294答案之后,我用SOFTMAX代替乙状结肠,所以损失的样子:

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 
+0

非常感谢你的反馈,Miriam!我提出了你建议的更新,现在我的模型运行良好!真的需要一些帮助,你让我感动! – newtensorflowguy

+0

不客气:) –

0

我猜你在这里的一些问题: 损失= tf.nn.sigmoid_cross_entropy_with_logits(logits = Y_,标签= Y)的预测和实际之间#计算损失

它应该水木清华这样的: 损失= tf.reduce_mean在你的代码很多,所以如果我就不会工作,你可以检查udacity深学习课程(tf.nn.sigmoid_cross_entropy_with_logits(logits = Y_,标签= Y))

我以前不看或论坛他们有你想要做的很好的样本。 GL

+0

感谢您的反馈!是的,这是有道理的 - 模型已经更新,以反映这一点。 – newtensorflowguy

2

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

+0

很棒的建议!我在我的解决方案中将其更改为softmax,并将时代减少到400(从原始2000年开始)。它在训练数据上给出了99.9%的准确度:) –

+0

不客气! :) – ml4294

+0

你好,毫升。我会尝试使用此解决方案更新模型,并让您知道我找到的是什么。谢谢!! – newtensorflowguy