我目前正在修改this要點以使用numpy .npz文件保存神經網絡的狀態。有問題的代碼使用變量:Python numpy在嘗試讀取未壓縮的npz文件時引發BadZipfile錯誤
Wxh = np.random.randn(hidden_size, vocab_size) * 0.01 # input to hidden
Whh = np.random.randn(hidden_size, hidden_size) * 0.01 # hidden to hidden
Why = np.random.randn(vocab_size, hidden_size) * 0.01 # hidden to output
bh = np.zeros((hidden_size, 1)) # hidden bias
by = np.zeros((vocab_size, 1)) # output bias
它們當然在運行網絡時發生了變化。現在,後救他們通過:
memFile = open(memoryFileName, "w")
np.savez(memFile, Wxh, Whh, Why, bh, by)
memFile.close()
我試圖測試通過打開他們:
Traceback (most recent call last):
File "D:/Python27/projects/poemGen/charRNN/char_rnn.py", line 58, in <module>
testDict.update(loaded)
File "D:\Python27\lib\site-packages\numpy\lib\npyio.py", line 224, in __getitem__
pickle_kwargs=self.pickle_kwargs)
File "D:\Python27\lib\site-packages\numpy\lib\format.py", line 664, in read_array
data = _read_bytes(fp, read_size, "array data")
File "D:\Python27\lib\site-packages\numpy\lib\format.py", line 803, in _read_bytes
r = fp.read(size - len(data))
File "D:\Python27\lib\zipfile.py", line 632, in read
data = self.read1(n - len(buf))
File "D:\Python27\lib\zipfile.py", line 672, in read1
self._update_crc(data, eof=(self._compress_left==0))
File "D:\Python27\lib\zipfile.py", line 647, in _update_crc
raise BadZipfile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipfile: Bad CRC-32 for file 'arr_1.npy'
據我瞭解,它:
loaded = np.load(getfile_local(memoryFileName))
Wxh, Whh, Why, bh, by = 0,0,0,0,0
varList = [Wxh, Whh, Why, bh, by]
for index, name in enumerate(loaded.files):
print loaded[name]
運行此提出以下異常
似乎這是numpy或zipfile中的問題,但我很樂意聽到這是我的錯誤。^^
你爲什麼不給'savez'文件名?爲什麼先打開它? – hpaulj