TensorFlow 初级

简介:

TensorFlow 是基于数据流图 (Data Flow Graph), 支持自动微分 (简称AD) 的数值计算库。本文仅仅考虑低级 API.

TensorFlow 的计算图模型一般分为两个步骤:创建计算图,在 Session 中运行。(暂不考虑 Eager)

为了更好的管理模型,最好在特定的 Graph 中创建模型,且对于实现不同功能的模块最好按照 name_scope 对其进行划分。下面是一个 Demo:

# Explicitly create a Graph object
graph = tf.Graph()

with graph.as_default():
    
    with tf.name_scope("variables"):
        # Variable to keep track of how many times the graph has been run
        global_step = tf.Variable(0, dtype=tf.int32, name="global_step")
        
        # Variable that keeps track of the sum of all output values over time:
        total_output = tf.Variable(0.0, dtype=tf.float32, name="total_output")
    
    # Primary transformation Operations
    with tf.name_scope("transformation"):
        
        # Separate input layer
        with tf.name_scope("input"):
            # Create input placeholder- takes in a Vector 
            a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")
    
        # Separate middle layer
        with tf.name_scope("intermediate_layer"):
            b = tf.reduce_prod(a, name="product_b")
            c = tf.reduce_sum(a, name="sum_c")
        
        # Separate output layer
        with tf.name_scope("output"):
            output = tf.add(b, c, name="output")
        
    with tf.name_scope("update"):
        # Increments the total_output Variable by the latest output
        update_total = total_output.assign_add(output)
        
        # Increments the above `global_step` Variable, should be run whenever the graph is run
        increment_step = global_step.assign_add(1)
    
    # Summary Operations
    with tf.name_scope("summaries"):
        avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
        
        # Creates summaries for output node
        tf.summary.scalar('Output', output)
        tf.summary.scalar('Sum of outputs over time', update_total)
        tf.summary.scalar('Average of outputs over time', avg)
    
    # Global Variables and Operations
    with tf.name_scope("global_ops"):
        # Initialization Op
        init = tf.initialize_all_variables()    
        # Merge all summaries into one Operation
        merged_summaries = tf.summary.merge_all()

def run_graph(input_tensor):
    """
    Helper function; runs the graph with given input tensor and saves summaries
    """
    feed_dict = {a: input_tensor}
    out, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)
    writer.add_summary(summary, global_step=step)

# Start a Session, using the explicitly created Graph
sess = tf.Session(graph=graph)

# Open a SummaryWriter to save summaries
writer = tf.summary.FileWriter('../graph/improved_graph', graph)

# Initialize Variables
sess.run(init)
# Run the graph with various inputs
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])
# Write the summaries to disk
writer.flush()

# Close the SummaryWriter
writer.close()

# Close the session
sess.close()
目录
相关文章
|
5月前
|
算法 TensorFlow 算法框架/工具
第3章 TensorFlow进阶
第3章 TensorFlow进阶
52 0
|
5月前
|
机器学习/深度学习 存储 TensorFlow
TensorFlow 基础实战
TensorFlow 基础实战
|
TensorFlow 算法框架/工具
tensorflow 入门学习
tensorflow 入门学习
36 0
|
10月前
|
机器学习/深度学习 TensorFlow API
tensorflow从头再学1
tensorflow从头再学1
44 1
|
机器学习/深度学习 TensorFlow API
TensorFlow2.0学习使用笔记
TensorFlow2.0学习使用笔记
|
机器学习/深度学习 人工智能 算法
人工智能实践Tensorflow笔记:Tensorflow框架-3
人工智能实践Tensorflow笔记:Tensorflow框架-3
152 0
人工智能实践Tensorflow笔记:Tensorflow框架-3
|
机器学习/深度学习 数据可视化 数据挖掘
斯坦福tensorflow教程(一) tensorflow概述
斯坦福tensorflow教程(一) tensorflow概述
206 0
斯坦福tensorflow教程(一) tensorflow概述
|
算法框架/工具 TensorFlow 算法
带你读《TensorFlow机器学习实战指南(原书第2版)》之二:TensorFlow进阶
本书由资深数据科学家撰写,从实战角度系统讲解TensorFlow基本概念及各种应用实践。真实的应用场景和数据,丰富的代码实例,详尽的操作步骤,带领读者由浅入深系统掌握TensorFlow机器学习算法及其实现。本书第1章和第2章介绍了关于TensorFlow使用的基础知识,后续章节则针对一些典型算法和典型应用场景进行了实现,并配有较详细的程序说明,可读性非常强。读者如果能对其中代码进行复现,则必定会对TensorFlow的使用了如指掌。
|
TensorFlow 算法框架/工具 C++
TensorFlow 高效编程
TensorFlow 高效编程 原文:vahidk/EffectiveTensorflow 译者:FesianXu、飞龙 协议:CC BY-NC-SA 4.0 一、TensorFlow 基础 TensorFlow 和其他数字计算库(如 numpy)之间最明显的区别在于 TensorFlow 中操作的是符号。
1100 0
|
机器学习/深度学习 TensorFlow 算法框架/工具
TensorFlow——入门基础
TensorFlow原理: TensorFlow使用Graph来描述计算任务,图中的节点被称之为op.一个op可以接受0或多个tensor作为输入,也可产生0或多个tensor作为输出.
1337 0