zoukankan      html  css  js  c++  java
  • P48 会话的run()方法

    import tensorflow as tf
    
    #创建一张图包含了一组op和tensor,上下文环境
    
    #g=tf.Graph()
    #print(g) #当创建了新的图后,又分配了新的内存
    #with g.as_default():
    #    c=tf.constant(12.0)
     #   print(c.graph) #当创建了新的图后,又分配了新的内存
    
    
    
    #实现一个加法运算
    a=tf.constant(5.0)
    b=tf.constant(6.0)
    sum1=tf.add(a,b)
    #图的定义,默认的这张图,相当于是给程序分配一段内存
    graph=tf.get_default_graph()
    #图的打印
    #print(graph)
    
    with tf.Session() as sess:
        print(sess.run([a,b,sum1]))
        #print(a.graph)
        #print(sum1.graph)
        #print(sess.graph)

    运行结果:

    [5.0, 6.0, 11.0]

    注解:

    • 可以运行多个变量(张量)
    import tensorflow as tf
    
    #实现一个加法运算
    a=tf.constant(5.0)
    b=tf.constant(6.0)
    sum1=tf.add(a,b)
    #图的定义,默认的这张图,相当于是给程序分配一段内存
    graph=tf.get_default_graph()
    
    var1=7.8
    var2=9.3
    sum2=var1+var2
    
    with tf.Session() as sess:
        print(sess.run(sum2))

    运行结果:

    TypeError: Fetch argument 17.1 has invalid type <class 'float'>, must be a string or Tensor. (Can not convert a float into a Tensor or Operation.)

    注解:

    • python变量是不能运行的。
    import tensorflow as tf
    
    #实现一个加法运算
    a=tf.constant(5.0)
    b=tf.constant(6.0)
    sum1=tf.add(a,b)
    #图的定义,默认的这张图,相当于是给程序分配一段内存
    graph=tf.get_default_graph()
    
    var1=7.8
    var2=9.3
    sum2=a+var2
    
    with tf.Session() as sess:
        print(sess.run(sum2))

    运行结果:

    14.3

    注解:

    • 一个Python变量和一个tensor张量相加是可以的,此时"+"号被tensorflow重载了。

    import tensorflow as tf
    
    #实现一个加法运算
    a=tf.constant(5.0)
    b=tf.constant(6.0)
    sum1=tf.add(a,b)
    #图的定义,默认的这张图,相当于是给程序分配一段内存
    graph=tf.get_default_graph()
    
    #训练一个网络模型的时候,
    #实时的提供数据进行训练
    
    #placeholder是一个占位符,在程序运行时提供数据
    plt=tf.placeholder(tf.float32,[2,3])  #也是一个op(操作、运算),只是占位,没有具体的数据,在sess.run()运行的时候提供数据
    #[2,3]代表将填充一个2行3列的数据
    
    
    with tf.Session() as sess:
        print(sess.run(plt,feed_dict={plt:[[1,2,3],[4,5,6]]}))

    运行结果:

    注解:
    feed_dict={}并不是每次都用得到,一般实时训练的时候能用到。

    import tensorflow as tf

    #实现一个加法运算
    a=tf.constant(5.0)
    b=tf.constant(6.0)
    sum1=tf.add(a,b)
    #图的定义,默认的这张图,相当于是给程序分配一段内存
    graph=tf.get_default_graph()

    #训练一个网络模型的时候,
    #实时的提供数据进行训练

    #placeholder是一个占位符,在程序运行时提供数据
    plt=tf.placeholder(tf.float32,[None,3]) #也是一个op(操作、运算),只是占位,没有具体的数据,在sess.run()运行的时候提供数据
    #[2,3]代表将填充一个2行3列的数据
    #[None,3]代表训练的时候,样本数可能不固定
    print(plt)
    with tf.Session() as sess:
    print(sess.run(plt,feed_dict={plt:[[1,2,3],[4,5,6],[7,8,9],[9,11,88]]}))

    运行结果:

    Tensor("Placeholder:0", shape=(?, 3), dtype=float32)

    [[ 1. 2. 3.]
    [ 4. 5. 6.]
    [ 7. 8. 9.]
    [ 9. 11. 88.]]

    注解:

    • 当样本数量写None,(此处特征数量写的是3,手写数字识别特征数是784)的时候,feed_dict={}中的样本数量就可以是任意的了,可以随意输入多少个样本数都是行的。
  • 相关阅读:
    清除浮动的三种方式
    js控制滚动条默认在底部
    npm 基础命令
    package.json中^,~的区别
    mac 使用iTerm2快捷登录远程服务器
    iterm 分屏切换快捷键与配色设置
    git tag — 标签相关操作
    gulp iconfont
    webpack 3 升级 webpack4 个人笔记
    import * as x from 'xx' 和 import x from 'xx'
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/14163904.html
Copyright © 2011-2022 走看看