tensorflow1.x 基础案例1

前言

在这里插入图片描述

在这里插入图片描述
从一些基础案例中慢慢掌握tensorflow:

准备工作:

所需的库:

tensorflow    1.12.0
numpy         1.19.5
matplotlib    3.3.4

1.1 用tensorflow打印“hello,world”

为什么首先学习hello world?

  1. 快速熟悉TensorFlow的基本用法和工作流程。
  2. "Hello World"不需要复杂的依赖,这有助于快速搭建TensorFlow环境,确保所有东西都安装正确。
  3. 通过编写一个简单的程序,学习TensorFlow中的基础概念:会话(Sessions)
import tensorflow as tf

hello = tf.constant('hello, world')  # 创建一个常量操作,创建的操作会被添加为计算图中的一个节点。
# 在TensorFlow中,所有的操作都是计算图中的节点,它们定义了数据(张量)如何流动和计算。
sess = tf.Session()
print(sess.run(hello))

1.2和积运算和矩阵乘法

如何在TensorFlow 1.x中创建图、定义操作、使用会话执行操作以及如何与占位符和动态输入一起工作

  • 常量操作(Constant Operations):

使用tf.constant创建常量。常量在TensorFlow中是不可变的,并且它们的值在图的执行过程中保持不变。
代码中创建了两个常量a和b,分别赋值为2和3。

  • 会话(Session):

TensorFlow 1.x需要会话来执行图中的操作。会话是执行图的上下文,它负责协调操作的执行。
使用with tf.Session() as sess:创建会话,并使用as关键字自动管理会话的生命周期。

  • 图(Graph):

TensorFlow程序是基于图的。图中的节点表示操作,边表示数据流(张量)。
代码中的a+b和a*b是图中的操作,它们在会话中被执行。

  • 占位符(Placeholders):

使用tf.placeholder创建占位符。占位符是图中的一个节点,它在执行图之前需要被赋予具体的值。
代码中的a和b作为占位符,允许在会话运行时提供动态输入。

  • 操作(Operations):

代码定义了两个操作:add和mul,分别对应加法和乘法。
这些操作在会话中通过sess.run执行,需要提供输入值。

  • 矩阵乘法(Matrix Multiplication):

使用tf.matmul进行矩阵乘法操作。这是深度学习中常用的操作之一。
代码创建了两个矩阵常量matrix1和matrix2,并进行了矩阵乘法。

  • 执行操作(Executing Operations):

使用sess.run执行图中的操作。对于product,它需要matrix1和matrix2作为输入,这些输入在会话中自动执行。
输出结果以NumPy数组的形式返回。

  • Feeding机制:

对于占位符,需要使用feed_dict参数在sess.run中提供输入值。这是TensorFlow 1.x中提供动态输入的方式。


import tensorflow as tf

# 和积运算方式1
a = tf.constant(2)
b = tf.constant(3)

with tf.Session() as sess:
    print("__________________")
    print("a:", sess.run(a), " b:", sess.run(b))
    print("a+b=", sess.run(a+b))
    print("a*b=", sess.run(a * b))

# 和积运算方式2
add = tf.add(a, b)
mul = tf.multiply(a, b)

with tf.Session() as sess:
    print("变量和为:", sess.run(add, feed_dict={a: 2, b: 3}))
    print("变量积为:", sess.run(mul, feed_dict={a: 2, b: 3}))

# 矩阵乘法
matrix1 = tf.constant([[3., 1.]])
matrix2 = tf.constant([[1.], [2.]])
product = tf.matmul(matrix2, matrix1)

with tf.Session() as sess:
    result = sess.run(product)
    print(result)

运行结果:

a: 2  b: 3
a+b= 5
a*b= 6
变量和为: 5
变量积为: 6
[[3. 1.]
 [6. 2.]]

1.3TensorFlow Eager API

Eager API的主要特点:

  1. 即时执行:操作在调用时立即执行,无需构建计算图和启动会话。
  2. 易用性:简化了TensorFlow的使用,使得研究和开发更加直观。
  3. 兼容性:大多数TensorFlow API在Eager模式下与非Eager模式下保持一致。
  4. 转换性:Eager模式编写的模型可以转换为计算图,以便进行优化和部署。
import numpy as np
import tensorflow as tf

# 设置Eager API
tf.enable_eager_execution()
tfe = tf.contrib.eager

# 定义常数张量
a = tf.constant(2)
b = tf.constant(3)

# 不用tf.Session,进行操作
c0 = a + b
d0 = a * b

# 与Numpy完全兼容
a = tf.constant([[2, 1], [1, 0]], dtype=tf.float32)
b = np.array([[3, 0], [5, 1]], dtype=np.float32)
c1 = a + b
d1 = tf.matmul(a, b)
print(c0, d0, c1, d1)

# 遍历张量
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])

结果·:

tf.Tensor(5, shape=(), dtype=int32) 
tf.Tensor(6, shape=(), dtype=int32) 
tf.Tensor(
[[5. 1.]
 [6. 1.]], shape=(2, 2), dtype=float32) 
 tf.Tensor(
[[11.  1.]
 [ 3.  0.]], shape=(2, 2), dtype=float32)
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0.0, shape=(), dtype=float32)

相关推荐

  1. tensorflow1基础函数学习

    2024-07-12 22:40:03       22 阅读
  2. 关于安装TensorFlow1.x版本的一些注意事项

    2024-07-12 22:40:03       37 阅读
  3. M1安装tensorflow

    2024-07-12 22:40:03       63 阅读
  4. TensorFlow 1.x 版本保存模型的三种方式的优缺点

    2024-07-12 22:40:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 22:40:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 22:40:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 22:40:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 22:40:03       69 阅读

热门阅读

  1. 使用 Spring Boot + POI 实现动态 DOCX 模版导出

    2024-07-12 22:40:03       23 阅读
  2. DelayQueue实现延时任务

    2024-07-12 22:40:03       18 阅读
  3. P1035 [NOIP2002 普及组] 级数求和 题解

    2024-07-12 22:40:03       20 阅读
  4. 留学生需要注意所谓的“写作套路”

    2024-07-12 22:40:03       23 阅读
  5. C++ 项目实践课设 图书馆管理系统

    2024-07-12 22:40:03       17 阅读
  6. 计算机网络 5.6网桥与交换机

    2024-07-12 22:40:03       15 阅读