Пытаюсь выполнить следующий код и получаю ошибку:
import torch.nn.functional as F
class TF(nn.Module):
def __init__(self):
super(TF, self).__init__()
self.flatten = nn.Flatten()
self.conv1 = nn.Conv1d(28, 56, 3)
self.conv2 = nn.Conv1d(56, 112, 3)
self.pool = nn.MaxPool1d(2, 2)
self.fc1 = nn.Linear(256, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)
self.soft=nn.LogSoftmax(dim=0)
def forward(self, x):
x=x.cuda()
x = x.view(-1, 28,28)
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.flatten(x)
x = self.fc1
x = self.fc2
x = self.fc3
x = F.softmax(x)
return x
tf=TF().to('cuda')
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(tf.parameters())
```for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data[0], data[1]
optimizer.zero_grad()
outputs = tf(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
> AttributeError Traceback (most recent call last)
><ipython-input-535-b76e0de90a8b> in <module>
> 10 optimizer.zero_grad()
> 11
>---> 12 outputs = tf(inputs)
> 13 print(outputs)
> 14 loss = F.nll_loss(outputs, labels)
>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, >*input, **kwargs)
> 887 result = self._slow_forward(*input, **kwargs)
> 888 else:
>--> 889 result = self.forward(*input, **kwargs)
> 890 for hook in itertools.chain(
> 891 _global_forward_hooks.values(),
><ipython-input-530-89adabe7361f> in forward(self, x)
> 24 x = self.fc2
> 25 x = self.fc3
>---> 26 x = F.log_softmax(x, dim=1)
> 27 return x
>~\anaconda3\lib\site-packages\torch\nn\functional.py in log_softmax(input, >dim, _stacklevel, dtype)
> 1670 dim = _get_softmax_dim("log_softmax", input.dim(), >_stacklevel)
> 1671 if dtype is None:
>-> 1672 ret = input.log_softmax(dim)
> 1673 else:
> 1674 ret = input.log_softmax(dim, dtype=dtype)
>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in __getattr__(self, >name)
> 945 if name in modules:
> 946 return modules[name]
>--> 947 raise AttributeError("'{}' object has no attribute >'{}'".format(
> 948 type(self).__name__, name))
> 949
>AttributeError: 'Linear' object has no attribute 'log_softmax'