国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

成功解決 AttributeError: ‘Field‘ object has no attribute ‘vocab‘

這篇具有很好參考價(jià)值的文章主要介紹了成功解決 AttributeError: ‘Field‘ object has no attribute ‘vocab‘。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

最近復(fù)現(xiàn)代碼過程中,需要用到 torchtext.data 中的 Field 類。本篇博客記錄使用過程中的問題及解決方式。

  1. 注意 torchtext 版本不宜過新

在較新版本的 torchtext.data 里面并沒有 Field 方法,這一點(diǎn)需要注意。

啟示:在復(fù)現(xiàn)別人代碼時(shí),應(yīng)同時(shí)復(fù)制他們使用環(huán)境的版本信息。

  1. 運(yùn)行下述代碼:
from torchtext.data import Field

SRC = Field(tokenize = tokenize_en, 
            init_token = '<sos>', 
            eos_token = '<eos>',
            fix_length = max_length,
            lower = True, 
            batch_first = True,
            sequential=True)

TRG = Field(tokenize = tokenize_en, 
            init_token = '<sos>', 
            eos_token = '<eos>', 
            fix_length = max_length,
            lower = True, 
            batch_first = True,
            sequential=True)

print(SRC.vocab.stoi["<sos>"])
print(TRG.vocab.stoi["<sos>"])

報(bào)錯(cuò)信息:

print(SRC.vocab.stoi["<sos>"])  # 2
AttributeError: 'Field' object has no attribute 'vocab'

于是查看 Field 類的定義,尋找和詞表建立相關(guān)的函數(shù),發(fā)現(xiàn)其 build_vocab() 函數(shù)中有建立詞表的操作, build_vocab() 函數(shù)定義如下:

class Field(RawField):
	
	...
    
    def build_vocab(self, *args, **kwargs):
        """Construct the Vocab object for this field from one or more datasets.

        Arguments:
            Positional arguments: Dataset objects or other iterable data
                sources from which to construct the Vocab object that
                represents the set of possible values for this field. If
                a Dataset object is provided, all columns corresponding
                to this field are used; individual columns can also be
                provided directly.
            Remaining keyword arguments: Passed to the constructor of Vocab.
        """
        counter = Counter()
        sources = []
        for arg in args:
            if isinstance(arg, Dataset):
                sources += [getattr(arg, name) for name, field in
                            arg.fields.items() if field is self]
            else:
                sources.append(arg)
        for data in sources:
            for x in data:
                if not self.sequential:
                    x = [x]
                try:
                    counter.update(x)
                except TypeError:
                    counter.update(chain.from_iterable(x))
        specials = list(OrderedDict.fromkeys(
            tok for tok in [self.unk_token, self.pad_token, self.init_token,
                            self.eos_token] + kwargs.pop('specials', [])
            if tok is not None))
        self.vocab = self.vocab_cls(counter, specials=specials, **kwargs)
	
	...

解決方式:在程序中 Field 定義后添加 SRC.build_vocab()TRG.build_vocab(),程序變成:

SRC.build_vocab()
TRG.build_vocab()

print(SRC.vocab.stoi["<sos>"])  # 輸出結(jié)果:2
print(TRG.vocab.stoi["<sos>"])  # 輸出結(jié)果:2

至此,程序就會(huì)順利執(zhí)行啦!


參考資料文章來源地址http://www.zghlxwxcb.cn/news/detail-539560.html

  1. python - BucketIterator 拋出 ‘Field’ 對(duì)象沒有屬性 ‘vocab’ - IT工具網(wǎng) (coder.work)
  2. ImportError: cannot import name ‘Field‘ from ‘torchtext.data‘, No module named “l(fā)egacy“_no module named 'torchtext.legacy_御用廚師的博客-CSDN博客

到了這里,關(guān)于成功解決 AttributeError: ‘Field‘ object has no attribute ‘vocab‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 解決AttributeError: ‘DataFrame‘ object has no attribute ‘a(chǎn)ppend‘

    解決AttributeError: ‘DataFrame‘ object has no attribute ‘a(chǎn)ppend‘

    自然語言處理執(zhí)行 train_data = pd.DataFrame()... contents = pd.DataFrame(content)... 再執(zhí)行train_data = train_data.append(contents[:400])出現(xiàn)錯(cuò)誤AttributeError: \\\'DataFrame\\\' object has no attribute \\\'append\\\' 估計(jì)是pandas版本升級(jí)棄用了 老版本\\\'DataFrame\\\'的append方法。由于pandas與眾多的第三方軟件包捆綁,一般不宜輕易

    2024年02月11日
    瀏覽(24)
  • 解決AttributeError: ‘Namespace‘ object has no attribute ‘a(chǎn)rch‘

    在運(yùn)行ACmix-ResNet模型時(shí)出現(xiàn)問題 很簡(jiǎn)單的一個(gè)錯(cuò)誤,沒有添加參數(shù) 使用parser添加相應(yīng)參數(shù)即可

    2024年02月08日
    瀏覽(24)
  • 已解決AttributeError: ‘list‘ object has no attribute ‘text‘

    已解決AttributeError: ‘list‘ object has no attribute ‘text‘

    已解決AttributeError: ‘list’ object has no attribute ‘text’ 粉絲群里面的一個(gè)小伙伴遇到問題跑來私信我,想用selenium操作瀏覽器自動(dòng)化,但是發(fā)生了報(bào)錯(cuò)(當(dāng)時(shí)他心里瞬間涼了一大截,跑來找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個(gè)bug不會(huì)解決

    2023年04月17日
    瀏覽(110)
  • 已解決AttributeError: ‘str‘ object has no attribute ‘read‘

    已解決AttributeError: ‘str‘ object has no attribute ‘read‘

    已解決(json.load()讀取json文件報(bào)錯(cuò))AttributeError: ‘str‘ object has no attribute ‘read‘ 粉絲群里面的一個(gè)粉絲在用Python讀取json文件的時(shí)候,出現(xiàn)了報(bào)錯(cuò)(跑來找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個(gè)bug不會(huì)解決的小伙伴),報(bào)錯(cuò)信息和代碼

    2024年02月12日
    瀏覽(41)
  • 成功解決AttributeError: module ‘numpy‘ has no attribute ‘float‘.

    AttributeError: module ‘numpy’ has no attribute ‘float’. np.float was a deprecated alias for the builtin float . To avoid this error in existing code, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here. The aliases was originally deprecated in NumPy 1.20; for

    2024年02月16日
    瀏覽(25)
  • AttributeError: ‘NoneType‘ object has no attribute ‘split‘的解決辦法

    AttributeError: ‘NoneType‘ object has no attribute ‘split‘的解決辦法

    在用KMeans算法訓(xùn)練數(shù)據(jù)的時(shí)候,報(bào)錯(cuò)如下: 經(jīng)過各種途徑的查詢,有些回答建議嘗試對(duì)sklearn、numpy修改版本。經(jīng)過驗(yàn)證,sklearn與numpy版本與建議者所要修改的版本一致,故沒有采納。 經(jīng)過自己的仔細(xì)觀察,因?yàn)樵谑褂肒Means算法訓(xùn)練數(shù)據(jù)代碼之前,只有一行代碼,那就是 故嘗

    2024年02月13日
    瀏覽(26)
  • AttributeError:‘CartPoleEnv‘ object has no attribute ‘seed‘解決方案

    在嘗試運(yùn)行g(shù)ym的classic?control模塊中的Cart Pole的相關(guān)代碼時(shí),想用隨機(jī)種子重置一下環(huán)境,結(jié)果不停的報(bào)AttributeError:\\\'CartPoleEnv\\\' object has no attribute \\\'seed\\\'的錯(cuò),查看gym的官方文檔后也沒有得出什么結(jié)果。后來,意外發(fā)現(xiàn)了在另外一臺(tái)機(jī)器上運(yùn)行該代碼的警告信息: gym/core.py:256:

    2024年02月15日
    瀏覽(18)
  • AttributeError: ‘bytes‘ object has no attribute ‘encode‘異常解決方案

    AttributeError: ‘bytes‘ object has no attribute ‘encode‘異常解決方案

    AttributeError:?\\\'bytes\\\' object has no attribute \\\'encode\\\'是:“字節(jié)”對(duì)象沒有屬性的編碼的意思。 很明顯,是編碼格式的問題,例如:已經(jīng)是byte格式的字符串類型,二次進(jìn)行encode的時(shí)候就會(huì)出現(xiàn)這個(gè)bug,示例如下: 異常的報(bào)錯(cuò)效果如下: 其實(shí)異常說的是比較明顯的,屬性誤差:【At

    2024年02月11日
    瀏覽(20)
  • 已解決AttributeError: ‘str‘ object has no attribute ‘decode‘方案二

    已解決AttributeError: ‘str‘ object has no attribute ‘decode‘解決方法異常的正確解決方法,親測(cè)有效?。?! AttributeError: ‘str‘ object has no attribute ‘decode‘ AttributeError: ‘str’ object has no attribute \\\'decode’錯(cuò)誤通常發(fā)生在Python 3版本中,當(dāng)嘗試對(duì)字符串對(duì)象使用decode()方法時(shí)。 下滑查

    2024年02月07日
    瀏覽(21)
  • 解決出現(xiàn)的AttributeError: ‘dict‘ object has no attribute ‘encode‘錯(cuò)誤

    這個(gè)錯(cuò)誤通常表示您正在嘗試對(duì)字典類型的對(duì)象使用字符串編碼方法。但是字典類型的對(duì)象沒有編碼屬性。 通常可能需要檢查代碼中哪些部分試圖將字典轉(zhuǎn)換為字符串并應(yīng)用編碼。例如,在以下代碼中: 這個(gè)錯(cuò)誤就會(huì)出現(xiàn),因?yàn)樽值漕愋偷膶?duì)象沒有encode() 方法 解決方法是將字

    2024年02月11日
    瀏覽(42)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包