Tensorflow 简明教程
TensorFlow - Gradient Descent Optimization
梯度下降优化被认为是数据科学中的一个重要概念。
Gradient descent optimization is considered to be an important concept in data science.
考虑下面所示的步骤来了解梯度下降优化的实现 −
Consider the steps shown below to understand the implementation of gradient descent optimization −
Step 1
包含必需的模块并声明 x 和 y 变量,我们将通过它们来定义梯度下降优化。
Include necessary modules and declaration of x and y variables through which we are going to define the gradient descent optimization.
import tensorflow as tf
x = tf.Variable(2, name = 'x', dtype = tf.float32)
log_x = tf.log(x)
log_x_squared = tf.square(log_x)
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(log_x_squared)
Step 2
初始化必要的变量,并调用优化器为其定义并使用各自函数调用它。
Initialize the necessary variables and call the optimizers for defining and calling it with respective function.
init = tf.initialize_all_variables()
def optimize():
with tf.Session() as session:
session.run(init)
print("starting at", "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
for step in range(10):
session.run(train)
print("step", step, "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
optimize()
上面这一行代码生成的输出如以下截图所示 −
The above line of code generates an output as shown in the screenshot below −

我们可以看到必要的 epoch 和迭代如输出所示已经计算出来。
We can see that the necessary epochs and iterations are calculated as shown in the output.