tensorflow学习之路(6):tf.strided_slice()和tf.cast()的认识

本文介绍了TensorFlow中的tf.strided_slice()和tf.cast()两个函数。tf.cast()用于将张量转换为新的类型,如在CIFAR10的代码中将uint8转换为int32。tf.strided_slice()则用于从输入张量中按指定步长和起始位置提取子序列,当步长为负数时,会反向取值。通过实例解析了这两个函数的具体用法。
摘要由CSDN通过智能技术生成

最近在看CIFAR10的代码,其中,在cifar10_input.py里面,出现了

  • code1:
    # The first bytes represent the label, which we convert from uint8->int32
    result.label = tf.cast(
        tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32
    )

于是,对tf.strided_slice()和tf.cast()有了认识。于是,整理如下:

  • tf.cast()的认识
def cast(x, dtype, name=None):

官方解释:Casts a tensor to a new type.
其中,

x:是一个Tensor或者SparseTensor
dtype:目标类型
name:这个op的名字(可选)

举例说明:

 # tensor `a` is [6.3, 7.4], dtype=tf.float
 tf.cast(a, tf.int32) ==> [6, 7]  # dtype=tf.int32

因此,在code1代码中,result.label的type由tf.cast()从uint8变成了int32.

  • tf.strided_slice()的认识
def 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):

官方解释:

To a first order, this operation extracts a slice of size `end - begin`
from a tensor `input`
starting at the location specified by `begin`. The slice continues by adding
`stride` to the `begin` index until all dimensions are not less than `end`.
Note that components of stride can be negative, which causes a reverse
slice.

简而言之,就是:
从输入tensor ‘input’中提取一个从‘begin’位置开始,长度为’end - begin’的片段。片段增加步长为’stride’,直到所有的维度不小于‘end’.但是,如果stride的中有负数,那么,会产生一个顺序相反的slice.

举例说明:

# 'input' is [[[1, 1, 1], [2, 2, 2]],
#            [[3, 3, 3], [4, 4, 4]],
#            [[5, 5, 5], [6, 6, 6]]]
tf.strided_slice(input, [1, 0, 0], [2, 1, 3], [1, 1, 1]) ==> [[[3, 3, 3]]]
tf.strided_slice(input, [1, 0, 0], [2, 2, 3], [1, 1, 1]) ==> [[[3, 3, 3],
                                                              [4, 4, 4]]]
tf.strided_slice(input, [1, -1, 0], [2, -3, 3], [1, -1, 1]) ==>[[[4, 4, 4],
                                                                [3, 3, 3]]]

在上面例子第三个tf.strided_slice()中,

begin = [1, -1, 0]
end = [2, -3, 3]
strides = [1, -1, 1]

其中,begin中的 -1 表示要从第二维最后一个元素开始,strides中的 -1 表示第二维中每次增长步长为-1,于是,取出的元素下标是-1, -2, -3,… ,且因为 strides中的第二维步长为负数,所以,第二维元素取出后是反方向,而end中的 -3 表示截至于第二维中倒数第二个元素(包括倒数第二个元素,下标为-2),所以,最终,输出结果为[[[4, 4, 4], [3, 3, 3]]]

File "/root/Desktop/EAST-master/multigpu_train.py", line 180, in <module> tf.app.run() File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/absl/app.py", line 312, in run _run_main(main, args) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/absl/app.py", line 258, in _run_main sys.exit(main(argv)) File "/root/Desktop/EAST-master/multigpu_train.py", line 110, in main total_loss, model_loss = tower_loss(iis, isms, igms, itms, reuse_variables) File "/root/Desktop/EAST-master/multigpu_train.py", line 30, in tower_loss f_score, f_geometry = model.model(images, is_training=True) File "/root/Desktop/EAST-master/model.py", line 77, in model spp_output = spp_layer(f[0]) File "/root/Desktop/EAST-master/model.py", line 44, in spp_layer strides=[1, strides[0], strides[1], 1], padding='VALID') File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_ops.py", line 3815, in max_pool name=name) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_nn_ops.py", line 5662, in max_pool ksize = [_execute.make_int(_i, "ksize") for _i in ksize] File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_nn_ops.py", line 5662, in <listcomp> ksize = [_execute.make_int(_i, "ksize") for _i in ksize] File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/eager/execute.py", line 169, in make_int (arg_name, repr(v))) TypeError: Expected int for argument 'ksize' not <tf.Tensor 'model_0/feature_fusion/SpatialPyramidPooling/strided_slice_2:0' shape=() dtype=int32>. Process finished with exit code 1
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值