tensorflow 一些常用类和方法

tf.nn,tf.layers,tf.contrib的区别(V1.0)



三个模块里都可以实现卷积层的操作,一直不太明白他们之间的区别。
tf.nn  提供神经网络相关操作的支持,包括各种卷积操作、激活函数、pooling、归一化、losses、分类操作、        embedding、RNN(dynamic_rnn bidirectional_dynamic_rnn raw_rnn)、Evaluation


tf.layers   这个库提供一系列的高级神经网络层、都是卷积相关的。

tf.contrib.layers 提供 构建计算图中网络层、正则化、摘要操作。是构建计算图的高级操作,但是contrib                   模块包含不稳定和实验性的代码,有可能以后API会改变。内容更丰富,支持的操作更多。


tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

Computes the mean of elements across dimensions of a tensor.

对张量的某个维度上的元素进行平均计算

Reduces input_tensor along the dimensions given in axis. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in axis. If keep_dims is true, the reduced dimensions are retained with length 1.

按照给定的轴axis在某个维度上缩减张量。

If axis has no entries, all dimensions are reduced, and a tensor with a single element is returned.

如果axis没有输入,所有维度都进行缩减,返回只有一个元素的张量。

For example:


  
  
# 'x' is [[1., 1.]
#         [2., 2.]]
tf
.reduce_mean(x) ==> 1.5
tf
.reduce_mean(x, 0) ==> [1.5, 1.5]
tf
.reduce_mean(x, 1) ==> [1.,  2.]
Args:
  • input_tensor: The tensor to reduce. Should have numeric type. 输入张量,数值型
  • axis: The dimensions to reduce. If None (the default), reduces all dimensions.需要缩减的维度,如果空,缩减所有维度
  • keep_dims: If true, retains reduced dimensions with length 1.如果真,保留缩减的维度为1
  • name: A name for the operation (optional).
  • reduction_indices: The old (deprecated) name for axis.老的方式,确定维度。
——————————————————————————————————————————————————————————————————————

tf.argmax(input, axis=None, name=None, dimension=None)

tf.argmax(input, axis=None, name=None, dimension=None)

See the guide: Math > Sequence Comparison and Indexing

Returns the index with the largest value across axes of a tensor.   返回的是张量按轴找最大值的index

Args:
  • input: A Tensor. Must be one of the following types: float32float64int64int32uint8uint16int16int8complex64complex128qint8quint8qint32half.
  • axis: A Tensor. Must be one of the following types: int32int64. int32, 0 <= axis < rank(input). Describes which axis of the input Tensor to reduce across. For vectors, use axis = 0.    0--找每一列   1--找每一行
  • name: A name for the operation (optional).
Returns:

Tensor of type int64. 返回的是最大值的位置,运行sess才能显示值。

Defined in tensorflow/python/ops/math_ops.py.

————————————————————————————————————————————————————————————————————————

tf.reshape(tensor, shape, name=None)

Reshapes a tensor.

Given tensor, this operation returns a tensor that has the same values as tensor with shape shape.

If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.

If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.

如果shape中有一个位置喂-1,是为了其余位置的大小保持不变,-1的位置自适应size。

For example:

  
  
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape
(t, [3, 3]) ==> [[1, 2, 3],
                       
[4, 5, 6],
                       
[7, 8, 9]]

# tensor 't' is [[[1, 1], [2, 2]],
#                [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape
(t, [2, 4]) ==> [[1, 1, 2, 2],
                       
[3, 3, 4, 4]]

# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape
(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]

# -1 can also be used to infer the shape

# -1 is inferred to be 9:
reshape
(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape
(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
                         
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape
(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
                             
[2, 2, 2],
                             
[3, 3, 3]],
                             
[[4, 4, 4],
                             
[5, 5, 5],
                             
[6, 6, 6]]]

# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape
(t, []) ==> 7
Args:
  • tensor: A Tensor.
  • shape: A Tensor of type int32. Defines the shape of the output tensor.
  • name: A name for the operation (optional).
Returns:

Tensor. Has the same type as tensor




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值