Python - AI - MAX78000 - Expected target size [90, 2], got [90]

 クラウディア
1. 概要
2. 現象
3. 対処

1. 概要

 前ページと同様の状況で・・・。  本ページは、下記のサイトを参考にさせていただきました。
【PyTorch view】Tensor 配列の次元を変換する torch.view

2. 現象


ai8x-training/.venv/lib/site-packages/torch/nn/functional.py
 の、下記の「return」の箇所でこけます。

    if size_average is not None or reduce is not None:
        reduction = _Reduction.legacy_get_string(size_average, reduce)
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
 下記のようなエラーになります。

RuntimeError: Expected target size [90, 2], got [90]
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> パス/ai8x-training/.venv/lib/site-packages/torch/nn/functional.py(3086)cross_entropy()
-> return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)

3. 対処

 ここで、「input」「target」が重要人物になっています。  「weight」は、「None」になっていますので、はずしていいでしょう。  デバッガで、状況を見ると。

p input.shape
torch.Size([90, 24, 2])

p target.shape
torch.Size([90])
 てな感じ。  見本にした「cats_vs_dogs」で、同じ個所を確認してみると。

p input.shape
torch.Size([53, 2])

p target.shape
torch.Size([53])
 となっております。  思うにエラーになるのは、「input」の次元が、1つ多いように思われます。  「cats_vs_dogs」で使用しているモジュールの記述を読んでみます。

ai8x-training/models/ai85net-cd.py
 ちゅうモジュールなのですが、おおまかに、重要なところを抜き出してみますと。

from torch import nn
import ai8x

class AI85CatsDogsNet(nn.Module):

    def __init__(self, num_classes=2, num_channels=3, dimensions=(128, 128),
                 fc_inputs=16, bias=False, **kwargs):
        super().__init__()

        assert dimensions[0] == dimensions[1]  # Only square supported

        dim = dimensions[0]
        self.conv1 = ai8x.FusedConv2dReLU(num_channels, 16, 3,
                                          padding=1, bias=bias, **kwargs)

        # padding 1 -> no change in dimensions -> 16x128x128
        pad = 2 if dim == 28 else 1
        self.conv2 = ai8x.FusedMaxPoolConv2dReLU(16, 32, 3, pool_size=2, pool_stride=2,
                                                 padding=pad, bias=bias, **kwargs)
        dim //= 2  # pooling, padding 0 -> 32x64x64

		・・・	略	・・・

    def forward(self, x):  # pylint: disable=arguments-differ
        """Forward prop"""
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.conv5(x)
        x = self.conv6(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

def ai85cdnet(pretrained=False, **kwargs):
    assert not pretrained
    return AI85CatsDogsNet(**kwargs)

models = [
    {
        'name': 'ai85cdnet',
        'min_input': 1,
        'dim': 2,
    },
]
 これが、下記のようなエラーになります。

    def forward(self, x):  # pylint: disable=arguments-differ
        """Forward prop"""
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.conv5(x)
        x = self.conv6(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x
 の「x = x.view(x.size(0), -1)」の行に着目したです。

torch.view
 というメソッドは、次元を変えるようです。  まだ理解できていませんが・・・。

        x = x.view(x.size(0), -1)
 ちゅうのは、同じサイズのまま、次元を減らしているのではないか?  「mat1 and mat2 shapes cannot be multiplied」のときのモジュールをいじります。

ai8x-training/models/xxx.py
 の。

from torch import nn
import ai8x

class xxx(nn.Module):

	・・・ 略  ・・・

  def forward(self, x):

    x = self.l1(x)
		・・・ 略  ・・・

    return x

models = [
	{
		'name': 'xxx',
		'min_input': 1,
		'dim': 2,
	},
]
 「forward」メソッドの、「return」の前に。

    x = x.view(x.size(0), -1)
 と1行をいれたら・・・。  先へ進むようになりました。
AbemaTV 無料体験