TensorFlow 中 tf.enable_eager_execution 使用实例

一、环境

TensorFlow API r1.14(rc)

CUDA 9.0 V9.0.176

Python 3.6.3

二、官方说明

在该程序的生命周期内启用“立即执行(eager execution)”

https://tensorflow.google.cn/versions/r1.14/api_docs/python/tf/enable_eager_execution

tf.enable_eager_execution(
config=None,
device_policy=None,
execution_mode=None
)

参数:

config:可选参数。通过 tf.compat.v1.ConfigProto 来配置操作执行的环境。需要注意的是 tf.compat.v1.ConfigProto 也用来配置图执行(通过 tf.compat.v1.Session),同时,当 eager execution 执行时,tf.compat.v1.ConfigProto 中的很多选项就不被实现(或无关)

device_policy:可选参数。控制在特定设备(例如,GPU 0)上需要输入的操作如何处理不同设备(例如,GPU 1或CPU)上的输入的策略。 设置为“无”时,将自动选择适当的值。 挑选的值可能会在TensorFlow版本之间发生变化。 有效值有:

  • tf.contrib.eager.DEVICE_PLACEMENT_EXPLICIT
  • tf.contrib.eager.DEVICE_PLACEMENT_WARN
  • tf.contrib.eager.DEVICE_PLACEMENT_SILENT
  • tf.contrib.eager.DEVICE_PLACEMENT_SILENT_FOR_INT32

execution_mode:可选参数。控制调度操作实际如何执行的策略。当设置为 None 时,将自动挑选合适的数值。挑选的值可能会在TensorFlow版本之间发生变化。 有效值有:

  • tf.contrib.eager.SYNC:同步执行每一项操作
  • tf.contrib.eager.ASYNC:异步执行每一项操作。这些操作可能返回 “non-ready” 的句柄

三、实例

>>> import tensorflow as tf
>>> import numpy as np
>>> tf.enable_eager_execution()
>>> data = np.random.random((5,30))
>>> data_tensor = tf.constant(value=data)
>>> split0,split1,split2 = tf.split(data_tensor,[4,15,11],-1)
>>> split0, split1, split2 = tf.split(data_tensor,[4,15,11],-1)
>>> tf.shape(split0)
<tf.Tensor: id=6, shape=(2,), dtype=int32, numpy=array([5, 4], dtype=int32)>
>>> tf.shape(split1)
<tf.Tensor: id=8, shape=(2,), dtype=int32, numpy=array([ 5, 15], dtype=int32)>
>>> tf.shape(split2)
<tf.Tensor: id=10, shape=(2,), dtype=int32, numpy=array([ 5, 11], dtype=int32)>

>>> split0.numpy()
array([[0.57453663, 0.09221047, 0.75910165, 0.42249415],
       [0.16336272, 0.89446457, 0.39257956, 0.72432271],
       [0.65180994, 0.36158292, 0.76486074, 0.90457114],
       [0.42410351, 0.33851704, 0.7050377 , 0.37142487],
       [0.22980119, 0.42904001, 0.29638316, 0.17583503]])
>>> split1.numpy()
array([[0.65376257, 0.28828267, 0.68491934, 0.0453603 , 0.52976963,
        0.51527368, 0.13887819, 0.66672668, 0.32432196, 0.53790642,
        0.73945528, 0.65519244, 0.00321005, 0.76134054, 0.43237806],
       [0.38422725, 0.85679974, 0.8189933 , 0.79522319, 0.28097536,
        0.01417454, 0.33259705, 0.58593418, 0.41862654, 0.92597912,
        0.62811071, 0.07483196, 0.57353834, 0.94820909, 0.05068218],
       [0.69485806, 0.5419911 , 0.53619689, 0.41984836, 0.17919892,
        0.90462194, 0.85142706, 0.88211552, 0.86519832, 0.46317626,
        0.32401062, 0.10729327, 0.94318461, 0.78615075, 0.31926406],
       [0.35100308, 0.73412197, 0.27498129, 0.56456188, 0.66821586,
        0.86167801, 0.99093966, 0.51488332, 0.54963875, 0.33069172,
        0.43082452, 0.28137253, 0.8633286 , 0.66494205, 0.85997601],
       [0.96145664, 0.05172849, 0.08726628, 0.16821706, 0.3590603 ,
        0.70607823, 0.83647191, 0.52196516, 0.45792938, 0.97573745,
        0.8225903 , 0.09839245, 0.41375104, 0.4079168 , 0.89302919]])
>>> split2.numpy()
array([[0.11837789, 0.52289792, 0.55807716, 0.96394212, 0.84602479,
        0.7968168 , 0.13631762, 0.99491611, 0.0810203 , 0.03436556,
        0.14814265],
       [0.5901035 , 0.99906073, 0.74242789, 0.63917107, 0.57637234,
        0.2895648 , 0.06453467, 0.75643109, 0.08641431, 0.8303831 ,
        0.50956032],
       [0.62365326, 0.84957791, 0.79885836, 0.90246727, 0.30868528,
        0.19903051, 0.78012872, 0.57343972, 0.28900716, 0.65201702,
        0.33714608],
       [0.17511412, 0.27725245, 0.6815811 , 0.89634696, 0.30047085,
        0.24422117, 0.13571881, 0.95206347, 0.36601743, 0.74425454,
        0.46046426],
       [0.38272897, 0.04541412, 0.68629762, 0.93640718, 0.42502042,
        0.70174166, 0.75355039, 0.15139218, 0.36311097, 0.55398162,
        0.09967336]])

需要注意:
(1)tf.enable_eager_execution() 必须放在 tensorflow 代码的前面,否则会报错!
(2)这里的 tf.split 是对张量的切分,需要注意的是其第一个数列表元素之和需要等于第一参数在第三参数所指定的维度上的数值

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值