为什么在jupyter-notebook中这是错误的,而在终端中却是正确的?

时间:2019-06-04 14:52:54

标签: jupyter-notebook pytorch

当我在jupyter中运行这段代码时,它没有正确运行。错误是:

TypeError                                 Traceback (most recent call
last) <ipython-input-16-76d9b57527d4> in <module>
     14 embed_layer = emb()
     15 
---> 16 inp = torch.tensor([ 1, 2, 3, 4, 5, 6 ])
     17 
     18 out = embed_layer( inp )

TypeError: 'module' object is not callable

但是,当我在终端中运行它时,我可以正确运行它。 我正在尝试训练一个模型来生成单词嵌入。

import torch
import torch.nn as nn

class emb( nn.Module ):

    def __init__( self ):
        super( emb, self ).__init__()
        self.embed_layer = nn.Embedding(8,3)

    def forward( self, inp ):
        out = self.embed_layer( inp )
        return out

embed_layer = emb()

inp = torch.tensor([ 1, 2, 3, 4, 5, 6 ])

out = embed_layer( inp )
print(out)

0 个答案:

没有答案