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

    运行代码:

    import tensorflow as tf
    import os
    
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    with tf.compat.v1.Session() as sess:
        a = tf.constant(2)
        b = tf.constant(3)
        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))

    在练习的过程中出现了错误  The Session graph is empty. Add operations to the graph before calling run().

    百度之后的结果是 

      1. tensorflow核心r2.0默认情况下已启用急切执行,因此无需编写tf.compat.v1.Session()并使用.run()函数
        如果我们想使用tf.compat.v1.Session()则我们需要
        在算法开始时执行tf.compat.v1.disable_eager_execution()。现在我们可以使用tf.compat.v1.Session()和.run()函数。

      2. Tensorflow核心r2.0默认情况下已启用急切执行。因此,无需更改它,
        我们只需更改我们的代码

    需要将赋值写到 with tf.compat.v1.Session() as sess: 里面;

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    import os
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    a=tf.placeholder(tf.int16)
    b=tf.placeholder(tf.int16)
    with tf.compat.v1.Session() as sess:
    
        add=tf.add(a,b)
        mul=tf.multiply(a,b)
        print("Addition with constants: %i" % sess.run(add,feed_dict={a:2,b:3}))
        print("Multiplication with constant: %i" % sess.run(mul,feed_dict={a:2,b:3}))

    报错:module 'tensorflow' has no attribute 'placeholder'

    将 import tensorflow.compat.v1 as tf 替换 import tensorflow as tf

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    import os
    os.environ["CUDA_VISIBLE_DEVICES"]="0"
    matrix1=tf.constant([[3.,3.]])
    matrix2=tf.constant([[2.],[2.]])
    a=tf.placeholder(tf.int16)
    b=tf.placeholder(tf.int16)
    with tf.compat.v1.Session() as sess:
        product = tf.matmul(matrix1, matrix2)
        result = sess.run(product)
        print(result)
        add=tf.add(a,b)
        mul=tf.multiply(a,b)
        print("Addition with constants: %i" % sess.run(add,feed_dict={a:2,b:3}))
        print("Multiplication with constant: %i" % sess.run(mul,feed_dict={a:2,b:3}))

  • 相关阅读:
    Docker决战到底(三) Rancher2.x的安装与使用
    golang实现给图片加水印
    golang实现图片水印效果
    百度ueditor 编辑器使用问题收集
    重要的文件和数据,别放在/tmp下
    在CentOS 7上切换默认的java版本
    [转载]Centos和RedHat的区别和联系
    在Excel的公式框内输入换行符
    Outlook打不开,报错信息为“The time limit for logging on was reached while waiting for system resources. Try again. MAPI 1.0 [000004C2]”
    VMXNET3与E1000E与E1000的比较
  • 原文地址:https://www.cnblogs.com/1234yyf/p/14270799.html
Copyright © 2011-2022 走看看