当前位置 : 主页 > 编程语言 > python >

Tensorflow的会话以及高级API

来源:互联网 收集:自由互联 发布时间:2022-06-30
会话 一个运行Tensorflow operation的类,会话开启的两种方式: tf.Session():用于完整的程序中 tf.InteractiveSession():用于交互上下文中的Tensorflow,例如:shell init(target='',graph=None,config=None):初始化

会话

  • 一个运行Tensorflow operation的类,会话开启的两种方式:
  • tf.Session():用于完整的程序中
  • tf.InteractiveSession():用于交互上下文中的Tensorflow,例如:shell
    • init(target='',graph=None,config=None):初始化会话
    • graph:默认情况下,session绑定默认图
    • target:访问远程设备,指定grpc://网址
    • config:允许指定一个tf.ConfigProto控制会话行为,如:ConfigProto协议用于打印设备
    #所有的op、sess、tensor都是在同一张图中,即内存地址相同
    def graph_demo():
    v_data1 = tf.constant([1, 2, 3])
    v_data2 = tf.constant([3, 2, 1])
    v_data3 = tf.add(v_data1, v_data2)

    default_graph = tf.get_default_graph()
    print(default_graph)
    print(v_data2.graph)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess:
    print(sess.graph)
    • run:
    • fetche:单一的operation,或者列表、元组(不属于tensorflow类型的不行)
    • feed_dict:参数允许调用覆盖图中的值,进行赋值
    • 与tf.placeholder搭配使用,检查值的形状与占位符号是否兼容
    def place_constant():
    #placholder占位类型
    a = tf.placeholder(tf.int32)
    b = tf.placeholder(tf.int32)
    c = tf.add(a, b)

    with tf.Session() as sess:
    #使用run中的feed_dict进行传值
    print(sess.run(c,feed_dict={a:1, b:2}))


    • 查看张量的值,只能在会话中查看
  • sess.run()
  • .eval()

    张量

    • TensorFlow的张量就是一个n维数组
    • type:数据类型
    • shape:形状
    • ​​张量类型​​
    • 张量的阶
    • 创建张量的时候,如果不指定类型
      默认tf.float32
      整型:tf.int32
      浮点型:tf.float32

    张量的变换

  • 类型变换
  • def Matrix_cast():
    matrix1 = tf.random_normal(shape=[9, 9],mean=1.8,stddev=5,seed=12)
    #查看类型
    print(matrix1.dtype)
    #tf.to....转换指定to..类型
    matrix2 = tf.to_int64(matrix1)
    print(matrix2)
    #转换成想要的类型,cast不会修改原本的tensor,返回修改内之后的tensor
    matrix3 = tf.cast(matrix2, dtype=tf.float64)
    print(matrix3)
    with tf.Session() as sess:
    print(sess.run(matrix2))
  • 形状修改
    静态形状(不能跨阶数)—-一开始创建的张量的形状,确定了维数就不能改变维数了1-D到1-D,2-D到2-D...
    • 修改静态形状,只有形状没有固定下来的情况下,才能改
    def place_mattrix():
    #占位,未确定shape
    a = tf.placeholder(dtype=tf.float32, shape=[None, None])
    b = tf.placeholder(dtype=tf.float32, shape=[None, 10])
    c = tf.placeholder(dtype=tf.float32, shape=[8, 8])
    #填充占位
    a.set_shape([9, 9])
    b.set_shape([9, 10])
    print(a)
    print(b)
    print(c)
    • 动态形状修改(可以跨阶数),只会返回修改之后的tensor,不会修改原始的tensor
    def d3_place_matrix():
    #占位,未确定shape
    a = tf.placeholder(dtype=tf.float32, shape=[None, None])
    b = tf.placeholder(dtype=tf.float32, shape=[None, 10])
    c = tf.placeholder(dtype=tf.float32, shape=[7,8]),
    #动态修改,可更改阶数,不能改变数量,但是不能修改元素的数量,列入c的[7,8]可以[8,7,1]不能[10,5,1]
    a_reshaped = tf.reshape(a, shape=([1, 2, 3]))
    c_reshaped = tf.reshape(c, shape=([8, 7, 1]))
    print(a)
    print(b)
    print(c)
    print(a_reshaped)
    print(c_reshaped)

    变量OP

    • 变量的特点:
  • 存储持久化
  • 可修改值
  • 可指定被训练
    • 创建变量
    def Variable_sc():
    #tf.variable_scope修改命名空间使结构更加清晰
    with tf.variable_scope("my_scopr1"):
    a = tf.Variable(initial_value=88)
    b = tf.Variable(initial_value=99)
    with tf.variable_scope("my_scope2"):
    c = tf.add(a, b)
    print(a)
    print(b)
    print(c)
    with tf.Session() as sess:
    #初始化全局变量
    sess.run(tf.global_variables_initializer())
    print(sess.run(c))

    基础API

  • tf.app
    相当于为Tensorflow脚本提供main函数入口
  • tf.image
    Tensorflow的图像处理,主要颜色变换,变形和解码
  • tf.gfile
    提供文件操作函数
  • tf.summary
    生成TensorBoard可用的统计日志,目前四种类型:
    audio、image、histogram、scalar
  • tf.python_io
    读取TFRecords文件
  • tf.train
    提供训练器,和tf.nn结合一起,实现网络的优化计算
  • tf.nn
    提供构建神经网络的底层函数,核心模块
    其中包括各种层的函数,比如:卷积层、池化层等
  • 高级API

  • tf.keras
    深度学习库
  • tf.layers
    以更高级的概念层定一个模型,类似keras
  • tf.contrib
    提供够计算图中的网络层、正则化、摘要操作、是构建计算图的高级操作
  • tf.estimator
    实现几种简单的分类器和回归器,这里的DNN的网络,只是全连接网络,没有提供卷积之类的
    上一篇:python神经网络基础
    下一篇:没有了
    网友评论