During the training phase I encountered a code error problem .RuntimeError: Expected condition, x and y to be on the same device, but condition is on cpu and x and y are on cuda:0 and cuda:0 respectively

chen123hao: <p><strong>The main code is as follows:</strong></p>
<p>net = xxnet.cuda()<br>
input = input.cuda()<br>
target = target.cuda()<br>
criterion = nn.MSELoss().cuda()			<br>
out, output, combine, route, l_out, c_out = net(input)<br>
loss = criterion(output,target)  + 0.2*criterion(combine,target)<br>
optimizer.zero_grad()<br>
loss.backward()</p>
<p><strong>I wonder what the “condition” means and how to fix this error issue.</strong></p>

ptrblck: <p>Could you post a minimal and executable code snippet to reproduce the error, please?</p>

chen123hao: <p>Thank you for your reply. The thin version of the code and error reporting are shown below:</p>
<p>def run(x, n, sigma):<br>
r1 = torch.pow(x, n) / (torch.pow(x, n) + torch.pow(sigma, n) + 1e-6)<br>
return r1</p>
<p>#######################</p>
<h2><a name="u_net-1" class="anchor" href="#u_net-1"></a>U_Net</h2>
<p>#######################<br>
class DoubleConv(nn.Module):<br>
“”“(convolution =&gt; [BN] =&gt; ReLU) * 2"”"</p>
<pre><code>def __init__(self, in_channels, out_channels, mid_channels=None):
    super().__init__()
    if not mid_channels:
        mid_channels = out_channels
    self.double_conv = nn.Sequential(
        nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),
        nn.BatchNorm2d(mid_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )

def forward(self, x):
    return self.double_conv(x)
</code></pre>
<p>class Down(nn.Module):<br>
“”“Downscaling with maxpool then double conv”“”</p>
<pre><code>def __init__(self, in_channels, out_channels):
    super().__init__()
    self.maxpool_conv = nn.Sequential(
        nn.MaxPool2d(2),
        DoubleConv(in_channels, out_channels)
    )

def forward(self, x):
    return self.maxpool_conv(x)
</code></pre>
<p>class Up(nn.Module):<br>
“”“Upscaling then double conv”“”</p>
<pre><code>def __init__(self, in_channels, out_channels, bilinear=True):
    super().__init__()

    # if bilinear, use the normal convolutions to reduce the number of channels
    if bilinear:
        self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
        self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
    else:
        self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
        self.conv = DoubleConv(in_channels, out_channels)

def forward(self, x1, x2):
    x1 = self.up(x1)
    # input is CHW
    diffY = x2.size()[2] - x1.size()[2]
    diffX = x2.size()[3] - x1.size()[3]

    x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
                    diffY // 2, diffY - diffY // 2])

    x = torch.cat([x2, x1], dim=1)
    return self.conv(x)
</code></pre>
<p>class OutConv(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels):<br>
super(OutConv, self).<strong>init</strong>()<br>
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)</p>
<pre><code>def forward(self, x):
    return self.conv(x)
</code></pre>
<p>“”" Full assembly of the parts to form the complete network “”"</p>
<p>class UNet(nn.Module):<br>
def <strong>init</strong>(self, n_channels=16, n_classes=1, bilinear=False):<br>
super(UNet, self).<strong>init</strong>()<br>
self.n_channels = n_channels<br>
self.n_classes = n_classes<br>
self.bilinear = bilinear</p>
<pre><code>    self.inc = DoubleConv(n_channels, 64)
    self.down1 = Down(64, 128)
    self.down2 = Down(128, 256)
    factor = 2 if bilinear else 1
    self.down3 = Down(256, 512 // factor)
    # factor = 2 if bilinear else 1
    # self.down4 = Down(512, 1024 // factor)
    # self.up1 = Up(1024, 512 // factor, bilinear)
    self.up2 = Up(512, 256 // factor, bilinear)
    self.up3 = Up(256, 128 // factor, bilinear)
    self.up4 = Up(128, 64, bilinear)
    self.outc = OutConv(64, n_classes)

def forward(self, x):
    x1 = self.inc(x)
    x2 = self.down1(x1)
    x3 = self.down2(x2)
    x4 = self.down3(x3)
    #x5 = self.down4(x4)
    #x = self.up1(x5, x4)
    x = self.up2(x4, x3)
    x = self.up3(x, x2)
    x = self.up4(x, x1)
    logits = torch.sigmoid(self.outc(x))
    return logits
</code></pre>
<p>class net1(nn.Module):<br>
def <strong>init</strong>(self,en_net=UNet(n_channels=10)):<br>
super(net1,self).<strong>init</strong>()<br>
self.en = en_net<br>
self.conv1 = nn.Sequential(nn.Conv2d(1,16, kernel_size=1, stride=1, padding=0, bias=False),<br>
nn.BatchNorm2d(16),<br>
nn.ReLU(inplace=True))</p>
<pre><code>    self.conv2=nn.Conv2d(16,1,1,1,bias=False)
def forward(self, x):
    output = []
    n = torch.ones(10)
    sigma = torch.linspace(0.1, 1.0, 10)
    input = self.conv1(x)
    input = torch.sigmoid(self.conv2(input))

    for i in range(len(n)):
        output.append(run(input,n[i],sigma[i]))

    x_fusion = output[0]
    for i in range(9):
        x_fusion = torch.cat([x_fusion, output[i + 1]], dim=1)

    out = self.en(x_fusion)

    return input, out, 0.3*input, n, sigma, output
</code></pre>
<p>class net2(nn.Module):<br>
def <strong>init</strong>(self,en_net=UNet(n_channels=16)):<br>
super(net2,self).<strong>init</strong>()<br>
self.en = en_net<br>
self.conv1 = nn.Sequential(nn.Conv2d(1, 16, kernel_size=1, stride=1, padding=0, bias=False),<br>
nn.BatchNorm2d(16),<br>
nn.ReLU(inplace=True))</p>
<pre><code>    self.conv2=nn.Conv2d(16,1,1,1,bias=False)




def forward(self, x):
    output = []
    sigma = x.mean() * torch.ones(16).cuda()

    n = torch.linspace(0.5, 8, 16)
    input = self.conv1(x)
    input = torch.sigmoid(self.conv2(input))

    for i in range(len(n)):
        output.append(run(input,n[i],sigma[i]))

    x_fusion = output[0]
    for i in range(15):
        x_fusion = torch.cat([x_fusion, output[i + 1]], dim=1)

    out = self.en(x_fusion)

    return input, out, 0.3*input, n, sigma, output
</code></pre>
<p>class net3(nn.Module):<br>
def <strong>init</strong>(self,net1 = net1(), net2 = net2()):<br>
super(net3,self).<strong>init</strong>()<br>
self.n1 = net1<br>
self.n2 = net2</p>
<pre><code>def forward(self,x):
    out1 = self.n1(x)
    out2 = self.n2(out1[1])
    return out2[0], out2[1], out1, out2
</code></pre>
<p>##################</p>
<h1><a name="train-2" class="anchor" href="#train-2"></a>train</h1>
<p>##################</p>
<p>net = net3().cuda()<br>
criterion = nn.MSELoss().cuda()</p>
<p>optimizer = torch.optim.Adam(net.parameters(), lr=config.lr, weight_decay=config.weight_decay)<br>
net.train()<br>
iteration = 0<br>
start_time = time.time()<br>
…<br>
<span class="hashtag-raw">#Dataset</span> loading was omitted<br>
input = input.cuda()<br>
target = target.cuda()<br>
combine, output, _, _ = net(input)</p>
<p>loss = criterion(output,target) + 0.2*criterion(combine,target)<br>
optimizer.zero_grad()<br>
loss.backward()</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/e/eedfc33e77590b4b27152702e85db3677e4ba312.png" data-download-href="https://discuss.pytorch.org/uploads/default/eedfc33e77590b4b27152702e85db3677e4ba312" title="1XB@4GXORHTGQH@8X4F"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/e/eedfc33e77590b4b27152702e85db3677e4ba312.png" alt="1XB@4GXORHTGQH@8X4F" data-base62-sha1="y5b2VKftfC3OTJlhD0jTXihRxSi" width="690" height="156" data-dominant-color="303030"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1XB@4GXORHTGQH@8X4F</span><span class="informations">1578×358 21.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<strong>I wonder what the “condition” means and how to fix this error issue.</strong></p>

ptrblck: <p>You are creating intermediate tensors on the CPU in your <code>forward</code> methods, so move them to the same device as e.g. the input or any parameter and it should work (e.g. <code>n = torch.ones(10).to(x.device)</code>).<br>
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>

chen123hao: <p>Thanks so much and based on your suggestion the problem has been resolved.</p>