Advertisement

TensorFlow学习过程记录(二)——基本使用(2)——交互式使用

阅读量:

2. 交互式使用

在前述示例中,我们借助Session对象来初始化计算图,并通过调用Session.run方法实现操作的执行。为提升在类似IPython这样的Python交互式环境中的使用便捷性,可选择采用InteractiveSession类替代传统的Session类,同时将Tensor.eval()与Operation.run()方法作为Session.run()的替代方案。这一调整能够有效减少对会话变量的依赖,从而简化代码结构。

复制代码
    # coding=utf-8
    
    # 进入一个交互式 TensorFlow 会话.
    import tensorflow as tf
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    sess = tf.InteractiveSession()
    
    x = tf.Variable([1.0, 2.0])
    a = tf.constant([3.0, 3.0])
    
    # 使用初始化器 initializer op 的 run() 方法初始化 'x'
    x.initializer.run()
    
    # 增加一个加法 add op, 从 'x' 加上 'a'. 运行加

全部评论 (0)

还没有任何评论哟~