AttributeError: 'NpzFile' object has no attribute 'zip', numpy.load(path) 过程中报错

载入已有模型参数过程中,出现报错,代码及错误如下:
def load_params(path, params):
load parameters
pp = numpy.load(path)
for kk, vv in params.items():
	if kk not in pp:
		warnings.warn('%s is not in the archive'%kk)
		continue
	params[kk] = pp[kk]
return params
报错如下:错误提示在找这个错误的过程中,遇到一种解释是:

just remove your database downloaded at ur root folder. and run again. make sure ur connection, because this problem caused by the database you downloaded is failed.

原答案戳这里
这正是我们这里遇到的问题,当我们在下载模型的过程中出现异常,所下载的额模型是破损的,按照路径可以找到文件,但是无法正常载入。如果你是导入数据出现此错误,请移步补充。
解决办法就是删除破损文件,重新下载该文件,即可正常运行。

  • 补充
    另一种报这个错误的情况:
    keras 实践中需要先导入数据集,如:
from keras.datasets import mnist

运行时可能也会出现 ‘NpzFile’ object has no attribute ‘zip’ 错误。导致这种情况的原因是上一次的数据集导入中途发生了中断,程序无法在这里恢复。
解决办法为:将下载失败的缓存文件删除,重新执行程序。
在Winodws下,这个文件夹为“C:\Users\Administrator.keras\datasets”
原文:https://blog.csdn.net/Kexiii/article/details/77099798

import sys import re import jieba import codecs import gensim import numpy as np import pandas as pd def segment(doc: str): stop_words = pd.read_csv('data/stopwords.txt', index_col=False, quoting=3, names=['stopword'], sep='\n', encoding='utf-8') stop_words = list(stop_words.stopword) reg_html = re.compile(r'<[^>]+>', re.S) # 去掉html标签数字等 doc = reg_html.sub('', doc) doc = re.sub('[0-9]', '', doc) doc = re.sub('\s', '', doc) word_list = list(jieba.cut(doc)) out_str = '' for word in word_list: if word not in stop_words: out_str += word out_str += ' ' segments = out_str.split(sep=' ') return segments def doc2vec(file_name, model, doc_id): start_alpha = 0.01 infer_epoch = 1000 doc = segment(codecs.open(file_name, 'r', 'utf-8').read()) return model.infer_vector(doc, alpha=start_alpha, steps=infer_epoch) # 计算两个向量余弦值 def similarity(a_vect, b_vect): dot_val = 0.0 a_norm = 0.0 b_norm = 0.0 cos = None for a, b in zip(a_vect, b_vect): dot_val += a * b a_norm += a ** 2 b_norm += b ** 2 if a_norm == 0.0 or b_norm == 0.0: cos = -1 else: cos = dot_val / ((a_norm * b_norm) ** 0.5) return cos def test_model(file1, file2): print('导入模型') model_path = 'tmp/zhwk_news.doc2vec' model = gensim.models.Doc2Vec.load(model_path) vect1 = doc2vec(file1, model, doc_id=0) # 转成句子向量 vect2 = doc2vec(file2, model, doc_id=1) print(vect1.nbytes) # 查看向量大小 print(vect2.nbytes) cos = similarity(vect1, vect2) print('相似度:%0.2f%%' % (cos * 100)) if __name__ == '__main__': file1 = 'data/corpus_test/t1.txt' file2 = 'data/corpus_test/t2.txt' test_model(file1, file2) 报错AttributeError: 'Doc2Vec' object has no attribute 'dv'怎么解决
06-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值