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()
目录
相关文章
|
安全 应用服务中间件 网络安全
开源对象存储Minio部署篇
MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。(摘自minio中文站点https://docs.min.io/cn/ 里面有很详细的介绍)。minio部署简单易用,分布式方式部署可以水平扩容且数据分散存储在所有节点上,只要在线节点N/2数据就非常安全,这类似raid6,不同的是卷级别和对象级别,使用方式有网页端、linux端工具mc、多语言SDK。
1623 0
开源对象存储Minio部署篇
|
负载均衡
slb自定义健康检查路径
slb自定义健康检查路径
201 3
|
消息中间件 负载均衡 中间件
【Alibaba中间件技术系列】「RocketMQ技术专题」让我们一起探索一下DefaultMQPullConsumer的实现原理及源码分析
【Alibaba中间件技术系列】「RocketMQ技术专题」让我们一起探索一下DefaultMQPullConsumer的实现原理及源码分析
355 83
【Alibaba中间件技术系列】「RocketMQ技术专题」让我们一起探索一下DefaultMQPullConsumer的实现原理及源码分析
|
机器学习/深度学习 编解码 弹性计算
云上视频处理:重塑视频行业的未来版图
云上视频处理技术作为视频行业的重要支撑力量,正以其高效、灵活、可扩展的优势引领着视频行业的未来发展。从在线视频平台到短视频平台再到智能安防等各个领域都能看到云上视频处理技术的身影
377 2
|
机器学习/深度学习 Ubuntu Linux
openpose原理及安装教程(姿态识别)
openpose原理及安装教程(姿态识别)
|
应用服务中间件 nginx
【Nginx学习】—Nginx基本知识
【Nginx学习】—Nginx基本知识
多线程
java多线程是指在程序中并发执行多个指令流,每个指令流都称为一个线程,彼此间互相独立。Java中所有变量都储存在主存中,对于所有线程都是共享的。Java中的多线程可以通过继承Thread类或者实现Runnable接口来创建,然后通过start()方法来启动线程。Java中的多线程可以提高程序的并发性和执行效率。
|
SQL 存储 分布式计算
一次Delta lake 0.8.0 踩坑有感:使用新框架的新版本,一定要今早关注多多关注社区动态
一次Delta lake 0.8.0 踩坑有感:使用新框架的新版本,一定要今早关注多多关注社区动态
|
缓存 运维 负载均衡
1. 微服务架构上篇:4. 基于etcd的服务发现与注册
1. 微服务架构上篇:4. 基于etcd的服务发现与注册
|
存储 前端开发 NoSQL
【畅购商城】购物车模块之查看购物车
【畅购商城】购物车模块之查看购物车
227 0
【畅购商城】购物车模块之查看购物车