当前位置:网站首页>tensorflow2.0稀疏矩阵输入操作
tensorflow2.0稀疏矩阵输入操作
2022-07-22 20:35:00 【biyezuopinvip】
tensorflow-sparsetensor
1.背景
最近在做模型训练,发现在导入大量数据时,由于要进行预处理(concat和reshape操作等),导致内存会占满,使得程序出错。由于输入数据存在大量的稀疏情况,想着能不能输入数据时利用稀疏矩阵进行保存,然后输入到模型中进行训练。
2.稀疏矩阵输入构造
python中scipy.sparse模块,能够有效的对输入数据进行稀疏化存储。但缺点在于稀疏矩阵必定只有两维的操作,但一般图片分类设置到多个维度,因此需要提前把输入数据reshape成两维矩阵。
假设现有的图片大小为[3, 31,31]。其中 31 ∗ 31 31*31 31∗31是图片的大小,而 3 3 3是图片channel。有20w的数据,则内存需要提前存储[200000, 3, 31, 31]的容量大小,这对于小内存的机器来说是不可行的。因此需要先把这20w图片进行稀疏化矩阵操作:
- 首先需要循环读取每一张图片,同时进行稀疏化操作
import numpy as np
from scipy.sparse import csr_matrix
input_data = []
with open(file, "r") as f:
while True:
line = f.readline()
if line:
fig = csr_matrix(np.reshape(line, [3, 31*31]))
input_data.append(fig)
else:
break
- 然后对取得的list进行稀疏化矩阵拼接,会得到一个
[200000 * 3, 31*31]的稀疏矩阵,这样就能够有效的在内存中进行存储
from scipy import sparse
input_data = sparse.vstack(input_data)
3.稀疏数据模型训练
3.1 利用tensorflow中的tf.SparseTensor
在tensorflow2.0中,可以包装对应的稀疏矩阵进行输入。
- 首先把scipy的稀疏矩阵,转换成tf.SparseTensor格式
def get_sparse_tensor(input_data):
indices = list(zip(*input_data.nonzero()))
return tf.SparseTensor(indices=indices, values=np.float32(input_data.data), dense_shape=input_data.get_shape())
- 然后在模型构建时,需要把输入的数据进行reshape,重新转换成
[batch_size, 3, 31, 31],这样才能用卷积的方法进行训练,核心代码如下:
# 把稀疏矩阵进行reshape操作
input_global_map = tf.sparse.reshape(input_global_map, [-1, 3, 31, 31])
# 把sparsetensor转换成普通tensor,这样模型才能够训练
input_global_map = tf.sparse.to_dense(input_global_map)
边栏推荐
- C language program environment
- Nftscan and ATEM network have reached strategic cooperation in the field of NFT data
- GNU LD脚本命令语言(一)
- 直播预告 | openGauss的自治运维平台DBMind实践分享
- What if the file copied by SSD is only dozens of KB? Solution to slow copying speed after installing hard disk in computer
- Detailed explanation of CAN bus
- 基于证据理论物联网安全态势感知方法研究
- rs485通信OSI模型网络层
- Countermeasure and defense method based on softmax activation transformation
- How to open the tutorial of administrator permission setting for computer administrator permission
猜你喜欢

Countermeasure and defense method based on softmax activation transformation

从BIO到实现简单多人聊天室功能--IO模型

常用机械设备安全虚拟仿真系统的应用场景及方案

小程序毕设作品之微信酒店预订小程序毕业设计(7)中期检查报告

在项目开发中的Logback日志框架技术

uniapp切换tab栏显示不同页面并记住页面位置和上拉获取新数据

【随笔】再见IE浏览器,一个时代的印记从此消失

Ambire Gas Tank 推出独家 NFT 投放

【计网实验报告】Cisco局域网模拟组建、简单网络测试

Esphone's self-made infrared remote control is connected to ha to control lights, switches, etc. any remote control can be used
随机推荐
直播预告 | openGauss的自治运维平台DBMind实践分享
基于以太坊状态数据库的攻击与防御方案
Google cloud and Oracle cloud are "hot"? It is imperative to deploy cross cloud disaster recovery!
[FAQ] common reasons and solutions for the failure of in app payment services to pull up the payment page
LAN SDN technology hard core insider 13 II from LAN to Internet
gnu 伪指令定义函数
图像处理解决方案 veImageX 技术演进之路
How to get administrator permissions on the computer tutorial on setting administrator permissions on the computer
国泰君安证券股票开户怎么样安全吗
在项目开发中的Logback日志框架技术
Redis——JedisConnectionException Could not get a resource from the pool
CAN总线详解
常用机械设备安全虚拟仿真系统的应用场景及方案
How to adjust the resolution of the computer with two monitors? Skills of setting different resolutions for two monitors
【JS】将字符串保存成文件到本地(.txt、.json、.md...)
What if you need system permission to delete files? You need permission from system to delete the solution
GNU pseudo instruction definition function
Image processing solution veimagex technology evolution Road
Detailed explanation of CAN bus
能量原理与变分法笔记11:形函数(一种降维思想)