Python 简单张量流多层神经网络不学习

Python 简单张量流多层神经网络不学习,python,machine-learning,tensorflow,neural-network,deep-learning,Python,Machine Learning,Tensorflow,Neural Network,Deep Learning,我正试图编写一个两层神经网络来训练一个班级贴标员。网络的输入是一个150个特征列表,包含大约1000个示例;所有示例上的所有功能都已被L2规范化 我只有两个输出,它们应该是不相交的——我只是试图预测示例是1还是0 我的代码相对简单;我将输入数据输入到隐藏层,然后将隐藏层输入到输出。因为我真的只想看到它的实际效果,所以我正在每一步都对整个数据集进行培训 我的代码如下。基于我提到的其他NN实现,我认为该网络的性能应该随着时间的推移而提高。然而,不管我设置了多少个纪元,我得到了大约20%的准确率。当步

我正试图编写一个两层神经网络来训练一个班级贴标员。网络的输入是一个150个特征列表,包含大约1000个示例;所有示例上的所有功能都已被L2规范化

我只有两个输出,它们应该是不相交的——我只是试图预测示例是1还是0

我的代码相对简单;我将输入数据输入到隐藏层,然后将隐藏层输入到输出。因为我真的只想看到它的实际效果,所以我正在每一步都对整个数据集进行培训

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

我想你在这里遇到了一些问题: 损失=tf.nn.sigmoid_交叉_熵_与_logits(logits=y,labels=y)#计算预测和实际之间的损失

它看起来应该是这样的: 损失=tf.reduce\u均值(tf.nn.sigmoid\u交叉\u熵\u与logits(logits=y,labels=y))

我没怎么看你的代码,所以如果这不起作用,你可以查看udacity深度学习课程或论坛,他们有你想要做的很好的示例。
GL

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

我还玩了一些参数。特别是,我为您的可变初始化选择了较小的std,降低了学习率(因为您的损失不稳定且“跳跃”)并增加了历次(因为我注意到您的损失持续减少)

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

关于你的损失,最好应用
tf。减少它的意思是
,这样损失将是一个数字。此外,根据的答案,我使用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

除了以下人员提供的答案之外,还有一个建议: 分类使用多维输出标签([0.,1.])。我建议使用softmax交叉熵
tf.nn.softmax\u cross\u entropy\u with_logits()
,而不是sigmoid交叉熵,因为您假设输出是不相交的。通过这个小小的修改,我实现了更快的收敛。
一旦您决定将输出维度从2增加到一个更高的数字,这也会提高您的性能。

如果您可以生成一些“玩具”输入和标签(不必是您的特定输入,您可以使用numpy random),这将有助于读卡器运行您的codeHi Miriam。我已经更新了我的代码,根据您的请求包含一些“玩具”输入。谢谢好建议!在我的解决方案中,我也将其更改为softmax,并将历元数减少到400(从原来的2000年)。它提供了99.9%的训练数据准确性:)你好,ml。我会尝试用这个解决方案更新模型,并告诉你我发现了什么。谢谢非常感谢您的反馈,Miriam!我做了你建议的更新,我的模型现在运行良好!那里真的需要一些帮助,你让我走了!谢谢你的反馈!是的,这是有道理的——模型已经更新以反映这一点。