Tensorflow 2.0 常见函数用法(一)

news/2024/6/18 21:24:06 标签: tensorflow, 人工智能, 深度学习, python

文章目录

  • 0. 基础用法
  • 1. tf.cast
  • 2. tf.keras.layers.Dense
  • 3. tf.variable_scope
  • 4. tf.squeeze
  • 5. tf.math.multiply


0. 基础用法

Tensorflow 的用法不定期更新遇到的一些用法,之前已经包含了基础用法参考这里 ,具体包含如下图的方法:
在这里插入图片描述
本文介绍其他常见的方法。

1. tf.cast

张量类型强制转换

官方用法:

python">tf.cast(
    x, dtype, name=None
)

示例:

python">x = tf.constant([1.8, 2.2], dtype=tf.float32)
print(tf.cast(x, tf.int32))

# 输出
tf.Tensor([1 2], shape=(2,), dtype=int32)

2. tf.keras.layers.Dense

构建一个全连接层

在1.0中是 tf.layers.dense ,2.0中可以用下面方法兼容:

python">import tensorflow.compat.v1 as tf
tf.layers.dense(xxx)

官方用法:

python">tf.keras.layers.Dense(
    units,
    activation=None,
    use_bias=True,
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    **kwargs
)

3. tf.variable_scope

这是 v1 版本的用法,用于管理变量

官方用法:

python">tf.compat.v1.variable_scope(
    name_or_scope,
    default_name=None,
    values=None,
    initializer=None,
    regularizer=None,
    caching_device=None,
    partitioner=None,
    custom_getter=None,
    reuse=None,
    dtype=None,
    use_resource=None,
    constraint=None,
    auxiliary_name_scope=True
)

示例:

python">import tensorflow as tf
with tf.variable_scope("one"):
    o=tf.get_variable("f",[1])
with tf.variable_scope("two"):
    o1=tf.get_variable("f",[1])

# 抛错,因为变量的作用范围不一样
# 一个作用域是one/f,一个作用域是two/f
assert o == o1

4. tf.squeeze

从张量的形状中移除大小为1的维度。该函数返回一个张量,这个张量是将原始input中所有维度为1的那些维都删掉的结果。
axis 可以用来指定要删掉的为1的维度,此处要注意指定的维度必须确保其是1,否则会报错。

官方用法:

python">tf.squeeze(
    input, axis=None, name=None
)

示例:

python"># 注意,a的shape是1*6,即存在一个大小为1的维度
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[1, 6])
print(a)
b = tf.squeeze(a, [0]) # 删除第0个维度为1的
# b = tf.squeeze(a) 的结果是一样的
print(b)

# 输出
tf.Tensor([[1 2 3 4 5 6]], shape=(1, 6), dtype=int32)
tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
python">a = tf.constant([1, 2, 3, 4, 5, 6], shape=[6, 1])
print(a)
b = tf.squeeze(a, [1])
print(b)

# 输出
tf.Tensor(
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]], shape=(6, 1), dtype=int32)
tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
python">a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
print(a)
b = tf.squeeze(a) # 如果不存在大小为1的维度,那么保持不变
print(b)

# 输出
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)

5. tf.math.multiply

元素相乘

在 1.0 中是 tf.multiply
官方用法:

python">tf.math.multiply(
    x, y, name=None
)

示例:

python">a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
print(tf.multiply(a, 2))
print(tf.multiply(a, a))

# 输出
tf.Tensor(
[[ 2  4  6]
 [ 8 10 12]], shape=(2, 3), dtype=int32)

tf.Tensor(
[[ 1  4  9]
 [16 25 36]], shape=(2, 3), dtype=int32)
python">x = tf.ones([1, 2]);
y = tf.ones([2, 1]);
print(x * y)  # Taking advantage of operator overriding
print(tf.multiply(x, y))

# 输出,如果维度不一致,会尝试匹配维度
tf.Tensor(
[[1. 1.]
 [1. 1.]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[1. 1.]
 [1. 1.]], shape=(2, 2), dtype=float32)

http://www.niftyadmin.cn/n/5445983.html

相关文章

7-24 两个整数最大值

本题我将用两种方法来解,不知道大家有没有发现,这道题非常适合三目运算符呢。 求两个整数a,b的最大值,结果存入max中。 输入格式: 在一行中输入两个整数,之间用一个空格间隔,没有其它任何附加字符。 输…

高通 8255 基本通信(QUP)Android侧控制方法说明

一:整体说明 高通8255芯片中,SPI IIC UART核心统一由QUP V3 进行控制 QUP V3为可编程模块,可以将不同通道配置为SPI IIC UART通路,此部分配置在QNX侧 QUP 资源可以直接被QNX使用,Android侧可以通过两种方法使用QUP资源…

力扣由浅至深 每日一题.11 加一

少年气,是历经千帆举重若轻地沉淀,也是乐观淡然笑对生活的豁达 —— 24.3.22 加一 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。 你可以…

C语言中动态内存相关的4个函数free、malloc、calloc、realloc,常⻅的动态内存的错误

目录 free malloc calloc realloc 常见的动态内存的错误 1.对NULL指针的解引⽤操作 2.对动态开辟空间的越界访问 3.对⾮动态开辟内存使⽤free释放 4.使⽤free释放⼀块动态开辟内存的⼀部分 5.对同⼀块动态内存多次释放 6.动态开辟内存忘记释放(内存泄漏&a…

【Flask】Flask项目结构初识

1.前提准备 Python版本 # python 3.8.0 # 查看Python版本 python --version 安装第三方 Flask pip install flask # 如果安装失败,可以使用 -i,指定使用国内镜像源 # 清华镜像源:https://pypi.tuna.tsinghua.edu.cn/simple/ 检查 Flask 是…

【数字IC/FPGA】书籍推荐(1)----《轻松成为设计高手--Verilog HDL实用精解》

在下这几年关于数字电路、Verilog、FPGA和IC方面的书前前后后都读了不少,发现了不少好书,也在一些废话书上浪费过时间。接下来会写一系列文章,把一部分读过的书做个测评,根据个人标准按十分制满分来打分分享给大家。 书名&#xf…

YOLOv5独家改进:backbone改进 | 视觉新主干!RMT:RetNet遇见视觉Transformer | CVPR2024

💡💡💡本文独家改进:RMT:一种强大的视觉Backbone,灵活地将显式空间先验集成到具有线性复杂度的视觉主干中,在多个下游任务(分类/检测/分割)上性能表现出色! 💡💡💡Transformer 在各个领域验证了可行性,在多个数据集下能够实现涨点 改进结构图如下: 收…

5.4.2、【AI技术新纪元:Spring AI解码】OpenAI Text-to-Speech (TTS) Integration

OpenAI文本转语音(TTS)集成 简介 音频API基于OpenAI的TTS(文本转语音)模型提供了一个语音端点,用户可以: 朗读一篇书面博客文章。以多种语言生成语音输出。利用流媒体实现实时音频输出。必要条件 创建OpenAI账号并获取API密钥。您可以在OpenAI注册页面上注册,并在API密…