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()
  • 相关阅读:
    LeetCode 169. Majority Element
    Object-c block块
    Object-c动态特性
    Object-c基础(2)
    Object-c基础
    LeetCode171:Excel Sheet Column Number
    LeetCode242:Valid Anagram
    LeetCood8:String to Integer
    理解qsort 中的 cmp
    google goble cache
  • 原文地址:https://www.cnblogs.com/cxy0210/p/14477292.html
Copyright © 2011-2022 走看看