当前位置:网站首页>Tensorflow2.0 sparse matrix input operation
Tensorflow2.0 sparse matrix input operation
2022-07-23 07:33:00 【biyezuopinvip】
tensorflow-sparsetensor
1. background
Recently, I am doing model training , Found that when importing a large amount of data , Due to pretreatment (concat and reshape Operation etc. ), As a result, the memory will be full , Make the program error . Because there are a lot of sparse input data , When thinking about whether you can input data, use sparse matrix to save , Then input it into the model for training .
2. Sparse matrix input construction
python in scipy.sparse modular , It can effectively store input data sparsely . But the disadvantage is that sparse matrix must only have two-dimensional operation , But generally, image classification is set to multiple dimensions , Therefore, it is necessary to input data in advance reshape Into a two-dimensional matrix .
Suppose the existing image size is [3, 31,31]. among 31 ∗ 31 31*31 31∗31 It's the size of the picture , and 3 3 3 It's a picture. channel. Yes 20w The data of , Memory needs to be stored in advance [200000, 3, 31, 31] Capacity size of , This is not feasible for machines with small memory . So we need to put this first 20w Image sparse matrix operation :
- First, you need to cycle through each picture , At the same time, sparse operation
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
- And then for the obtained list Sparse matrix splicing , You'll get one
[200000 * 3, 31*31]The sparse matrix of , In this way, it can be effectively stored in memory
from scipy import sparse
input_data = sparse.vstack(input_data)
3. Sparse data model training
3.1 utilize tensorflow Medium tf.SparseTensor
stay tensorflow2.0 in , You can wrap the corresponding sparse matrix for input .
- First turn on the scipy The sparse matrix of , convert to tf.SparseTensor Format
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())
- Then when building the model , The input data needs to be reshape, Convert it back to
[batch_size, 3, 31, 31], Only in this way can we train by convolution , The core code is as follows :
# Make sparse matrix reshape operation
input_global_map = tf.sparse.reshape(input_global_map, [-1, 3, 31, 31])
# hold sparsetensor Convert to normal tensor, Only in this way can the model be trained
input_global_map = tf.sparse.to_dense(input_global_map)
边栏推荐
- Shell script
- Image processing solution veimagex technology evolution Road
- “外卖员的时间没有程序员值钱”:读过书就把自己当人上人?得电
- 基于证据理论物联网安全态势感知方法研究
- String in SQL Server_ Implementation of split function
- 小程序毕设作品之微信酒店预订小程序毕业设计(7)中期检查报告
- Zhimeng dedecms forgot to manage the background password retrieval method
- Codeforces Round #808 (Div. 2) A - D
- Redis common basic configuration files
- GNU LD脚本命令语言(二)
猜你喜欢

C language program environment

小程序毕设作品之微信校园二手书交易小程序毕业设计成品(6)开题答辩PPT

Countermeasure and defense method based on softmax activation transformation

删除数组中的重复项(保留最后一次出现的重复元素并保证数组的原有顺序)

程序员45岁之后,绝大部分都被淘汰吗?真相寒了众人的心

Analyse de la stratégie de lecture et d'écriture du cache

基于证据理论物联网安全态势感知方法研究

小程序毕设作品之微信酒店预订小程序毕业设计(5)任务书

Stm32cubeide link script explanation

【随笔】再见IE浏览器,一个时代的印记从此消失
随机推荐
redis的持久化
LAN SDN technology hard core insider 13 II from LAN to Internet
GNU LD脚本命令语言(一)
启牛老师说给开的vip账户安全吗?
Educational Codeforces Round 132 A - D
能量原理与变分法笔记11:形函数(一种降维思想)
用于图像语义分割的GAU与PPM
Countermeasure and defense method based on softmax activation transformation
0day attack path prediction method based on network defense knowledge map
【学习笔记之菜Dog学C】大厂笔试,就这?
千亿营收之后,阿里云生态有了新打法
IP第二次实验 MGRE OSPF
聪明人的游戏提高篇:第三章第二课:因子个数(factor)
“外卖员的时间没有程序员值钱”:读过书就把自己当人上人?得电
LAN SDN technology hard core insider - 02 forward multi-core technology for Moore's law for one second
常用机械设备安全虚拟仿真系统的应用场景及方案
聪明人的游戏提高篇:第三章第三课:找数游戏(find)
dispatch_once 的秘密
【ARC127F】±AB
程序员45岁之后,绝大部分都被淘汰吗?真相寒了众人的心