# tensorflow 会话
import tensorflow as tf
"""
1. 定义:
一个运行tesnorflow op 操作的类 (tf.Session())
2. 开启会话:
a. 完整程序当
sess = tf.Session()
sess.close()
with tf.Session() as sess:
pass
b. 交互式程序中
tf.InteractiveSession(): 打开会话
tensor.eval()
3. Session类
位置: tf.Session()
初始化:
def __init__(self, target='', graph=None, config=None)
pass
参数:
target: 网络主机的ip号. ssh 登录到网络主机 scp
graph: 指定运行的图 *****
config: 此参数允许您指定一个 tf.ConfigProto
以便控制会话的行为。例如,ConfigProto协议用于打印设备使用信息
run 方法:
def run(self, fetches, feed_dict=None):
fetches: 单一 op 操作:
feed_dict: 配合placeholder 使用:
dict :
"""
def run_demo():
"""
:return:
"""
# 定义一个单一的张量
a = tf.constant(10)
ph = tf.placeholder(dtype=tf.float32, shape=[None, 4])
# 开启一个会话
with tf.Session() as sess:
print(sess.run(a))
print("ph = ",sess.run(ph, feed_dict={ph: [[1,2,3,4],[5,6,7,8]]}))
run_demo()
def sess():
a = tf.constant(10)
sess = tf.Session()
print(sess.run(a))
sess.close()
tensorflow 会话
最新推荐文章于 2024-10-09 00:00:00 发布