首页 文章

TensorFlow代码帮助 - 入门示例

提问于
浏览
2

我需要别人的帮助才能向我解释下面的代码 . 我是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.

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

# 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

2 回答

  • 0

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

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

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

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

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

    y_train代表什么?

    x_trainy_train 是训练数据,第一个是输入,第二个是预期标签 . 在这种情况下,您告诉输入 1 应该导致结果 0 ,输入 21 ,依此类推 . 希望网络能够从这4个例子中找出它应该学习的操作(旁注:神经网络只是一个完全适合的线性模型) . 它被称为监督学习 .

    变量curr_W,curr_b在这里代表什么?

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

  • 0

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

    x是输入,b是那些输入的偏差,W是那些输入的权重矩阵,y是这些输入的目标值 .

    要训练像这里的监督学习模型,您需要一些训练数据 . 你的是这个:

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

    y_train是与输入值[1,2,3,4]对应的目标值 .

    curr_W, curr_b
    

    经过一轮培训后,分别是模型的当前权重和偏差 .

相关问题