Unsupported operand types(s) for %: 'tuple' and 'int'

Hello everybody, I have a problem when I want to train my data using cycle GAN. Following is the list of my code:
input_shape = (opt.channels, opt.img_height, opt.img_width)

print(type(input_shape))

G_AB = Generator(input_shape)
G_BA = Generator(input_shape)
D_A = Discriminator(input_shape)
D_B = Discriminator(input_shape)

And following is my error when I run my code

Traceback (most recent call last):
File “/home/riza/Documents/Cycle_GAN/Cycle_GAN.py”, line 61, in
G_AB = Generator(input_shape)
File “/home/riza/Documents/Cycle_GAN/models.py”, line 44, in init
nn.Conv2d(input, output, 7),
File “/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py”, line 332, in init
False, _pair(0), groups, bias, padding_mode)
File “/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py”, line 22, in init
if in_channels % groups != 0:
TypeError: unsupported operand type(s) for %: ‘tuple’ and ‘int’

Please help to solve my problem. Thank you

Hi,

I think the problem is that instead of the number of input channels, you give to the convolution constructor a tuple containing the shape of the input. It should be a simple number.

I also have try to use a simple number, but it is still error

Could you give a small code sample I can run that reproduces the error please?

Oke, this is my listing program
This is my small code of Generator Network
class Generator(nn.Module):
def init(self, input, residual_block, output=64):
super(Generator, self).init()

    model = [
        nn.ReflectionPad2d(1),
        nn.Conv2d(input, output, 7),
        nn.InstanceNorm2d(output),
        nn.ReLU(True),
    ]

This is my small code of Discriminator Network
class Discriminator(nn.Module):
def init(self, input):
super(Discriminator, self).init()

    channels, heigh, width = input

    #---calculate output shape of image discriminator (PatchGAN)---
    self.output = (1, heigh//2**4, width//2**4)

    def discriminator_block(in_shape, out_shape, normalize=True):
        """Returnt downsampling layers of each discriminator block"""
        layers = [nn.Conv2d(in_shape, out_shape, 4, stride=2, padding=1)]
        if normalize:
            layers.append(nn.InstanceNorm2d(out_shape))
        layers.append(nn.LeakyReLU(0.2, inplace=True))
        return layers

    self.model = nn.Sequential(
        *discriminator_block(input, 64, normalize=False),
        *discriminator_block(64, 128),
        *discriminator_block(128, 256),
        *discriminator_block(256, 512),
        nn.ZeroPad2d((1, 0, 1, 0)),
        nn.Conv2d(512, 1, 4, padding=1)
    )class Discriminator(nn.Module):
def __init__(self, input):
    super(Discriminator, self).__init__()

    channels, heigh, width = input

    #---calculate output shape of image discriminator (PatchGAN)---
    self.output = (1, heigh//2**4, width//2**4)

    def discriminator_block(in_shape, out_shape, normalize=True):
        """Returnt downsampling layers of each discriminator block"""
        layers = [nn.Conv2d(in_shape, out_shape, 4, stride=2, padding=1)]
        if normalize:
            layers.append(nn.InstanceNorm2d(out_shape))
        layers.append(nn.LeakyReLU(0.2, inplace=True))
        return layers

    self.model = nn.Sequential(
        *discriminator_block(input, 64, normalize=False),
        *discriminator_block(64, 128),
        *discriminator_block(128, 256),
        *discriminator_block(256, 512),
        nn.ZeroPad2d((1, 0, 1, 0)),
        nn.Conv2d(512, 1, 4, padding=1)
    )

and This is my main program to call the generator and discriminator
G_AB = Generator(3, opt.n_residual_blocks)
G_BA = Generator(3, opt.n_residual_blocks)
D_A = Discriminator(input_shape)
D_B = Discriminator(input_shape)

Hi,

You can use triple backticks like ``` before and efter your code to format it properly.

This code looks good and long as input shape is a tuple or list of 3 numbers.

which part I have to use backticks?

If you write the following:

```python
# You code here
```

You code will be properly indented.

Oh I see, oke thank you