TensorFlow之sparse tensor

embedded/2025/4/1 7:51:20/

目录

  • 前言
  • 创建sparse tensor
  • sparse tensor的运算:

前言

sparse tensor 稀疏tensor, tensor中大部分元素是0, 少部分元素是非0.

创建sparse tensor

python">import tensorflow as tf# indices指示正常值的索引, 即哪些索引位置上是正常值. 
# values表示这些正常值是多少.
# indices和values是一一对应的. [0, 1]表示第0行第1列的值是1, [1,0]表示第一行第0列的值是2, [2, 3]表示第2行第3列的值是3, 以此类推.
# dense_shape表示这个SparseTensor的shape是多少
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s)
# 把sparse tensor转化为稠密矩阵 
print(tf.sparse.to_dense(s))

结果如下:

SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 1. 0. 0.][2. 0. 0. 0.][0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

sparse tensor的运算:

python">
import tensorflow as tf# indices指示正常值的索引, 即哪些索引位置上是正常值. 
# values表示这些正常值是多少.
# indices和values是一一对应的. [0, 1]表示第0行第1列的值是1, [1,0]表示第一行第0列的值是2, [2, 3]表示第2行第3列的值是3, 以此类推.
# dense_shape表示这个SparseTensor的shape是多少
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s)# 乘法
s2 = s * 2.0
print(s2)try:# 加法不支持.s3 = s + 1
except TypeError as ex:print(ex)s4 = tf.constant([[10., 20.],[30., 40.],[50., 60.],[70., 80.]])
# 得到一个3 * 2 的矩阵
print(tf.sparse.sparse_dense_matmul(s, s4))

结果如下:

SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
SparseTensor(indices=tf.Tensor(
[[0 1][1 0][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([2. 4. 6.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
unsupported operand type(s) for +: 'SparseTensor' and 'int'
tf.Tensor(
[[ 30.  40.][ 20.  40.][210. 240.]], shape=(3, 2), dtype=float32)

注意在定义sparse tensor的时候 indices必须是排好序的. 如果不是,定义的时候不会报错, 但是在to_dense的时候会报错:

python">import tensorflow as tfs5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],values = [1., 2., 3.],dense_shape = [3, 4])
print(s5)
#print(tf.sparse.to_dense(s5)) #报错:indices[1] = [0,1] is out of order. Many sparse ops require sorted indices.
# 可以通过reorder对排序, 这样to_dense就没问题了.
s6 = tf.sparse.reorder(s5)
print(tf.sparse.to_dense(s6))

结果如下:

SparseTensor(indices=tf.Tensor(
[[0 2][0 1][2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 2. 1. 0.][0. 0. 0. 0.][0. 0. 0. 3.]], shape=(3, 4), dtype=float32)

http://www.ppmy.cn/embedded/177764.html

相关文章

游戏引擎学习第176天

今天的计划 今天我要做的是,首先讨论一下调试服务(debug services),它们是什么,它们应该如何工作。然后我们可能会开始实现它们,但我不认为我们能做到很远,因为首先要做一些基础的准备工作。通…

MATLAB中getfield函数用法

目录 语法 说明 示例 访问标量结构体的字段 嵌套结构体的字段 结构体数组元素的字段 嵌套结构体数组的索引 字段的元素 getfield函数的功能是结构体数组字段。 语法 value getfield(S,field) value getfield(S,field1,...,fieldN) value getfield(S,idx,field1,..…

SpringMVC 请求处理

SpringMVC 请求处理深度解析:从原理到企业级应用实践 一、架构演进与核心组件协同 1.1 从传统Servlet到前端控制器模式 SpringMVC采用前端控制器架构模式,通过DispatcherServlet统一处理请求,相比传统Servlet的分散处理方式,实…

uv:现代 Python 项目管理的高效助手

在 Python 开发中,我们常用的工具包括 pip、virtualenv、pip-tools 等,但它们各自有局限。由 Astral 团队推出的 uv 则以其极速表现和全能特性,正逐渐成为“Python 的 Cargo”。本文将详细解析 uv 常用命令及其中文说明,助你在日常…

uni-app AES 加密

uni-app 官网没有 加密 API 我们 可以 安装 crypto-js npm install crypto-js他会保存到项目中 node_modules import CryptoJS from ../node_modules/crypto-js //引用AES源码js const keyCode 012345678 //密钥 const ivCode 012345678 //偏移量const key CryptoJS.enc.Ut…

CSS3学习教程,从入门到精通,CSS3 定位布局页面知识点及案例代码(18)

CSS3 定位布局页面知识点及案例代码 一、普通流&#xff08;Normal Flow&#xff09; 知识点 普通流是 CSS 中最基本的布局方式&#xff0c;元素按照其在 HTML 文档中出现的顺序依次排列。块级元素独占一行&#xff0c;内联元素则在同一行排列。 案例代码 <!DOCTYPE ht…

Python----计算机视觉处理(Opencv:图像噪点消除:滤波算法,噪点消除)

一、噪声 噪声&#xff1a;指图像中的一些干扰因素&#xff0c;通常是由图像采集设备、传输信道等因素造成的&#xff0c;表现为图像中随机 的亮度&#xff0c;也可以理解为有那么一些点的像素值与周围的像素值格格不入。常见的噪声类型包括高斯噪声 和椒盐噪声。高斯噪声是一种…

深入 SVG:矢量图形、滤镜与动态交互开发指南

1.SVG 详细介绍 SVG&#xff08;Scalable Vector Graphics&#xff09; 是一种基于 XML 的矢量图形格式&#xff0c;用于描述二维图形。 1. 命名空间 (Namespace) ★ 了解 命名空间 URI&#xff1a;http://www.w3.org/2000/svg 用途&#xff1a;在 XML 或 XHTML 中区分不同标…