神经网络模型不学习?

时间:2016-05-23 05:23:23

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

我尝试使用softmax回归建模NN。 经过999次迭代后,我得到的每个数据点的误差约为0.02%,我认为这很好。但是当我在tensorboard上显示模型时,我的成本函数没有达到0,而是我得到类似this WP Core blog post

的东西

对于权重和偏见直方图this

我是初学者,我似乎无法理解错误。可能是我使用错误的方法来定义成本?

以下是我的完整代码供参考。

import tensorflow as tf
import numpy as np
import random

lorange= 1
hirange= 10
amplitude= np.random.uniform(-10,10)
t= 10
random.seed()
tau=np.random.uniform(lorange,hirange)


x_node = tf.placeholder(tf.float32, (10,))
y_node = tf.placeholder(tf.float32, (10,))

W = tf.Variable(tf.truncated_normal([10,10], stddev= .1))
b = tf.Variable(.1)

y = tf.nn.softmax(tf.matmul(tf.reshape(x_node,[1,10]), W) + b)

##ADD SUMMARY

W_hist = tf.histogram_summary("weights", W)
b_hist = tf.histogram_summary("biases", b)
y_hist = tf.histogram_summary("y", y)

# Cost function sum((y_-y)**2)
with tf.name_scope("cost") as scope:
   cost = tf.reduce_mean(tf.square(y_node-y))
   cost_sum = tf.scalar_summary("cost", cost)

# Training using Gradient Descent to minimize cost
with tf.name_scope("train") as scope:
  train_step = tf.train.GradientDescentOptimizer(0.00001).minimize(cost)

sess = tf.InteractiveSession()

# Merge all the summaries and write them out to logfile
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/mnist_logs_4", sess.graph_def)
error = tf.reduce_sum(tf.abs(y - y_node))


init = tf.initialize_all_variables()
sess.run(init)

steps = 1000

for i in range(steps):
    xs = np.arange(t)
    ys = amplitude * np.exp(-xs / tau)

    feed = {x_node: xs, y_node: ys}
    sess.run(train_step, feed_dict=feed)
    print("After %d iteration:" % i)
    print("W: %s" % sess.run(W))
    print("b: %s" % sess.run(b))
    print('Total Error: ', error.eval(feed_dict={x_node: xs, y_node:ys}))
    # Record summary data, and the accuracy every 10 steps
    if i % 10 == 0:
      result = sess.run(merged, feed_dict=feed)
      writer.add_summary(result, i)

1 个答案:

答案 0 :(得分:0)

我和你几次有同样的情节。

这主要发生在我在多个日志文件上运行tensorboard时。也就是说,我给TensorBoard的logdir包含多个日志文件。尝试在一个日志文件上运行TensorBoard,让我知道会发生什么