计算机视觉和图像处理
Tensorflow入门
一、深度学习框架—TensorFlow
1.1 TensorFlow介绍
TensorFlow 是一个非常强大且流行的开源机器学习框架,可以让开发者轻松构建和部署各种类型的机器学习模型,包括但不限于深度学习模型。
TensorFlow 的主要组件
- Tensor:TensorFlow 中的基本数据结构,是一个多维数组,可以用来表示数据。
- Graph:TensorFlow 1.x 中使用的一种数据流图,用于描述计算过程。
- Session:TensorFlow 1.x 中用于执行 Graph 中的计算。
- Eager Execution:TensorFlow 2.x 引入的一种即时执行模式,允许开发者以更直观的方式编写和调试代码。
- Keras:一个高级神经网络 API,自 TensorFlow 2.0 开始集成到 TensorFlow 中,提供了易于使用的接口来构建和训练模型。
1.2 安装TensorFlow
- 创建虚拟环境
在Anaconda终端中创建虚拟环境tf_env
conda create --name tf_env
- 激活虚拟环境
conda activate tf_env
- 安装TensorFlow
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
- 查看是否安装成功
import tensorflow as tf
rf._version_
1.3 张量及其操作
1.3.1 张量Tensor
张量是⼀个多维数组。 与NumPy ndarray对象类似
import tensorflow as tf
import numpy as np
- 创建张量
# 创建0维张量,即标量(数字)
tf.constant(3)
# 创建一维张量
tf.constant([1.0, 2.0, 3.0])
# 创建二维张量
tf.constant([[1,2],[3,4],[5,6]],dtype=tf.float16)
# 创建三维张量
tf.constant([[[1,2,3,4,5],[6,7,8<