您当前的位置: 首页 >  tensorflow

IT之一小佬

暂无认证

  • 2浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

TensorFlow介绍之TF数据流图

IT之一小佬 发布时间:2021-05-25 17:26:00 ,浏览量:2

TensorFlow介绍之TF数据流图 1. TF数据流图 1.1案例:TensorFlow实现一个加法运算

示例代码:

def tensorflow_demo():
    """
    通过简单案例来了解tensorflow的基础结构
    :return: None
    """
    # 一、原生python实现加法运算
    a = 10
    b = 20
    c = a + b
    print("原生Python实现加法运算方法1:\n", c)
    def add(a, b):
        return a + b
    sum = add(a, b)
    print("原生python实现加法运算方法2:\n", sum)

    # 二、tensorflow实现加法运算
    a_t = tf.constant(10)
    b_t = tf.constant(20)
    # 不提倡直接运用这种符号运算符进行计算
    # 更常用tensorflow提供的函数进行计算
    # c_t = a_t + b_t
    c_t = tf.add(a_t, b_t)
    print("tensorflow实现加法运算:\n", c_t)
    # 如何让计算结果出现?
    # 开启会话
    with tf.Session() as sess:
        sum_t = sess.run(c_t)
        print("在sess当中的sum_t:\n", sum_t)

    return None

注意问题:警告指出你的CPU支持AVX运算加速了线性代数计算,即点积,矩阵乘法,卷积等。可以从源代码安装TensorFlow来编译,当然也可以选择关闭

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

查看tensorflow版本:

import tensorflow as tf

print(tf.__version__)

示例代码:

import tensorflow as tf

a = tf.constant(10)
b = tf.constant(20)

# c = a + b
c = tf.add(a, b)
print(c)

运行结果:

此时并没有出现相加得到的和,需要建立一个会话。

示例代码:

import tensorflow as tf

a = tf.constant(10)
b = tf.constant(20)

# c = a + b
c = tf.add(a, b)

with tf.Session() as sess:
    sum_t = sess.run(c)
    print('a+b=', sum_t)

解决如下:

import tensorflow.compat.v1 as tf

a = tf.constant(10)
b = tf.constant(20)

# c = a + b
c = tf.add(a, b)

with tf.Session() as sess:
    sum_t = sess.run(c)
    print('a+b=', sum_t)

最终解决:

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

a = tf.constant(10)
b = tf.constant(20)

# c = a + b
c = tf.add(a, b)

with tf.Session() as sess:
    sum_t = sess.run(c)
    print('a+b=', sum_t)

接下来去掉报红的日志信息:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

a = tf.constant(10)
b = tf.constant(20)

# c = a + b
c = tf.add(a, b)

with tf.Session() as sess:
    sum_t = sess.run(c)
    print('a+b=', sum_t)

1.2 TensorFlow结构分析

TensorFlow 程序通常被组织成一个构建图阶段和一个执行图阶段。

在构建阶段,数据与操作的执行步骤被描述成一个图。

在执行阶段,使用会话执行构建好的图中的操作。

  • 图和会话 :
    • 图:这是 TensorFlow 将计算表示为指令之间的依赖关系的一种表示法
    • 会话:TensorFlow 跨一个或多个本地或远程设备运行数据流图的机制
  • 张量:TensorFlow 中的基本数据对象    【可以理解为numpy中的数组】
  • 节点:提供图当中执行的操作   【图中 的一些运算机制】
1.3 数据流图介绍

TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源框架。

节点(Operation)在图中表示数学操作,线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。

关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.1348s