gpt4 book ai didi

python - TensorFlow 代码帮助 - 入门示例

转载 作者:行者123 更新时间:2023-11-30 08:49:10 25 4
gpt4 key购买 nike

我需要有人帮助我解释下面的代码。我对 TensorFlow 不太熟悉,但我在下面的代码中定义了具体问题

import tensorflow as tf

# Model parameters
#Why are variables initialize with .3 and -.3
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)

x、b、W 和 y 变量代表什么?

# Model input and output
x = tf.placeholder(tf.float32) # this is the input
linear_model = W * x + b # this is the linear_model operation
y = tf.placeholder(tf.float32) # Is this the output we're trying to predict.

为什么代码向 GradientDescentOptimizer 函数传递参数值 0.01?

# loss - measure how far apart the current model is from the provided data.
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01) # Why are we passing the value '0.01'
train = optimizer.minimize(loss)

这里的y_train代表什么?

# training data
x_train = [1, 2, 3, 4] # the input variables we know
y_train = [0, -1, -2, -3] #

# training loop
init = tf.global_variables_initializer() # init is a handle to the TensorFlow sub-graph that initializes all the global variables. Until we call sess.run, the variables are unitialized
sess = tf.Session() # Sesson encapsulates the control and state of the TensorFlow runtime. ITs used to evaluate the nodes, we must run the computational graph within a session
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})

这里变量curr_W、curr_b代表什么?

# evaluate training accuracy
# Why does the variables W and b represent?
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

代码示例来自Tensorflow网站:https://www.tensorflow.org/get_started/get_started#complete_program

最佳答案

What does the x, b, W and y variables represent?

这些是模型将使用的符号变量 - 输入、输出和神经网络参数。xy 是数据,它们不会改变,这就是它们被定义为 tf.placeholder 的原因。 Wy 是可学习参数(在 TF 术语中可训练)。初始值不如这些参数的维度重要(事实上, not exactly ,但这是一个高级主题)。在此示例中,Wb 都是一维,但通常 W 是矩阵,b 是一个向量。

所有定义的变量一起形成一个所谓的计算图

Why is the code passing in a parameter value of 0.01 to the GradientDescentOptimizer function?

这是学习率。简单来说,它是引擎优化目标函数loss时所采用的步长。学习率通常接近于 0,但确切的值取决于许多因素。事实上,它是研究人员手动尝试的常见超参数之一。 0.01 似乎是一个很好的起点,因为在很多情况下它已经足够好了。

What does y_train represent here?

x_trainy_train 是训练数据,第一个是输入,第二个是预期标签。在本例中,您告诉输入 1 应该导致结果 0,输入 2 应该导致 1 等等在。希望网络能够从这 4 个示例中找出它应该学习“减一”操作(旁注:神经网络只是一个完美拟合的线性模型)。这就是所谓的“监督学习”。

What does the variables curr_W, curr_b represent here?

首先请注意,curr_Wcurr_b是普通的Python变量,而Wb是符号变量。符号变量定义了计算的组织方式,并且它们在训练期间采用不同的值。 curr_Wcurr_b 只是经过一些迭代后的值之一。基本上,您拍摄模型的快照并将其打印出来,以查看神经网络学到了什么。结果值 -11(几乎)意味着神经网络成功进行了线性变换。

关于python - TensorFlow 代码帮助 - 入门示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258954/

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