zoukankan      html  css  js  c++  java
  • 吴裕雄 python 神经网络——TensorFlow 图、张量及会话

    import tensorflow as tf
    
    g1 = tf.Graph()
    with g1.as_default():
        v = tf.get_variable("v", [1], initializer = tf.zeros_initializer()) # 设置初始值为0
    
    g2 = tf.Graph()
    with g2.as_default():
        v = tf.get_variable("v", [1], initializer = tf.ones_initializer())  # 设置初始值为1
        
    with tf.Session(graph = g1) as sess:
        tf.global_variables_initializer().run()
        with tf.variable_scope("", reuse=True):
            print(sess.run(tf.get_variable("v")))
    
    with tf.Session(graph = g2) as sess:
        tf.global_variables_initializer().run()
        with tf.variable_scope("", reuse=True):
            print(sess.run(tf.get_variable("v")))

    import tensorflow as tf
    
    a = tf.constant([1.0, 2.0], name="a")
    b = tf.constant([2.0, 3.0], name="b")
    result = a + b
    print result
    
    sess = tf.InteractiveSession ()
    print(result.eval())
    sess.close()

    # 创建一个会话。
    sess = tf.Session()
    
    # 使用会话得到之前计算的结果。
    print(sess.run(result))
    
    # 关闭会话使得本次运行中使用到的资源可以被释放。
    sess.close()

    with tf.Session() as sess:
        print(sess.run(result))

    sess = tf.Session()
    with sess.as_default():
         print(result.eval())

    sess = tf.Session()
    
    # 下面的两个命令有相同的功能。
    print(sess.run(result))
    print(result.eval(session=sess))

    sess = tf.InteractiveSession ()
    print(result.eval())
    sess.close()

    config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
    sess1 = tf.InteractiveSession(config=config)
    sess2 = tf.Session(config=config)
  • 相关阅读:
    Pascal's Triangle II
    Pascal's Triangle
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Populating Next Right Pointers in Each Node
    path sum II
    Path Sum
    [转载]小波时频图
    [转载]小波时频图
    [转载]Hilbert变换及谱分析
  • 原文地址:https://www.cnblogs.com/tszr/p/10872464.html
Copyright © 2011-2022 走看看