我做一个对中文文本情感分析的项目
我用了CNN,和keras框架
报的错误我一直处理不好啊
下面是我的代码:
import os
import pickle
import numpy as np
from keras.models import load_model
import preprocess as pp
# Parameters
dict_size = 10000
max_sequence_len = 30 # 序列对齐的长度
batch_size = 64
stopwords_name = 'cn_stopwords_punctuations.csv'
model_name = f'lstm-{max_sequence_len}.hdf5'
tokenizer_name = f'tokenizer-{dict_size}.pickle'
test_name = 'test.txt'
outputs_name = 'result.txt'
# Paths
init_path = os.getcwd()
data_dir = os.path.join(init_path, 'datasets')
stopwords_path = os.path.join(data_dir, stopwords_name)
test_path = os.path.join(data_dir, test_name)
outputs_path = os.path.join(data_dir, outputs_name)
save_dir = os.path.join(init_path, 'saved_models')
model_path = os.path.join(save_dir, model_name)
tokenizer_path = os.path.join(save_dir, tokenizer_name)
# load data
with open(test_path, encoding='utf-8') as f:
texts = f.readlines()
texts = [x.strip() for x in texts]
# tokenize
stopwords = pp.get_stopwords(stopwords_path)
texts = pp.tokenize_texts(texts, stopwords)
# to sequences
with open(tokenizer_path, 'rb') as t:
tokenizer = pickle.load(t)
x, _ = pp.texts_to_sequence_vectors(texts, max_sequence_len,
tokenizer=tokenizer)
# load model
model = load_model(model_path, compile=True)
# predict
y_pred = model.predict(x,
batch_size=batch_size,
verbose=1)
y_pred = ['正面' if x[0] > 0.5 else '负面' for x in y_pred.tolist()]
# output
y_pred = [x + '\n' for x in y_pred]
with open(outputs_path, 'w') as f:
f.writelines(y_pred)
下面是报错信息:
Traceback (most recent call last):
File "C:/Users/28219/Downloads/Compressed/202312107U5OA0u2/sentimentanalysis_lstm/predict.py", line 55, in <module>
model = load_model(model_path, compile=True)
File "D:\ProgramData\Anaconda3\envs\bishe\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "D:\ProgramData\Anaconda3\envs\bishe\lib\site-packages\keras\engine\saving.py", line 583, in load_model
with H5Dict(filepath, mode='r') as h5dict:
File "D:\ProgramData\Anaconda3\envs\bishe\lib\site-packages\keras\utils\io_utils.py", line 191, in __init__
self.data = h5py.File(path, mode=mode)
File "D:\ProgramData\Anaconda3\envs\bishe\lib\site-packages\h5py\_hl\files.py", line 427, in __init__
swmr=swmr)
File "D:\ProgramData\Anaconda3\envs\bishe\lib\site-packages\h5py\_hl\files.py", line 190, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 96, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = 'C:\Users\28219\Downloads\Compressed\202312107U5OA0u2\sentimentanalysis_lstm\saved_models\lstm-30.hdf5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)