Tensorflow 2.0 常见函数用法(一)

文章目录

  • [0. 基础用法](#0. 基础用法)
  • [1. tf.cast](#1. tf.cast)
  • [2. tf.keras.layers.Dense](#2. tf.keras.layers.Dense)
  • [3. tf.variable_scope](#3. tf.variable_scope)
  • [4. tf.squeeze](#4. tf.squeeze)
  • [5. tf.math.multiply](#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)
相关推荐
cooldream20091 小时前
华为云Flexus+DeepSeek征文|基于华为云Flexus X和DeepSeek-R1打造个人知识库问答系统
人工智能·华为云·dify
老胖闲聊4 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1184 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
scdifsn5 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
DFminer5 小时前
【LLM】fast-api 流式生成测试
人工智能·机器人
lyaihao5 小时前
使用python实现奔跑的线条效果
python·绘图
郄堃Deep Traffic5 小时前
机器学习+城市规划第十四期:利用半参数地理加权回归来实现区域带宽不同的规划任务
人工智能·机器学习·回归·城市规划
ai大师6 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
海盗儿6 小时前
Attention Is All You Need (Transformer) 以及Transformer pytorch实现
pytorch·深度学习·transformer