跑官方情感分析案例,出现错误
守心水瓶 发布于2020-05-17 14:07 浏览:167 回复:9
0
收藏

 

出现下上错误,不清楚啥原因,都是源代码,没做任何改动呀

收藏
点赞
0
个赞
共9条回复 最后由鹿鼎记肯定回复于2020-05-18 20:43
#10鹿鼎记肯定回复于2020-05-18 20:43:40
#9 守心水瓶回复
好的,非常感谢

将文档中的最后两都段代码改成一下形式即可,其中new_dict变量是我加上去的,因为原来的字典键类型为bytes,预测结果不准确

import paddle
import paddle.fluid as fluid
import numpy as np

place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)

reviews_str = [
    'read the book forget the movie', 'this is a great movie', 'this is very bad'
]
reviews = [c.split() for c in reviews_str]

word_dict = paddle.dataset.imdb.word_dict()

UNK = word_dict['']

# 修改字典bytes类的键
new_dict = {}
for k, v in word_dict.items():
    if isinstance(k, bytes):
        k = k.decode()
        new_dict[k] = v
    else:
        pass

def reader():
    for sample in reviews:
        sample_data = [new_dict.get(word, UNK) for word in sample]
        sample_data = np.array(sample_data, dtype="int64").reshape(1, -1)
        print(sample_data)
        yield sample_data


params_dirname = "./understand_sentiment_conv.inference.model"
[inferencer, feed_target_names, fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)

batch_reader = fluid.io.batch(reader=reader, batch_size=128)
feeder = fluid.DataFeeder(place=place, feed_list=feed_target_names, program=inferencer)

assert feed_target_names[0] == "words"

for data in batch_reader():
    results = exe.run(inferencer,
                      feed=feeder.feed(data),
                      fetch_list=fetch_targets,
                      return_numpy=False)
    np_data = np.array(results[0])
    for i, r in enumerate(np_data):
        print("Predict probability of ", r[0], " to be positive and ", r[1],
              " to be negative for review \'", reviews_str[i], "\'")
0
#9守心水瓶回复于2020-05-18 09:16:58

好的,非常感谢

0
#8鹿鼎记肯定回复于2020-05-17 23:02:33

确实是个问题,我帮你提了issue,可以的话跟进一下吧

https://github.com/PaddlePaddle/Paddle/issues/24605

0
#7守心水瓶回复于2020-05-17 19:18:00

CPU训练

处理器:
Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
1.99 GHz

0
#6鹿鼎记肯定回复于2020-05-17 18:56:44

不好意思,没看清楚你是在本地运行的,能否提供一下版本信息?

0
#5鹿鼎记肯定回复于2020-05-17 18:55:08

该案例要求cuda为8,而AI Studio提供的cuda为9

0
#4守心水瓶回复于2020-05-17 18:39:24

你可以试着跑一下代码

0
#3守心水瓶回复于2020-05-17 18:38:26

就是从头到尾运行的

0
#2xiaohongyd回复于2020-05-17 17:39:16

是在NoteBook运行的吧,前面几段代码运行了吗

0
TOP
切换版块