解讀什么是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文件寫(xiě)入一個(gè)txt文件
import numpy as np
test=np.load('./bvlc_alexnet.npy',encoding = "latin1") #加載文件
doc = open('1.txt', 'a') #打開(kāi)一個(gè)存儲(chǔ)文件,并依次寫(xiě)入
print(test, file=doc) #將打印內(nèi)容寫(xiě)入文件中
(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)容如下:由一個(gè)字典組成,字典中的每一個(gè)鍵對(duì)應(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 寫(xiě)入訓(xùn)練日志文件并控制臺(tái)輸出解析
這篇文章主要介紹了Python 寫(xiě)入訓(xùn)練日志文件并控制臺(tái)輸出解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Django項(xiàng)目使用CircleCI的方法示例
這篇文章主要介紹了Django項(xiàng)目使用CircleCI的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python使用sqlalchemy實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的幫助類
這篇文章主要為大家詳細(xì)介紹了Python如何使用sqlalchemy實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)的幫助類,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考下2024-02-02
Python django框架開(kāi)發(fā)發(fā)布會(huì)簽到系統(tǒng)(web開(kāi)發(fā))
這篇文章主要介紹了Python django框架開(kāi)發(fā)發(fā)布會(huì)簽到系統(tǒng)(web開(kāi)發(fā)),本文通過(guò)實(shí)例代碼效果展示截圖的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02

