TensorFlow学习笔记(1):Introduction章helloworld与basic_operations篇
TensorFlow学习笔记(1):Introduction章helloworld与basic_operations篇
本文是 TensorFlow 学习笔记系列的实操第一篇,主要介绍 TensorFlow 的基础概念和张量运算。通过实际代码示例,帮助初学者快速入门 TensorFlow 2.x。
一、helloworld
1. 简介
这是一个最基础的 TensorFlow 入门示例,通过创建和操作简单的张量来帮助初学者理解 TensorFlow 的基本概念。
2. 环境准备
TensorFlow 2.x 版本的安装与配置:
1
import tensorflow as tf
3. 张量(Tensor)基础
张量是 TensorFlow 中的核心概念,它是一个多维数组,类似于 NumPy 的 ndarray。
3.1 创建第一个张量
1
2
3
# 创建一个字符串张量
hello = tf.constant("hello world")
print(hello) # 输出张量对象信息
3.2 访问张量值
1
2
# 使用 numpy() 方法获取张量的具体值
print(hello.numpy()) # 输出: b'hello world'
4. 重要概念解释
- tf.constant()
- 用于创建常量张量
- 支持多种数据类型(字符串、数值等)
- 创建后值不可改变
- 张量的属性
- shape: 张量的形状
- dtype: 数据类型
- device: 所在设备(CPU/GPU)
5. 注意事项
- TensorFlow 2.x 采用即时执行模式
- 不需要创建会话(Session)
- 张量可以直接通过 numpy() 方法转换为 NumPy 数组
6. 练习建议
- 尝试创建不同类型的张量(整数、浮点数等)
- 打印张量的各种属性
- 尝试在 CPU 和 GPU 上创建张量
参考资源
- TensorFlow 官方文档:https://tensorflow.org
- GitHub 项目地址:https://github.com/aymericdamien/TensorFlow-Examples/
二、basic_operations
1. 简介
这是一个关于 TensorFlow 2.x 基本张量操作的教程。本教程将帮助你理解 TensorFlow 中最基础也是最重要的概念 - 张量(Tensor)操作。
2. 环境准备
首先需要导入必要的库:
1
2
from __future__ import print_function
import tensorflow as tf
3. 张量常量定义
在 TensorFlow 中,我们可以使用 tf.constant()
来创建常量张量:
1
2
3
a = tf.constant(2) # 创建标量张量
b = tf.constant(3)
c = tf.constant(5)
4. 基本数学运算
TensorFlow 提供了丰富的张量运算操作:
4.1 算术运算
1
2
3
4
add = tf.add(a, b) # 加法
sub = tf.subtract(a, b) # 减法
mul = tf.multiply(a, b) # 乘法
div = tf.divide(a, b) # 除法
注意:也可以直接使用 Python 运算符 (+, -, *, /) 进行运算。
4.2 统计运算
1
2
mean = tf.reduce_mean([a, b, c]) # 计算平均值
sum = tf.reduce_sum([a, b, c]) # 计算总和
5. 矩阵运算
TensorFlow 支持强大的矩阵运算功能:
1
2
3
4
5
6
7
8
# 创建 2x2 矩阵
matrix1 = tf.constant([[1., 2.],
[3., 4.]])
matrix2 = tf.constant([[5., 6.],
[7., 8.]])
# 矩阵乘法
product = tf.matmul(matrix1, matrix2)
6. 张量与 NumPy 互转
TensorFlow 张量可以方便地转换为 NumPy 数组:
1
numpy_array = product.numpy()
7. 关键要点
- TensorFlow 2.x 采用即时执行模式,无需创建会话(Session)
- 基本数学运算支持 Python 运算符
- 张量可以方便地与 NumPy 数组互转
- 张量支持 GPU 加速计算
8. 练习建议
- 尝试创建不同维度的张量
- 实践各种数学运算
- 尝试更复杂的矩阵运算
- 熟悉张量的属性(如 shape, dtype 等)
参考资源
- TensorFlow 官方文档:https://tensorflow.org
- GitHub 项目地址:https://github.com/aymericdamien/TensorFlow-Examples/
This post is licensed under CC BY 4.0 by the author.