gpt4 book ai didi

python-3.x - 使用 TensorFlow 在 2D 中绘制梯度下降轨迹时的 Traceback

转载 作者:行者123 更新时间:2023-12-04 17:13:17 24 4
gpt4 key购买 nike

我想绘制二维梯度下降的轨迹。不幸的是,我有以下回溯:

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,1]

我正在使用 TensorFlow v 1.13.1,以及 Google COLABPython v 3.6.7

从下面的代码中,我发现变量 target是类 <tf.Tensor 'Placeholder_1:0' shape=(?, 1) dtype=float32> .我试着按照提到的方式喂它feed_dict={features: x, target: y} , 但我仍然有相同的回溯。

这是我用于此任务的代码:

## BLOCK 1

import tensorflow as tf
import numpy as np
from matplotlib import animation, rc
import matplotlib_utils
from IPython.display import HTML, display_html
import matplotlib.pyplot as plt
%matplotlib inline
## BLOCK 2

tf.reset_default_graph()

# generate model data
N = 1000
D = 3
x = np.random.random((N, D))
w = np.random.random((D, 1))
y = x @ w + np.random.randn(N, 1) * 0.20

## Deep Learning steps:
# 1. Get input (features) and true output (target)
features = tf.placeholder(tf.float32, shape=(None, D))
target = tf.placeholder(tf.float32, shape=(None, 1))

weights = tf.get_variable("weights", shape=(D, 1), dtype=tf.float32)

# 2. Compute the "guess" (predictions) based on the features and weights
predictions = features @ weights

# 3. Compute the loss based on the difference between the predictions and the target
loss = tf.reduce_mean((target - predictions) ** 2)

# 4. Update the weights (parameters) based on the gradient descent of the loss
optimizer = tf.train.GradientDescentOptimizer(0.1)
step = optimizer.minimize(loss)

s = tf.Session()
s.run(tf.global_variables_initializer())
_, curr_loss, curr_weights = s.run([step, loss, weights],
feed_dict={features: x, target: y})

我希望以下代码能够正常运行(运行此代码时出现回溯):

## BLOCK 3

# nice figure settings

fig, ax = plt.subplots()
y_true_value = s.run(target)
level_x = np.arange(0, 2, 0.02)
level_y = np.arange(0, 3, 0.02)
X, Y = np.meshgrid(level_x, level_y)
Z = (X - y_true_value[0])**2 + (Y - y_true_value[1])**2
ax.set_xlim(-0.02, 2)
ax.set_ylim(-0.02, 3)
s.run(tf.global_variables_initializer())
ax.scatter(*s.run(target), c='red')
contour = ax.contour(X, Y, Z, 10)
ax.clabel(contour, inline=1, fontsize=10)
line, = ax.plot([], [], lw=2)

# start animation with empty trajectory
def init():
line.set_data([], [])
return (line,)

trajectory = [s.run(predictions)]

# one animation step (make one GD step)
def animate(i):
s.run(step)
trajectory.append(s.run(predictions))
line.set_data(*zip(*trajectory))
return (line,)

anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)

注意:图书馆matplotlib_utils可以查到here !


示例

这是代码完美运行的示例。

如果我运行以下代码而不是第二个 block ,它会显示漂亮的二维梯度下降

y_guess = tf.Variable(np.zeros(2, dtype='float32'))
y_true = tf.range(1, 3, dtype='float32')

loss = tf.reduce_mean((y_guess - y_true + 0.5*tf.random_normal([2]))**2)

optimizer = tf.train.RMSPropOptimizer(0.03, 0.5)
step = optimizer.minimize(loss, var_list=y_guess)

这个轨迹是这样的:

trajectory of gradient descent in 2D

添加这段黑色代码将显示完美的轨迹自动生成器:

## BLOCK 4

try:
display_html(HTML(anim.to_html5_video()))
except (RuntimeError, KeyError):
# In case the build-in renderers are unaviable, fall back to
# a custom one, that doesn't require external libraries
anim.save(None, writer=matplotlib_utils.SimpleMovieWriter(0.001))

现在我想用自己的代码(第二 block 代码)在2D中画出这样的梯度下降轨迹。

最佳答案

使用 tf.session.run 传递 feed_dict 参数。示例:

s.run([step, loss, weights], feed_dict={features: x, target: y})

解释:当计算图上的操作依赖于占位符时,必须提供它们。 s.run(tf.global_variables_initializer()) 等操作不依赖于占位符,因此不传递它们不会引发错误。

关于python-3.x - 使用 TensorFlow 在 2D 中绘制梯度下降轨迹时的 Traceback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022099/

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