Tensorflow学习系列(二): tensorflow基础

    xiaoxiao2021-03-25  25

    如需转载,请注明出处,欢迎加入深度学习群 255568483

    Graph basics由nodes(结点)Edges(边缘)组成。

    用一个简单的例子来讲解:

     

    数据从左到右,请参见箭头的方向

    1.在开始的时候,有两个值53,他们可能来自其它的Graph 或者从文件中或者是用户直接输入的。

    2.这两个初始化的值被传到input结点,在graph中被标记为aba被传给了结点cdb也执行相同的操作。

    3.结点c执行相乘的操作,他从结点ab取值,输出15到结点e。而结点d执行相加操作,他从结点ab取值,输出8到结点e

    4.结点e是最终的输出操作,他执行的是相加的操作,他收到的值为158,最终输出的结果为23.

    整个流程执行完毕,执行过程用数据表达如下:

    a = input1;b=input2;

    c=a*b;d=a+b;

    e=c+d;

     

    下面通过一个例子来说明:

    完整代码如下:

    import tensorflow as tf graph = tf.Graph() with graph.as_default(): with tf.name_scope("variables"): # 定义变量,共执行了多少次 global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") # 输出的累加值求和 total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output") with tf.name_scope("transformation"): # 定义输入层 with tf.name_scope("input"): # 定义输入数据的格式 a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a") # 定义中间层 with tf.name_scope("intermediate_layer"): b = tf.reduce_prod(a, name="product_b") c = tf.reduce_sum(a, name="sum_c") # 定义输出层 with tf.name_scope("output"): output = tf.add(b, c, name="output") with tf.name_scope("update"): # 更新输出的累加值 update_total = total_output.assign_add(output) # 更新操作次数 increment_step = global_step.assign_add(1) # 记录操作日志 with tf.name_scope("summaries"): avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average") tf.summary.scalar(name="output_summary", tensor=output) tf.summary.scalar(name="total_summary", tensor=update_total) tf.summary.scalar(name="average_summary", tensor=avg) with tf.name_scope("global_ops"): # 初始化参数 init = tf.initialize_all_variables() # 合并所有日志 merged_summaries = tf.summary.merge_all() sess = tf.Session(graph=graph) writer = tf.summary.FileWriter('log/tensorflow-basic', graph) sess.run(init) # 帮助函数 def run_graph(input_tensor): feed_dict = {a: input_tensor} _, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict) writer.add_summary(summary, global_step=step) run_graph([2, 8]) run_graph([3, 1, 3, 3]) run_graph([8]) run_graph([1, 2, 3]) run_graph([11, 4]) run_graph([4, 1]) run_graph([7, 3, 1]) run_graph([6, 3]) run_graph([0, 2]) run_graph([4, 5, 6]) # 关闭相关操作 writer.flush() writer.close() sess.close() 查看log文件

    $ tensorboard --logdir="tensorflow-basic" I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally I tensorflow/stream_executor/dso_loader.cc:126] Couldn't open CUDA library libcufft.so.8.0. LD_LIBRARY_PATH: I tensorflow/stream_executor/cuda/cuda_fft.cc:344] Unable to load cuFFT DSO. I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally Starting TensorBoard b'41' on port 6006 (You can navigate to http://127.0.1.1:6006)

    转载请注明原文地址: https://ju.6miu.com/read-50211.html

    最新回复(0)