我想写一个两层神经网络来训练一个类标签。网络输入是一个包含约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
如果您可以制作一些“玩具”输入和标签(不一定是您的特定输入,您可以随意使用numpy),这样读者就能够运行您的代码 –
Hi Miriam。我已更新我的代码,以根据您的请求包含一些“玩具”输入。谢谢! – newtensorflowguy