zoukankan      html  css  js  c++  java
  • 机器学习系列-tensorflow-01-急切执行API

    tensorflow急切执行概述

    Eager execution is an imperative, define-by-run interface where operations are executed immediately as they are called from Python. This makes it easier to get started with TensorFlow, and can make research and development more intuitive. A vast majority of the TensorFlow API remains the same whether eager execution is enabled or not. As a result, the exact same code that constructs TensorFlow graphs (e.g. using the layers API) can be executed imperatively by using eager execution. Conversely, most models written with Eager enabled can be converted to a graph that can be further optimized and/or extracted for deployment in production without changing code. **急切执行**是一个必要的,逐个运行的界面,其中操作在从Python调用时立即执行。 这使得TensorFlow开始变得更容易,并且可以使研究和开发更加直观。 无论是否启用了急切执行,绝大多数TensorFlow API都保持不变。 通过使用急切执行,可以强制执行构造TensorFlow图的完全相同的代码。 相反,大多数使用Eager编写的模型都可以转换为可以进一步优化和/或提取的图形,以便在不更改代码的情况下在生产中进行部署。

    代码图解分析如下

    代码

    from __future__ import absolute_import, division, print_function
    
    import numpy as np
    import tensorflow as tf
    import tensorflow.contrib.eager as tfe
    
    # Set Eager API
    print("Setting Eager mode...")
    tfe.enable_eager_execution()
    
    # Define constant tensors
    print("Define constant tensors")
    a = tf.constant(2)
    print("a = %i" % a)
    b = tf.constant(3)
    print("b = %i" % b)
    
    # Run the operation without the need for tf.Session
    print("Running operations, without tf.Session")
    c = a + b
    print("a + b = %i" % c)
    d = a * b
    print("a * b = %i" % d)
    
    
    # Full compatibility with Numpy
    print("Mixing operations with Tensors and Numpy Arrays")
    
    # Define constant tensors
    a = tf.constant([[2., 1.],
                     [1., 0.]], dtype=tf.float32)
    print("Tensor:
     a = %s" % a)
    b = np.array([[3., 0.],
                  [5., 1.]], dtype=np.float32)
    print("NumpyArray:
     b = %s" % b)
    
    # Run the operation without the need for tf.Session
    print("Running operations, without tf.Session")
    
    c = a + b
    print("a + b = %s" % c)
    
    d = tf.matmul(a, b)
    print("a * b = %s" % d)
    
    print("Iterate through Tensor 'a':")
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            print(a[i][j])
    

    参考点

    https://github.com/brightyu/TensorFlow-Examples/blob/master/examples/
  • 相关阅读:
    推荐一款Notepad++主题Dracula
    一个小工具,利用php把指定目录文件递归上传到阿里云OSS
    svn2个小问题的解决
    借助Algorithmia网站API:用AI给黑白照片上色,复现记忆中的旧时光
    C++@sublime GDB调试
    C++@重载函数
    C++@语句块
    C++@命名空间(转)
    《Linux与Qt程序设计》知识框架
    多线程中的使用共享变量的问题 (转)
  • 原文地址:https://www.cnblogs.com/brightyuxl/p/9880172.html
Copyright © 2011-2022 走看看