English 中文(简体)
在PyTorch示范类中使用吨数的异常错误
原标题:Unusual Error When Using nn.Sequential in Model Class in PyTorch

只是在这里建立一个简单的神经网络,以尝试nn。 由于某种原因,必然会发现这一错误

该守则是:

# Create Model

class Net(nn.Module):
    def __init__(self):
        super().__init__()

        # Define Network

        self.stack = nn.Sequential(
            nn.Linear(in_features=3, out_features=8),

            nn.ReLU(),

            nn.Linear(in_features=8, out_features=8),

            nn.ReLU(),

            nn.Linear(in_features=8, out_features=1),

            nn.Sigmoid(),
        )

    def forward(self, x):
        # Define Forward Pass

        return self.stack(x)


# Instance Of Model

model = Net(X_train[:5])

否则:

TypeError                                 Traceback (most recent call last)
<ipython-input-32-f947c74336f3> in <cell line: 31>()
     29 # Instance Of Model
     30 
---> 31 model = Net(X_train[0])

TypeError: Net.__init__() takes 1 positional argument but 2 were given

在这里,我试图测试一下,如果我的模式通过输入我培训数据的头5个数值,而不是在0到1之间找到5个数字,前[0.1、0.2、0.43、0.67、.78],我就犯了上述错误。

问题回答

你正在向示范建筑商转达你的意见。 您在使用<代码>forward方法之前需要立即采用该模型。

model = Net()
out = model(torch.randn(8, 3))

你之所以重新发现这一错误,是因为你将投入帐篷“到早期”。 在模型标的形成和初步化时,使用<代码>Net(>)为“模拟造物”,但你的模型还没有成像! 模型设定之后的示范执行权是:

Net(X_train[:5]

但通常我们采取两个步骤:

# Create model
model = Net()
# Now you can call model multiple times...
model(X_train[:5])




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签