TensorFlow示例如何实际更新权重以找到解决方案

斯图霍

tensorflow,python和numpy的新手(我想这是示例中的全部内容)

在下面的代码中,我(几乎)了解到循环中的update_weights.run()调用正在计算损失并开发新的权重。我看不到这实际上是如何导致权重发生变化的。

我坚持的观点已被评论#这是我不了解的

update_weights.run()和放置在权重中的新值之间是什么关系?- 也许; 在值更改的循环之后调用weights.eval时会怎样?

谢谢你的帮助

#@test {"output": "ignore"}

# Import tf
import tensorflow as tf

# Numpy is Num-Pie n dimensional arrays
# https://en.wikipedia.org/wiki/NumPy
import numpy as np

# Plotting library
# http://matplotlib.org/users/pyplot_tutorial.html
import matplotlib.pyplot as plt

# %matplotlib magic
# http://ipython.readthedocs.io/en/stable/interactive/tutorial.html#magics-explained
%matplotlib inline

# Set up the data with a noisy linear relationship between X and Y.
# Variable?
num_examples = 5
noise_factor = 1.5
line_x_range = (-10,10)

#Just variables in Python
# np.linspace - Return evenly spaced numbers over a specified interval.
X = np.array([
        np.linspace(line_x_range[0], line_x_range[1], num_examples), 
        np.linspace(line_x_range[0], line_x_range[1], num_examples)
    ])

# Plot out the starting data
# plt.figure(figsize=(4,4))
# plt.scatter(X[0], X[1])
# plt.show()

# npm.random.randn - Return a sample (or samples) from the “standard normal” distribution.
# Generate noise for x and y (2)
noise = np.random.randn(2, num_examples) * noise_factor

# plt.figure(figsize=(4,4))
# plt.scatter(noise[0],noise[1])
# plt.show()

# += on an np.array
X += noise

# The 'Answer' polyfit to the noisy data
answer_m, answer_b = np.polyfit(X[0], X[1], 1)


# Destructuring Assignment - http://codeschool.org/python-additional-miscellany/
x, y = X

# plt.figure(figsize=(4,4))
# plt.scatter(x, y)
# plt.show()

# np.array
# for a in x
#  [(1., a) for a in [1,2,3]] => [(1.0, 1), (1.0, 2), (1.0, 3)]
# numpy.ndarray.astype - http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html 
# Copy of the array, cast to a specified type.
x_with_bias = np.array([(1., a) for a in x]).astype(np.float32)

#Just variables in Python
# The difference between our current outputs and the training outputs over time
# Starts high and decreases
losses = []
history = []
training_steps = 50
learning_rate = 0.002

# Start the session and give it a variable name sess 
with tf.Session() as sess:
  # Set up all the tensors, variables, and operations.
  # Creates a constant tensor
  input = tf.constant(x_with_bias)
  # Transpose the ndarray y of random float numbers
  target = tf.constant(np.transpose([y]).astype(np.float32))
  # Start with random weights
  weights = tf.Variable(tf.random_normal([2, 1], 0, 0.1))

  # Initialize variables ...?obscure?
  tf.initialize_all_variables().run()
  print('Initialization complete')

  # tf.matmul - Matrix Multiplication
  # What are yhat? Why this name?
  yhat = tf.matmul(input, weights)

  # tf.sub - Matrix Subtraction
  yerror = tf.sub(yhat, target)

  # tf.nn.l2_loss - Computes half the L2 norm of a tensor without the sqrt
  # loss function?
  loss = tf.nn.l2_loss(yerror)

  # tf.train.GradientDescentOptimizer - Not sure how this is updating the weights tensor?
  # What is it operating on?
  update_weights = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

  # _ in Python is conventionally used for a throwaway variable
  for step in range(training_steps):
    # Repeatedly run the operations, updating the TensorFlow variable.
    # THIS IS WHAT I DONT UNDERSTAND
    update_weights.run()
    losses.append(loss.eval())
    b, m = weights.eval()
    history.append((b,m,step))

  # Training is done, get the final values for the graphs
  betas = weights.eval()
  yhat = yhat.eval()

# Show the fit and the loss over time.
# destructuring assignment
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)

# Adjust whitespace between plots
plt.subplots_adjust(wspace=.2)

# Output size of the figure
fig.set_size_inches(12, 4)

ax1.set_title("Final Data Fit")
ax1.axis('equal')
ax1.axis([-15, 15, -15, 15])

# Scatter plot data x and y (pairs?) set with 60% opacity
ax1.scatter(x, y, alpha=.6)
# Scatter plot x and np.transpose(yhat)[0] (must be same length), in red, 50% transparency
# these appear to be the x values mapped onto the 
ax1.scatter(x, np.transpose(yhat)[0], c="r", alpha=.5)

# Add the line along the slope defined by betas (whatever that is)
ax1.plot(line_x_range, [betas[0] + a * betas[1] for a in line_x_range], "g", alpha=0.6)

# This polyfit coefficients are reversed in order vs the betas
ax1.plot(line_x_range, [answer_m * a + answer_b for a in line_x_range], "r", alpha=0.3)


ax2.set_title("Loss over Time")

# Create a range of intefers from 0 to training_steps and plot the losses as a curve
ax2.plot(range(0, training_steps), losses)

ax2.set_ylabel("Loss")
ax2.set_xlabel("Training steps")

ax3.set_title("Slope over Time")
ax3.axis('equal')
ax3.axis([-15, 15, -15, 15])

for b, m, step in history:
  ax3.plot(line_x_range, [b + a * m for a in line_x_range], "g", alpha=0.2)

# This line seems to be superfluous removing it doesn't change the behaviour
plt.show()
詹杜特

好的,所以update_weights()在您定义为预测与目标之间的误差的损失上调用最小化器。

它将要做的是将一些小的权重添加(权重由learning_rate参数控制),以使您的损失减少,从而使您的预测“更真实”。

这是在您调用update_weights()时发生的情况,因此在调用之后,您的权重已从较小的值更改,并且如果一切都按计划进行,则损失值将减少。

您想要的是跟踪损失的演变情况,权重例如可以查看损失是否确实在减少(并且您的算法有效),或者权重变化很大,或者可以将其可视化。

通过可视化损失的变化方式,您可以获得很多见识。这就是为什么您必须查看参数和损耗演化的完整历史记录的原因。这就是为什么您在每个步骤都评估它们的原因。

当您在最小化器上进行评估或运行时,评估或运行操作有所不同,而在最小化器上进行评估时,对参数进行评估时,它将对加权应用最小化器我强烈建议您阅读此网站,在该网站上作者的解释要比我好得多,并且提供了更多详细信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何找到解决任务的数学解决方案?

来自分类Dev

如何使用Matlab找到此解决方案?

来自分类Dev

如何使用Matlab找到此解决方案?

来自分类Dev

如何使用 PuLP 找到次优(次优)解决方案?

来自分类Dev

在哪里可以找到Windows 10通用应用程序的示例解决方案?

来自分类Dev

如何显示解决方案?

来自分类Dev

在TopCoder Arena中找到解决方案

来自分类Dev

如何跟踪CRM解决方案中所做的更改(例如,版本控制)?

来自分类Dev

请找到此查询的解决方案我该如何解决这个问题

来自分类Dev

如何在Linux“ man”命令中搜索单词或短语并循环找到找到的解决方案?

来自分类Dev

如何恢复解决冲突的解决方案?

来自分类Dev

丢失更新解决方案

来自分类Dev

如何通过Django渠道共享文件,现在有不完整的解决方案,例如S3存储桶中的所有解决方案

来自分类Dev

选择实际解决方案的正确方法是什么?

来自分类Dev

比Evernote更好的代码片段/示例解决方案?

来自分类Dev

如何在解决方案中找到哪些项目引用了特定的dll?

来自分类Dev

如何在C#项目(解决方案)中找到所有硬编码值?

来自分类Dev

如何在VSPackage中找到解决方案使用哪个版本控制系统

来自分类Dev

如何找到两个数组的交集(最佳解决方案)

来自分类Dev

如何优化解决方案以获得线性性能以找到直方图的孔总面积?

来自分类Dev

如何遍历所有可能的路径以找到解决方案并选择最佳路径

来自分类Dev

未捕获的TypeError:$(...)。froalaEditor未找到函数//解决方案,但不确定如何使用它

来自分类Dev

如何在解决方案中找到哪些项目引用了特定的dll?

来自分类Dev

找到第一个解决方案后如何终止回溯递归

来自分类Dev

如何在我的解决方案中找到所有构建事件

来自分类Dev

如何在R中的一个函数中找到多个解决方案?

来自分类Dev

无法使用Sympy解决方案找到符号解决方案

来自分类Dev

A *:使用给定的解决方案为15平方拼图找到更好的解决方案

来自分类Dev

即使给出了一个解决方案,fsolve 也无法找到解决方案

Related 相关文章

  1. 1

    如何找到解决任务的数学解决方案?

  2. 2

    如何使用Matlab找到此解决方案?

  3. 3

    如何使用Matlab找到此解决方案?

  4. 4

    如何使用 PuLP 找到次优(次优)解决方案?

  5. 5

    在哪里可以找到Windows 10通用应用程序的示例解决方案?

  6. 6

    如何显示解决方案?

  7. 7

    在TopCoder Arena中找到解决方案

  8. 8

    如何跟踪CRM解决方案中所做的更改(例如,版本控制)?

  9. 9

    请找到此查询的解决方案我该如何解决这个问题

  10. 10

    如何在Linux“ man”命令中搜索单词或短语并循环找到找到的解决方案?

  11. 11

    如何恢复解决冲突的解决方案?

  12. 12

    丢失更新解决方案

  13. 13

    如何通过Django渠道共享文件,现在有不完整的解决方案,例如S3存储桶中的所有解决方案

  14. 14

    选择实际解决方案的正确方法是什么?

  15. 15

    比Evernote更好的代码片段/示例解决方案?

  16. 16

    如何在解决方案中找到哪些项目引用了特定的dll?

  17. 17

    如何在C#项目(解决方案)中找到所有硬编码值?

  18. 18

    如何在VSPackage中找到解决方案使用哪个版本控制系统

  19. 19

    如何找到两个数组的交集(最佳解决方案)

  20. 20

    如何优化解决方案以获得线性性能以找到直方图的孔总面积?

  21. 21

    如何遍历所有可能的路径以找到解决方案并选择最佳路径

  22. 22

    未捕获的TypeError:$(...)。froalaEditor未找到函数//解决方案,但不确定如何使用它

  23. 23

    如何在解决方案中找到哪些项目引用了特定的dll?

  24. 24

    找到第一个解决方案后如何终止回溯递归

  25. 25

    如何在我的解决方案中找到所有构建事件

  26. 26

    如何在R中的一个函数中找到多个解决方案?

  27. 27

    无法使用Sympy解决方案找到符号解决方案

  28. 28

    A *:使用给定的解决方案为15平方拼图找到更好的解决方案

  29. 29

    即使给出了一个解决方案,fsolve 也无法找到解决方案

热门标签

归档