深度学习基础之《TensorFlow框架(1)—TF数据流图》

一、TensorFlow实现一个加法运算

day01_deeplearning.py

python 复制代码
import tensorflow as tf

def tensorflow_demo():
    """
    TensorFlow的基本结构
    """

    # TensorFlow实现加减法运算
    a_t = tf.constant(2)
    b_t = tf.constant(3)
    c_t = a_t + b_t
    print("TensorFlow加法运算结果:\n", c_t)
    print(c_t.numpy())

    # 2.0版本不需要开启会话,已经没有会话模块了

    return None

if __name__ == "__main__":
    # 代码1:TensorFlow的基本结构
    tensorflow_demo()
bash 复制代码
python3 day01_deeplearning.py

2024-02-16 01:04:36.715081: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2024-02-16 01:04:36.715126: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2024-02-16 01:04:38.803888: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2024-02-16 01:04:38.803994: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2024-02-16 01:04:38.804045: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (server001): /proc/driver/nvidia/version does not exist
2024-02-16 01:04:38.804692: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
TensorFlow加法运算结果:
 tf.Tensor(5, shape=(), dtype=int32)
5

二、TensorFlow结构分析

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

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

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

TensorFlow1.x构建和执行是分成两个步骤,TensorFlow2.x升级到了即时执行模式,所以就不需要会话了

参考资料:

https://www.jianshu.com/p/006d1292402b

https://blog.csdn.net/weixin_40920183/article/details/106718315

注:Session函数在2.x版本中有保留的tf.compat.v1.Session

2、图

这是TensorFlow将计算表示为指令之间的依赖关系的一种表示法

图定义了数据和操作的步骤

3、会话

TensorFlow1.x中跨一个或多个本地或远程设备运行数据流图的机制

4、张量(Tensor)

TensorFlow中的基本数据对象

5、节点

提供图当中执行的操作

三、其他注意点

1、不打印警告信息

添加:

python 复制代码
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

注意:要写在import tensorflow as tf的前面

2、如果要完全弃用2.x的功能(不建议)

添加:

python 复制代码
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
相关推荐
子午18 分钟前
【植物识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
sensen_kiss20 分钟前
INT301 Bio-computation 生物计算(神经网络)Pt.10 联想存储器与HOPFIELD网络
人工智能·深度学习·神经网络
All The Way North-22 分钟前
PyTorch 二分类损失函数详解:BCELoss vs BCEWithLogitsLoss 最佳实践指南
人工智能·pytorch·深度学习·机器学习·二分类损失函数
严文文-Chris25 分钟前
神经网络的前向传播、反向传播、优化器分别是什么?有什么关系?
人工智能·深度学习·神经网络
梯度下降不了班11 小时前
【mmodel/xDit】Cross-Attention 深度解析:文生图/文生视频的核心桥梁
人工智能·深度学习·ai作画·stable diffusion·音视频·transformer
齐齐大魔王12 小时前
深度学习(三)
人工智能·深度学习
ziwu13 小时前
【鱼类识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
ziwu13 小时前
【植物识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
金融小师妹15 小时前
基于NLP政策信号解析的联邦基金利率预测:美银动态调整12月降息概率至88%,2026年双降路径的强化学习模拟
大数据·人工智能·深度学习·1024程序员节
JobDocLS15 小时前
深度学习软件安装
人工智能·深度学习