gpt4 book ai didi

machine-learning - 神经网络模型不学习?

转载 作者:行者123 更新时间:2023-11-30 09:21:16 27 4
gpt4 key购买 nike

我尝试使用 softmax 回归对神经网络进行建模。经过 999 次迭代后,每个数据点的误差约为 0.02%,我认为这很好。但是当我在张量板上可视化模型时,我的成本函数没有达到 0,而是得到类似 this 的结果。

对于权重和偏差直方图 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)

最佳答案

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

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

关于machine-learning - 神经网络模型不学习?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com