tensorflow中使用的一些函数

1.np.mgrid生成网格

np.mgrid[start?step]
start:开始坐标,end:结束坐标(不包括),step:步长

例如:res = np.mgrid[-3:3:.1,-3:3:.1]
生成从-3到3的二维坐标,第一部分是y轴的范围,第二部分是x轴的范围,返回数组的res[0]是y轴,res[1]是x轴

2.python中type dtype astype 的用法

1.type 获取数据类型

2.dtype 数组元素的类型

3.astype 修改数据类型

3.np.expand_dims:用于扩展数组的形状

原始数组:

import numpy as np

In [12]:
a = np.array([[[1,2,3],[4,5,6]]])
a.shape
Out[12]:
(1, 2, 3)

np.expand_dims(a, axis=0)表示在0位置添加数据,转换结果如下:

In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])

In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)

np.expand_dims(a, axis=2)表示在2位置添加数据,转换结果如下:

In [17]:
d = np.expand_dims(a, axis=2)
d
Out[17]:
array([[[[1, 2, 3]],
        [[4, 5, 6]]]])

In [18]:
d.shape
Out[18]:
(1, 2, 1, 3)

4.enumerate() 函数用于将一个可遍历的数据对象组合为一个索引序列

enumerate(sequence, [start=0])
sequence – 一个序列、迭代器或其他支持迭代对象。
start – 下标起始位置。
实例:

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))       # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
...     print i, element
... 
0 one
1 two
2 three

5.tf.cast()转换格式

cast(
    x,
    dtype,
    name=None
)

将x的数据格式转化成dtype.例如,原来x的数据格式是bool,
那么将其转化成float以后,就能够将其转化成0和1的序列。反之也可以

6.tf.reduce_sum计算一个张量的各个维度上元素的总和

reduce_sum ( 
    input_tensor , 
    axis = None , 
    keep_dims = False , 
    name = None , 
    reduction_indices = None
 )

函数中的input_tensor是按照axis中已经给定的维度来减少的;除非 keep_dims 是true,否则张量的秩将在axis的每个条目中减少1;如果keep_dims为true,则减小的维度将保留为长度1。
如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量
例如:

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x)  # 6
tf.reduce_sum(x, 0)  # [2, 2, 2]
tf.reduce_sum(x, 1)  # [3, 3]
tf.reduce_sum(x, 1, keep_dims=True)  # [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 6

参数:
input_tensor:要减少的张量。应该有数字类型。
axis:要减小的尺寸,表示在那个维度进行sum操作。如果为None(默认),则缩小所有尺寸。必须在范围[-rank(input_tensor), rank(input_tensor))内。在这里0代表列,1代表行
keep_dims:表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度。如果为true,则保留长度为1的缩小尺寸。
name:操作的名称(可选)。
reduction_indices:axis的废弃的名称。
返回:
该函数返回减少的张量。
numpy兼容性
相当于np.sum

类似函数:
这样的函数在tensorflow中还有很多:

tf.reduce_prod #沿维度相乘

tf.reduce_min #沿维度找最小

tf.reduce_max #沿维度找最大

tf.reduce_mean #沿维度求平均

tf.reduce_all#沿维度与操作

tf.reduce_any#沿维度或操作

具体可以参考点击打开链接

7.tf.nn.max_pool是CNN当中的最大值池化操作

tf.nn.max_pool(value, ksize, strides, padding, name=None)
参数是四个,和卷积很类似:
第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape

第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1

第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]

第四个参数padding:和卷积类似,可以取’VALID’ 或者’SAME’

返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

8.slim.arg_scope共享参数

使用slim.arg_scope的前提条件:

  1. 用@add_arg_scope修饰目标函数
  2. 用with arg_scope(…) 设置默认参数.

arg_scope(list_ops_or_scope,**kwargs)

  • list_ops_or_scope: 操作列表或作用域列表
  • kwargs: 参数,以keyword=value方式显示

实例:

net = slim.conv2d(inputs, 64, [11, 11], 4, padding='SAME',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv1')
net = slim.conv2d(net, 128, [11, 11], padding='VALID',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv2')
net = slim.conv2d(net, 256, [11, 11], padding='SAME',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv3')

由于上例中:

weights_initializer=tf.truncated_normal_initializer(stddev=0.01)
weights_regularizer=slim.l2_regularizer(0.0005)

完全重复,我们可以将他提前出来,而

padding='SAME'

padding比较常用的值是’SAME’,如果有特例,再重新声明,主要的话,我们可以通过slim.arg_scope改造如下

with slim.arg_scope([slim.conv2d], padding='SAME',
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.01)
                      weights_regularizer=slim.l2_regularizer(0.0005)):
    net = slim.conv2d(inputs, 64, [11, 11], scope='conv1')
    net = slim.conv2d(net, 128, [11, 11], padding='VALID', scope='conv2')
    net = slim.conv2d(net, 256, [11, 11], scope='conv3')

参考:https://blog.youkuaiyun.com/zj360202/article/details/78590285

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值