- tf.placeholder
tf.placeholder(
dtype,
shape=None,
name=None
)
实例:
self.tfs = tf.placeholder(tf.float32, [None, S_DIM], 'state')
None表示长度待定
- tf.reduce_mean
用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
reduce_mean(input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None)
第一个参数input_tensor: 输入的待降维的tensor;
第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
第四个参数name: 操作的名称;
第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;
- tf.layer.dense
tf.layers.dense(
inputs,
units,
activation=None,
use_bias=True,
kernel_initializer=None, ##卷积核的初始化器
bias_initializer=tf.zeros_initializer(), ##偏置项的初始化器,默认初始化为0
kernel_regularizer=None, ##卷积核的正则化,可选
bias_regularizer=None, ##偏置项的正则化,可选
activity_regularizer=None, ##输出的正则化函数
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None, ##层的名字
reuse=None ##是否重复使用参数
)
inputs:输入该网络层的数据
units:输出的维度大小,改变inputs的最后一维
activation:激活函数,即神经网络的非线性变化
use_bias:使用bias为True(默认使用),不用bias改成False即可,是否使用偏置项
trainable=True:表明该层的参数是否参与训练。如果为真则变量加入到图集合中
- tf.distributions.Normal()
该函数定义了一个正态分布
tf.distributions.Normal(loc=0,scale=1) #这个是标准正态分布
正态分布选取动作
- tf.get_collection()
主要作用:从一个集合中取出变量
tf.get_collection(
key,
scope=None
)
- sess.run()
def run(self, fetches, feed_dict=None, options=None, run_metadata=None):
为了取回(Fetch)操作的输出内容, 可以在使用 Session 对象的 run()调用执行图时,传入一些 tensor, 这些 tensor 会帮助你取回结果。
在python语言中,返回的tensor是numpy ndarray对象。
-
fetches
可以是单个图元素(single graph element),也可以是任意嵌套的列表list,元组tuple,名称元组namedtuple,字典dict或包含图元素的OrderedDict。 -
feed_dict
可选参数 feed_dict允许调用者替换图中张量的值(the value of tensors in the graph)。 -
options
可选的options参数需要一个RunOptions原型。 选项允许控制该特定步骤的行为(例如,打开跟踪)。 -
run_metadata
可选的run_metadata参数需要一个RunMetadata原型。 适当时,将在那里收集此步骤的非Tensor输出。 例如,当用户在options中打开跟踪时,配置信息将被收集到此参数中并传回。
例子:
a = tf.constant([10, 20])
b = tf.constant([1.0, 2.0])
# 'fetches'可以是一个单例
v = session.run(a)
# v is the numpy array [10, 20]
# 'fetches' can be a list.
v = session.run([a, b])
# v is a Python list with 2 numpy arrays: the 1-D array [10, 20] and the
# 1-D array [1.0, 2.0]
# 'fetches' can be arbitrary lists, tuples, namedtuple, dicts:
MyData = collections.namedtuple('MyData', ['a', 'b'])
v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
# v is a dict with
# v['k1'] is a MyData namedtuple with 'a' (the numpy array [10, 20]) and
# 'b' (the numpy array [1.0, 2.0])
# v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
# [10, 20].
- tf.Variable()定义变量
使用Variable必须进行初始化
initializa_op = tf.global_variables_initializer()
- .prob(self, value, name=“prob”)
概率密度/质量函数
参数:
value:float
ordouble
Tensor
.
name: Pythonstr
prepended to names of ops created by this function.
返回值:
prob: a Tensor
of shape sample_shape(x) + self.batch_shape
with
values of type self.dtype
.
- tf. reduce_mean()
def reduce_mean_v1(input_tensor,
axis=None,
keepdims=None,
name=None,
reduction_indices=None,
keep_dims=None):
计算张量维度间元素的均值
例子:
x = tf.constant([[1., 1.], [2., 2.]])
tf.reduce_mean(x) # 1.5,不指定轴则所有方向上减1
tf.reduce_mean(x, 0) # [1.5, 1.5]
tf.reduce_mean(x, 1) # [1., 2.]
参数:
input_tensor: 要缩减的张量。 应该有数字类型。
axis: 要减少的轴. If None
(the default), reduces all
dimensions. Must be in the range [-rank(input_tensor), rank(input_tensor))
.
keepdims: 如果为真,则保留长度为1的缩减尺寸。
name: 操作的名称(可选)。
reduction_indices: axis的旧(已弃用)名称。
keep_dims: Deprecated alias for keepdims
.
返回值:
The reduced tensor.
- tf.minimum()
def minimum(x, y, name=None)
返回x和y的最小值(即x < y ? X: y)元素。
-
参数:
x: ATensor
. Must be one of the following types:bfloat16
,half
,float32
,float64
,int32
,int64
.
y: ATensor
. Must have the same type asx
.
name: A name for the operation (optional). -
返回值:
ATensor
. Has the same type asx
.
- tf.clip_by_value():
def clip_by_value(t, clip_value_min, clip_value_max,
name=None):
剪辑张量值到指定的最小和最大。
给定一个张量t
,这个操作返回一个相同类型的张量和 Shape为t
,其值被剪切为 clip_value_min
和clip_value_max
。任何小于clip_value_min
的值都被设置为clip_value_min
。 任何值大于clip_value_max
将被设置为clip_value_max
。
注意:clip_value_min
需要小于或等于clip_value_max
。
- 参数:
t: ATensor
orIndexedSlices
.
clip_value_min: A 0-D (scalar)Tensor
, or aTensor
with the same shape ast
. The minimum value to clip by.
clip_value_max: A 0-D (scalar)Tensor
, or aTensor
with the same shape ast
. The maximum value to clip by.
name: A name for the operation (optional). - 返回值:
A clippedTensor
orIndexedSlices
.
- tf.squeeze()
def squeeze(input, axis=None, name=None, squeeze_dims=None):
从张量的形状中去除尺寸为1的维度
Given a tensor input
, this operation returns a tensor of the same type with
all dimensions of size 1 removed. If you don’t want to remove all size 1
dimensions, you can remove specific size 1 dimensions by specifying
axis
.
- 参数:
input: ATensor
. Theinput
to squeeze.
axis: An optional list ofints
. Defaults to[]
. If specified, only
squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Must be in the range[-rank(input), rank(input))
.Must be specified ifinput
is aRaggedTensor
.
name: A name for the operation (optional).
squeeze_dims:已弃用的关键字参数,现在是轴 - 返回值:
ATensor
. Has the same type asinput
.
Contains the same data asinput
, but has one or more dimensions of
size 1 removed.
OpenAI Gym
import gym # 导入库
env = gym.make('CartPole-v0')
env.reset() # 初始化环境
# 循环执行一些时间步长,并在每个步长内渲染环境
for _ in range(1000):
env.render()
env.step(env.action_space.sample()