Numpy学习下 Taks 1

本文介绍了如何使用numpy进行数据的二进制保存和加载,以提高深度学习项目的训练效率。讨论了.npy和.npz格式,以及如何通过numpy的set_printoptions设置输出格式,控制精度、阈值等参数。并提供了多个示例展示设置方法及其效果。通过掌握这些技巧,可以优化数据处理流程,加速项目运行。
摘要由CSDN通过智能技术生成

输入和输出

前言

使用numpy将数据转为二进制数据保存并加载,在许多实际场景有着非常重要的应用。比如:深度学习项目的数据加载过程,通常需要繁杂的加载、格式转化、预处理等。这样的过程花费时间较大,如果每次训练都需要加载,则大大加长整个训练时长。因此可以将处理好的tensor保存为二进制文件,下次训练时直接加载这个tensor,一个项目应用例子是[1]。

这次接着组队学习的机会,系统了解下numpy相关输入输出操作。

知识点

将tensor转换为不同的格式可总结为下表

数据格式解释存储方式加载方式
.npy这个格式以二进制的方式存储文件,在二进制文件第一行以文本形式保存了数据的元信息(ndim,dtype,shape等),可以用二进制工具查看内容。np.save()np.load()
.npz这个格式以压缩打包的方式存储文件,可以用压缩软件解压。np.savez()np.load()
.txt文本np.sav etxt()np.loadtxt(),genfromtxt()(更强大)

文本格式选项

设置打印的方式

  • numpy.set_printoptions(precision=None,threshold=None, edgeitems=None,linewidth=None, suppress=None, nanstr=None, infstr=None,formatter=None, sign=None, floatmode=None, **kwarg) Set printing options.
    • precision:设置浮点精度,控制输出的小数点个数,默认是8。
    • threshold:概略显示,超过该值则以“…”的形式来表示,默认是1000。
    • linewidth:用于确定每行多少字符数后插入换行符,默认为75。
    • suppress:当suppress=True,表示小数不需要以科学计数法的形式输出,默认是False。
    • nanstr:浮点非数字的字符串表示形式,默认nan
    • infstr:浮点无穷大的字符串表示形式,默认inf

These options determine the way floating point numbers, arrays and other NumPy objects are displayed.

【例】

import numpy as np

np.set_printoptions(precision=4)
x = np.array([1.123456789])
print(x)  # [1.1235]

np.set_printoptions(threshold=20)
x = np.arange(50)
print(x)  # [ 0  1  2 ... 47 48 49]

np.set_printoptions(threshold=np.iinfo(np.int).max)
print(x)
# [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#  24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#  48 49]

eps = np.finfo(float).eps
x = np.arange(4.)
x = x ** 2 - (x + eps) ** 2
print(x)  
# [-4.9304e-32 -4.4409e-16  0.0000e+00  0.0000e+00]
np.set_printoptions(suppress=True)
print(x)  # [-0. -0.  0.  0.]

x = np.linspace(0, 10, 10)
print(x)
# [ 0.      1.1111  2.2222  3.3333  4.4444  5.5556  6.6667  7.7778  8.8889
#  10.    ]
np.set_printoptions(precision=2, suppress=True, threshold=5)
print(x)  # [ 0.    1.11  2.22 ...  7.78  8.89 10.  ]
  • numpy.get_printoptions() Return the current print options.

【例】

import numpy as np

x = np.get_printoptions()
print(x)
# {
# 'edgeitems': 3, 
# 'threshold': 1000, 
# 'floatmode': 'maxprec', 
# 'precision': 8, 
# 'suppress': False, 
# 'linewidth': 75, 
# 'nanstr': 'nan', 
# 'infstr': 'inf', 
# 'sign': '-', 
# 'formatter': None, 
# 'legacy': False
# }

练习题

  • 打印小数点后三位

precision:控制输出的小数点个数,默认是8

np.set_printoptions(precision=3)
  • 将numpy数组a中打印的项数限制为最多6个元素

threshold:控制输出的值的个数,其余以…代替;
当设置打印显示方式threshold=np.nan,意思是输出数组的时候完全输出,不需要省略号将中间数据省略

np.set_printoptions(threshold=6)
  • 打印完整的numpy数组a而不中断
np.set_printoptions(threshold=np.nan)
# or
np.set_printoptions(threshold=np.iinfo(np.int).max) 

注意点

经试验,np.set_printoptions(threshold=t),其中t小于6,打印的还是6个,只有超过array最大值,才会全部打印。例如

import numpy as np
np.set_printoptions(threshold=10)
a = np.arange(15)
print(a)
[ 0  1  2 ... 12 13 14]

参考资料

[1] https://github.com/jonas-koehler/s2cnn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值