• Tensorflow--图和会话


    代码:

    # -*- coding: UTF-8 -*-
    
    # 引入 TensorFlow
    import tensorflow as tf
    
    # 创建两个常量 Tensor,两个[]才是矩阵
    const1 = tf.constant([[2, 2]])
    const2 = tf.constant([[4],
                          [4]])
    
    # 张量相乘(multiply 是 相乘 的意思)
    multiply = tf.matmul(const1, const2)
    
    # 尝试用 print 输出 multiply 的值
    print("sess.run() 之前,尝试输出 multiply 的值: {}".format(multiply))
    
    # 创建了 Session(会话)对象
    sess = tf.Session()
    
    # 用 Session 的 run 方法来实际运行 multiply 这个
    # 矩阵乘法操作,并把操作执行的结果赋值给 result
    result = sess.run(multiply)
    # 用 print 打印矩阵乘法的结果
    print("sess.run() 之后,输出 multiply 的值: {}".format(result))
    
    if const1.graph is tf.get_default_graph():
        print("const1 所在的图(Graph)是当前上下文默认的图")
    
    # 关闭已用完的 Session(会话)
    sess.close()
    
    # 第二种方法来创建和关闭 Session
    # 用了 Python 的上下文管理器(with ... as ... :)
    with tf.Session() as sess:
        result2 = sess.run(multiply)
        print("multiply 的结果是 {} ".format(result2))

    运行结果:

    sess.run() 之前,尝试输出 multiply 的值: Tensor("MatMul:0", shape=(1, 1), dtype=int32)
    sess.run() 之后,输出 multiply 的值: [[16]]
    const1 所在的图(Graph)是当前上下文默认的图
    multiply 的结果是 [[16]] 
  • 相关阅读:
    kotlin 通过 下标比对
    textarea元素调整
    jquery给两个标签绑定一个事件
    开发过程中遇到的错误
    response.setHeader各种用法详解
    如何在eclipse里删除一个类 然后SVN服务器也同时删了这个类
    @pathvariable 与@requestparam 写rest接口时遇到的
    $.getJSON
    easyUI学习
    jQuery validator addMethod 动态提示信息
  • 原文地址:https://www.cnblogs.com/SCCQ/p/12328149.html
Copyright © 2020-2023  润新知