m0_59683341 2023-12-11 09:10 采纳率: 0%
浏览 10
已结题

我做一个对中文文本情感分析的项目 我用了CNN,和keras框架 报的错误我一直处理不好

我做一个对中文文本情感分析的项目
我用了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)
  • 写回答

6条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2023-12-11 09:12
    关注

    由人工智能和答主提供,可以参考如下,如果回答的不正确,及时评论区回复,我追加回答,谢谢。


    从报错信息来看,问题出在无法打开模型文件。请检查以下几点:

    1. 确保模型文件(lstm-30.hdf5)确实存在于saved_models文件夹中。
    2. 确保模型文件的路径正确。在这个例子中,模型文件的路径是C:\Users\28219\Downloads\Compressed\202312107U5OA0u2\sentimentanalysis_lstm\saved_models\lstm-30.hdf5。请确保这个路径与实际文件所在的路径一致。

    如果以上两点都没有问题,那么可能是文件权限问题。请尝试以管理员身份运行Python脚本,或者更改模型文件的权限。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月11日
  • 创建了问题 12月11日

悬赏问题

  • ¥15 未生成产品编码机器人还连不上 wifi
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal