解讀什么是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進(jìn)度條實(shí)時顯示處理進(jìn)度的示例代碼

Python位置參數(shù)與關(guān)鍵字參數(shù)的區(qū)別

Python灰度變換中伽馬變換分析實(shí)現(xiàn)

Django項目中用JS實(shí)現(xiàn)加載子頁面并傳值的方法

softmax及python實(shí)現(xiàn)過程解析

Python繪制圣誕樹+落葉+雪花+背景音樂+浪漫彈窗?五合一版圣誕樹

python使用 cx_Oracle 模塊進(jìn)行查詢操作示例