Code Monkey home page Code Monkey logo

Comments (6)

Aurelius84 avatar Aurelius84 commented on June 14, 2024

请问下,使用的训练代码是什么时候的版本?
从报错来看,是输入words的shape不匹配,可能尝试如下方法:

  1. 定义wordsfluid.data输入为[-1],但实际训练输入的shape为[-1, 1],可以尝试对输入的words调用fluid.layers.squeeze(words, -1)移除最后多余的shape.

from book.

Er1-x avatar Er1-x commented on June 14, 2024

用的就是这个paddlepaddle/book中的版本

from book.

Er1-x avatar Er1-x commented on June 14, 2024

def infer(use_cuda, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
word_dict = paddle.dataset.imdb.word_dict()

exe = fluid.Executor(place)

inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
    # Use fluid.io.load_inference_model to obtain the inference program desc,
    # the feed_target_names (the names of variables that will be feeded
    # data using feed operators), and the fetch_targets (variables that
    # we want to obtain data from using fetch operators).
    [inferencer, feed_target_names,
     fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)

    # Setup input by creating LoDTensor to represent sequence of words.
    # Here each word is the basic element of the LoDTensor and the shape of
    # each word (base_shape) should be [1] since it is simply an index to
    # look up for the corresponding word vector.
    # Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]],
    # which has only one lod level. Then the created LoDTensor will have only
    # one higher level structure (sequence of words, or sentence) than the basic
    # element (word). Hence the LoDTensor will hold data for three sentences of
    # length 3, 4 and 2, respectively.
    # Note that lod info should be a list of lists.
    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]

    UNK = word_dict['<unk>']
    lod = []
    for c in reviews:
        lod.append([np.int64(word_dict.get(words, UNK)) for words in c])

    base_shape = [[len(c) for c in lod]]

    tensor_words = fluid.create_lod_tensor(lod, base_shape, place)
    assert feed_target_names[0] == "words"
    results = exe.run(
        inferencer,
        feed={feed_target_names[0]: tensor_words},
        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], "\'")

这是出现问题的函数,问题出现在 results = exe.run()中

from book.

lfchener avatar lfchener commented on June 14, 2024

book 后续可能不会再维护了
最新源码可以参考:
https://github.com/PaddlePaddle/FluidDoc/tree/release/1.8/doc/fluid/user_guides/nlp_case/understand_sentiment#application-model

from book.

Er1-x avatar Er1-x commented on June 14, 2024

谢谢,新版本代码可以正常运行,

from book.

Aurelius84 avatar Aurelius84 commented on June 14, 2024

谢谢,新版本代码可以正常运行,

@er11c 请问是否已经解决了您的问题?如果问题已解决,辛苦close下哈~~

from book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.