TensorFlow (a)

一, Hello World

1. Install only CPU version, TensorFlow1.14.0 version of the code

# Import tensorflow TF AS 
Import tensorflow.compat.v1 TF AS
 Import OS 

# os.environ [ "TF_CPP_MIN_LOG_LEVEL"] = '. 1' # default, all of the information displayed 
os.environ [ " TF_CPP_MIN_LOG_LEVEL " ] = ' 2 '   # display only the warning and Error 
# os.environ [ "TF_CPP_MIN_LOG_LEVEL"] = '. 3' # displays Error 


IF  the __name__ == ' __main__ ' : 
    Hello = tf.constant ( ' the Hello, TensorFlow! ' )   # define a constant 
    sess = tf.  The Session () # establish a session
    Print (sess.run (Hello))   # is run by the run results session inside 
    sess.close ()   # Close session

 

 

Second, other issues

1. Set Run Window display information

Import OS 

# os.environ [ "TF_CPP_MIN_LOG_LEVEL"] = '. 1' # default, all of the information displayed 
os.environ [ " TF_CPP_MIN_LOG_LEVEL " ] = ' 2 '   # display only the warning and Error 
# os.environ [ "TF_CPP_MIN_LOG_LEVEL"] = ' 3 'Show only Error #


2. version, GPU whether to use
version = tf.__version__
gpu_ok = tf.test.is_gpu_available()
print("tf version:",version,"\nuse GPU",gpu_ok)


3. If both of the two versions of installation, operation gpu default version, such as version cpu want to run, the code may be set as follows:
with tf.Session() as ses:
    with tf.device("/cpu:0"): # "/gpu:0"是GPU
        matrix1=tf.constant([[3.,3.]])
        matrix2=tf.constant([[2.],[2.]])
        product=tf.matmul(matrix1,matrix2)

 

Guess you like

Origin www.cnblogs.com/sybil-hxl/p/12375588.html