Tensorflow的structure的例子(一)

来源:互联网 发布:vue.js 2.0 教程下载 编辑:程序博客网 时间:2024/06/07 03:54

本例子包括两部分内容:logging的使用和tf的整体流程训练。

经常用print,感觉很方便,但是没有一下输出自己想要输出的内容,所以想到用logging代替print。

首先是logging的配置文件logger.conf,前面有了解过日志系统。

# logging.basicConfig函数各参数:# filename: 指定日志文件名# filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'# format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:#  %(levelno)s: 打印日志级别的数值#  %(levelname)s: 打印日志级别名称#  %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]#  %(filename)s: 打印当前执行程序名#  %(funcName)s: 打印日志的当前函数#  %(lineno)d: 打印日志的当前行号#  %(asctime)s: 打印日志的时间#  %(thread)d: 打印线程ID#  %(threadName)s: 打印线程名称#  %(process)d: 打印进程ID#  %(message)s: 打印日志信息# datefmt: 指定时间格式,同time.strftime()# level: 设置日志级别,默认为logging.WARNING# stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略###############################################[loggers]keys=root,example01,example02[logger_root]level=DEBUGhandlers=hand01,hand02[logger_example01]handlers=hand01,hand02qualname=example01propagate=0[logger_example02]handlers=hand01,hand03qualname=example02propagate=0###############################################[handlers]keys=hand01,hand02,hand03[handler_hand01]class=StreamHandlerlevel=INFOformatter=form02args=(sys.stderr,)[handler_hand02]class=FileHandlerlevel=INFOformatter=form01args=('Proj.log', 'a')[handler_hand03]class=handlers.RotatingFileHandlerlevel=INFOformatter=form02args=('Proj.log', 'a', 10*1024*1024, 5)###############################################[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)sdatefmt=%a, %d %b %Y %H:%M:%S[formatter_form02]format=%(name)-12s: %(levelname)-8s %(message)sdatefmt=
然后是test_1.py的代码:

import numpy as npimport tensorflow as tfimport logging.configlogging.config.fileConfig("logger.conf")logger = logging.getLogger("example01")#create datax_data=np.random.rand(100)#.astype(np.float32)logging.info('This is the type of x_data:%s',x_data.dtype)x_data=x_data.astype(np.float32)# logging.info('This is the type of x_data:%s',x_data.dtype)y_data=x_data*0.1+0.3#create tensorflow structureWeights =tf.Variable(tf.random_uniform([1],-1.0,1.0))biases =tf.Variable(tf.zeros([1]))y=Weights*x_data +biasesloss=tf.reduce_mean(tf.square(y-y_data))optimizer=tf.train.GradientDescentOptimizer(0.5)train=optimizer.minimize(loss)#tensorflow structure endinit=tf.global_variables_initializer()sess=tf.Session()sess.run(init)for step in range(201):    sess.run(train)    if step%20==0:        print(step,sess.run(Weights),sess.run(biases))
如果中途想调用老版本的tf,中间的global_variables_initializer()函数替换成initialize_all_tables()。新版本的这个函数已经废弃了。

如果还用这个函数的话会报:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value local/hidden_1/w         [[Node: local/hidden_1/w/read = Identity[T=DT_FLOAT, _class=["loc:@local/hidden_1/w"], _device="/job:worker/replica:0/task:1/cpu:0"](local/hidden_1/w)]]
的错误。

屏幕输出:

0 [ 0.57068026] [ 0.03290105]20 [ 0.22996345] [ 0.22493491]40 [ 0.13604316] [ 0.27918199]60 [ 0.10999594] [ 0.2942265]80 [ 0.10277221] [ 0.29839882]100 [ 0.10076882] [ 0.29955596]120 [ 0.10021321] [ 0.29987687]140 [ 0.10005914] [ 0.29996586]160 [ 0.1000164] [ 0.29999053]180 [ 0.10000455] [ 0.29999739]200 [ 0.10000127] [ 0.29999927]