tf.train

tf.train

前言

tf.train属于Training类,一般用于梯度的计算。由于使用缘故探究下tf.train的简单用法。

官方

1.tf.train.GradientDescentOptimizer

Optimizer that implements the gradient descent algorithm.

tf.train.GradientDescentOptimizer.__init__(learning_rate, use_locking=False, name='GradientDescent')

Construct a new gradient descent optimizer.

Args:

  • learning_rate: A Tensor or a floating point value. The learning rate to use.
  • use_locking: If True use locks for update operation.s
  • name: Optional name prefix for the operations created when applying gradients. Defaults to “GradientDescent”.

Returns:
An Operation that updates the variables in ‘var_list’. If ‘global_step’ was not None, that operation also increments global_step.

2.tf.train.Optimizer.minimize

tf.train.Optimizer.minimize(loss, global_step=None, var_list=None, gate_gradients=1, name=None)

Add operations to minimize ‘loss’ by updating ‘var_list’.

This method simply combines calls compute_gradients() and apply_gradients(). If you want to process the gradient before applying them call compute_gradients() and apply_gradients() explicitly instead of using this function.

Args:

  • loss: A Tensor containing the value to minimize.
  • global_step: Optional Variable to increment by one after the variables have been updated.
  • var_list: Optional list of variables.Variable to update to minimize ‘loss’. Defaults to the list of variables collected in the graph under the key GraphKeys.TRAINABLE_VARIABLES.
  • gate_gradients: How to gate the computation of gradients. Can be GATE_NONE, GATE_OP, or GATE_GRAPH.
  • name: Optional name for the returned operation.

例子

import tensorflow as tf
import numpy as np

# Creat data
X_Data = np.random.rand(100).astype(np.float32)
Y_Data = X_Data*0.1 + 0.3

# Creat TF Structure Start #
Weight = tf.random_uniform([1], -1.0, 1.0)
biases = tf.Variable(tf.zeros([1]))

y = Weight*X_Data + biases

loss = tf.reduce_mean(tf.square(y-Y_Data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

#Creat TF Structure End

sess = tf.Session()
sess.run(init)  # Important

for step in range(201):
    sess.run(train)
    if step%20 == 0:
        print(step, sess.run(Weight), sess.run(biases))

结果
这里写图片描述

理解

  • tf.train.Optimizer.minimize中参数loss一般用tf.reduce_mean计算出来

Reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值