我跟特遣部队在一起
Getting Started
,我们在线性模型上做一个简单的梯度下降,对它做一个小的调整,会导致一个问题,我将其用作学习tf调试器的测试用例。这是密码来自
开始
:
import tensorflow as tf
from tensorflow.python import debug as tf_debug
sess = tf.Session()
# sess = tf_debug.LocalCLIDebugWrapperSession(sess)
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
model = W * x + b
y = tf.placeholder(tf.float32)
sq_deltas = tf.square(model - y)
loss = tf.reduce_sum(sq_deltas)
init = tf.global_variables_initializer()
sess.run(init)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
for i in range(1000):
sess.run(train, {x: list(range(1, 8)),
y: list(range(0, -7, -1))})
out = sess.run([W, b])
现在
W
和
b
有分歧:
>>> print(out)
[array([nan], dtype=float32), array([nan], dtype=float32)]
通知
sess.run(train, ...)
我的示例使用7个示例的数据集,而它们的示例使用4个示例:
{
x : np.array([1., 2., 3., 4.]),
y : np.array([0., -1., -2., -3.])
}
梯度正在发散,所以如果我使用这个,它可以再次正确地解决问题,尽管速度很慢:
optimizer = tf.train.GradientDescentOptimizer(0.001)
所以我可以跳到调试模式:
python -m mything.py --debug
> run # runs sess.run(init)
> run -f has_inf_or_nan # runs gradient descent, filters for inf/nan
# Square:0 was found to have inf
> pt Square:0 # all at or approaching inf
> ni Square # I'm not sure what do do with this
> ni -t Square # buried in the output, my code line:
# sq_deltas = tf.square(model - y)
但现在我迷路了,调试器对我来说仍然有点难以捉摸。
1)我如何找到这些的来源
inf
S,而且重要的是,2)我做了什么,使这个简单的线性模型不能扩展到更大的数据集?