zoukankan      html  css  js  c++  java
  • 01.TensorFlow基本操作

    运行代码

    1.

    import tensorflow as tf
    tf.compat.v1.disable_eager_execution() #保证sess.run()能够正常运行
    import os
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    
    a=tf.constant(2)
    b=tf.constant(3)
    
    
    with tf.compat.v1.Session() as sess:
        print("a:%i" % sess.run(a),"b:%i" % sess.run(b))
        print("Addition with constants: %i" % sess.run(a+b))
        print("Multiplication with constant:%i" % sess.run(a*b))
    
    
    matrix1=tf.constant([[3.,3.]])
    matrix2=tf.constant([[2.],[2.]])
    
    product=tf.matmul(matrix1,matrix2)
    with tf.compat.v1.Session() as sess:
        result = sess.run(product)
        print(result)

    运行结果:

    a:2 b:3
    Addition with constants: 5
    Multiplication with constant:6

    2.

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    
    #使用变量Variable构造计算图a,b
    a=tf.placeholder(tf.int16)
    b=tf.placeholder(tf.int16)
    #使用tf中的add,multiply函数对a,b进行求和与求积操作
    add=tf.add(a,b)
    mul=tf.multiply(a,b)
    #创建一个Session会话对象,调用run方法,运行计算图
    with tf.Session() as sess:
        print("Addition with variables: %i" % sess.run(add,feed_dict={a:2,b:3}))
        print("Multiplication with variables: %i" %sess.run(mul,feed_dict={a:2,b:3}))

    运行结果:

    Addition with variables: 5
    Multiplication with variables: 6

    遇到问题:

    由于python3X 与 python2x  有所变换 sess 无法使用

    python3 引用:

    import tensorflow as tf
    tf.compat.v1.disable_eager_execution() #保证sess.run()能够正常运行

    python2 直接引用:

    import tensorflow as tf

    保证

    placeholder  函数能够调用

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
  • 相关阅读:
    溢出省略号
    自定义字体
    jquery实现上一页下一页
    集成学习理解
    常用命令-python篇
    python 多进程和多线程
    10预处理命令上
    9函数
    8指针2
    7指针1
  • 原文地址:https://www.cnblogs.com/cxy0210/p/14477292.html
Copyright © 2011-2022 走看看