什么是npy文件,为什么要用npy格式保存文件?

一、官方文档(中文)

在这里插入图片描述

二、官方文档(英文)

在这里插入图片描述

三、为什么要用npy文件保存文件

在这里插入图片描述

  • A LOT faster, also notice that we didn’t need to reshape the data since that information was contained in the .npy file.(速度更快)
  • Another “minor” feature of using .npy files is the reduced storage the file occupies. In this case it’s more than a 50% reduction in size. This can wary a lot though but in general the .npy files are more storage friendly.(占用内存更少)

四、读取和保存

(1)加载npy文件,并将npy文件写入一个txt文件
import numpy as np
test=np.load('./bvlc_alexnet.npy',encoding = "latin1")  #加载文件
doc = open('1.txt', 'a')  #打开一个存储文件,并依次写入
print(test, file=doc)  #将打印内容写入文件中
(2)保存npy文件
#显示字典
print(train_dataset.class_to_idx)
idx_to_labels = {y:x for x,y in train_dataset.class_to_idx.items()}#获取dict:train_dataset.class_to_idx的keys和values
print(idx_to_labels)

np.save('idx_to_labels.npy',idx_to_labels)
np.save('labels_to index.npy',train_dataset.class_to_idx)
(3)结构解析
import numpy as np
from numpy import *  #使用numpy的属性且不需要在前面加上numpy
import tensorflow as tf
 
#模型文件(.npy)部分内容如下:由一个字典组成,字典中的每一个键对应一层网络模型参数。(包括权重w和偏置b)
a = {'conv1':[array([[1,2],[3,4]],dtype=float32),array([5,6],dtype=float32)],'conv2':[array([[1,2],[3,4]],dtype=float32),array([5,6],dtype=float32)]}
 
conv1_w = a['conv1'][0]
conv1_b = a['conv1'][1]
conv2_w = a['conv2'][0]
conv2_b = a['conv2'][1]
 
print(conv1_w)
print(tf.Variable(conv1_w))
print(conv1_b)
print(tf.Variable(conv1_b))
  • 20
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

verse_armour

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值