TensorFlow——交互式使用会话:InteractiveSession类

目的是在交互式环境下(如jupyter),手动设定当前会话为默认会话,从而省去每次都要显示地说明sess的繁琐,如:Tensor.ecal(session=sess)或sess.Operation.run()

只需要写成Tensor.ecal()或Operation.run()

>>> import tensorflow as tf
>>> sess = tf.InteractiveSession()
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
>>> x = tf.Variable([1.0, 2.0])
>>> a = tf.constant([3.0, 3.0])
>>> x.initializer.run()    # initializer初始化了变量x 初始化了的x可以直接用run()而不需要用sess.run()来运行
>>> sub = tf.sub(x, a)    # tf里的矩阵减法函数是sub(),其实写成x-a没错
>>> print(sub.eval())    # 在这里,sub.eval()相当于sess.run(sub),因为操作sub与张量sub同名了
[-2. -1.]
>>> sess.close()

 

 

    原文作者:tensorflow
    原文地址: https://www.cnblogs.com/DianeSoHungry/p/7144218.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞