tensorflow beginners Quick Start (1)

Import:

TF_CPP_MIN_LOG_LEVEL which is said to ignore the warning, but I do not have sense (some of the warning numpy)

import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'

 

 

Constant string operations:

Wherein, log_device_placement to answer Session parameters may be displayed at runtime is used which part of the resources (the CPU, GPU)

hello=tf.constant('Hello')
cfig=tf.ConfigProto(log_device_placement=True)
sess=tf.Session(config=cfig)
dss=sess.run(hello)
print(dss)
sess.close()

  

Constant matrix operations:

 

sess=tf.Session()
a=tf.constant([1,2,3,4,5,6],shape=[2,3],name='a')
b=tf.constant([1,2,3,4,5,6],shape=[3,2],name='b')
c=tf.matmul(a,b)
xss=sess.run(c)
print(xss)
sess.close()

  

Constant number crunching:

tf.add be herein used in the form of a + b, name automatically acquired

sess=tf.Session()
a=tf.constant(1,name='ta')
b=tf.constant(2,name='tb')
#c=a+b
c=tf.add(a,b,name='tc')
sess=tf.Session()
xss=sess.run(c)
print(xss)

  

Save Tensorboard map:

FIG saved in the process, provided the corresponding path, and then save sess.graph, all running through FIG.

xsum=tf.summary.FileWriter(".",sess.graph)

After saving, open tensorboard: Use type cmd command: open service, and then browse graph items

tensorboard --logdir="."

tensorboard other part can also be seen as a histogram, charts, maps and the like.

 

Get the default tensorboard showing variables:

gg=tf.get_default_graph()
op1=gg.get_operations()
print(op1)
print(op1[1].node_def)

 

Because the original information before :( run through a string constant, so there would be a record of the Const) conducted before the matrix operation, a first matrix, through which information op [1] .node_def shows up

 

 

 

 

 

 Variable calculation:

Variable calculations require the use of tf.global_variables_initializer () to initialize variables, or it may report an error.

x=tf.constant(1.0,name='input')
w=tf.Variable(0.8,name='weight')
y=tf.multiply(w,x,name='output')
sess=tf.Session()
sess.run(tf.global_variables_initializer())
ans = sess.run (y)
xsum=tf.summary.FileWriter('.',sess.graph)
print (years)
sess.close()

Map:

 

 

 

Placeholders operation demonstrates:

After the variables are defined, input data dictionary format, the results and print them out.

x=tf.placeholder(tf.float32,name='x')
y=tf.placeholder(tf.float32,name='y')
z=tf.add(x,y,name='z')
ss=tf.Session()
xsum=tf.summary.FileWriter('.',ss.graph)
xss=ss.run(z,feed_dict={x:1,y:2})
print(xss)

Map:

 

 

 

eval can explain the string expression

D is the output 12

dss='10+2'
d=eval(dss)
d

 

 

 

Guess you like

Origin www.cnblogs.com/bai2018/p/11873958.html