一、tf.range()生成数字序列
range()函数用于创建数字序列变量,有以下两种形式:
range(limit, delta=1, dtype=None, name='range') range(start, limit, delta=1, dtype=None, name='range')
该数字序列开始于 start 并且将以 delta 为增量扩展到不包括 limit 时的最大值结束,类似python的range函数。
二、tf.expand_dims()
TensorFlow中,想要维度增加一维,可以使用tf.expand_dims(input, dim, name=None)函数。当然,我们常用tf.reshape(input, shape=[])也可以达到相同效果,但是有些时候在构建图的过程中,placeholder没有被feed具体的值,这时就会包下面的错误:TypeError: Expected binary or unicode string, got 1
tf.expand_dims(x,[]) 列表的内容表示在第几个维度上增加
例子如下:
# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
三、tf.tile()
tf.tile()在指定维度上复制tensor一定次数 [2,1]表示在第一个维度上(行)重复2次,列上重复1次
import tensorflow as tf with tf.Session() as sess: a = tf.constant([[15, 16], [17, 18]]) b = tf.tile(a, [1, 3]) c = tf.tile(a, [3, 2]) print('------------------------------------') print(sess.run(a)) print('------------------------------------') print(sess.run(b)) print('------------------------------------') print(sess.run(c))
输出值如下:
[[15 16] [17 18]] ------------------------------------ [[15 16 15 16 15 16] [17 18 17 18 17 18]] ------------------------------------ [[15 16 15 16] [17 18 17 18] [15 16 15 16] [17 18 17 18] [15 16 15 16] [17 18 17 18]]
四、tf.strided_slice函数
tf.strided_slice( input_, begin, end, strides=None, begin_mask=0, end_mask=0, ellipsis_mask=0, new_axis_mask=0, shrink_axis_mask=0, var=None, name=None )
粗略地说,这个运算从给定的 input_ 张量中提取一个尺寸 (end-begin)/stride 的片段.从 begin 片段指定的位置开始,继续添加 stride 索引,直到所有维度都不小于 end.请注意,步幅可能是负值,这会导致反向切片.
一个案例:
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]]) tf.strided_slice(t, [1, 0, 0], [2, 1, 3], [1, 1, 1]) # [[[3, 3, 3]]] tf.strided_slice(t, [1, 0, 0], [2, 2, 3], [1, 1, 1]) # [[[3, 3, 3], # [4, 4, 4]]] tf.strided_slice(t, [1, -1, 0], [2, -3, 3], [1, -1, 1]) # [[[4, 4, 4], # [3, 3, 3]]]
五、训练过程中batch_size的设置问题
一般来说,会设置成一个固定的值,直接使用。但是可以发现当预测时或者在解码器端(以机器翻译为例),如下:
# 去掉句末的<EOS> ending = tf.strided_slice(self.decoder_targets, [0, 0], [self.batch_size, -1], [1, 1]) # 添加上句首的<GO> self.decoder_inputs = tf.concat([tf.fill([self.batch_size, 1], self.en_word2id_dict['<GO>']), ending], 1) #将batch_size改成ending[0],防止不足一个batch的情况出现
会用到batch_size,而当最后的样本不足一个batch_size时,表现为ending的第一维度不是batch_size,那么会出现维度不匹配的错误。
所有可以将batch_size设置为超参数:
batch_size=tf.placeholder(tf.int32,[],name='batch_size') #[None]也会报错
作为一个张量来对待