解讀什么是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.(占用內(nèi)存更少)
四、讀取和保存
(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) #將打印內(nèi)容寫入文件中
(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)結(jié)構(gòu)解析
import numpy as np
from numpy import * #使用numpy的屬性且不需要在前面加上numpy
import tensorflow as tf
#模型文件(.npy)部分內(nèi)容如下:由一個字典組成,字典中的每一個鍵對應(yīng)一層網(wǎng)絡(luò)模型參數(shù)。(包括權(quán)重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))
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類
這篇文章主要為大家詳細(xì)介紹了Python如何使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考下2024-02-02
Python django框架開發(fā)發(fā)布會簽到系統(tǒng)(web開發(fā))
這篇文章主要介紹了Python django框架開發(fā)發(fā)布會簽到系統(tǒng)(web開發(fā)),本文通過實例代碼效果展示截圖的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

