我正在着手实施分类问题。在将复杂噪声插入输出层之前,我的代码工作得很好。
下面的代码不适用于Pytorch错误
def forward(self, x):
#Encoder Process
out = self.feature(x)
out = self.encoder(out)
out = self.last(out)
#### Start of error point ######
batch_size, y = out.size()
out_real = x[:, 0:int(y / 2)]
out_comp = x[:, int(y / 2) : y + 1]
out_comp = out_comp * 1j
symbols = out_real + out_comp
n = torch.randn(symbols.shape. dtype=torch.cfloat).to(device)
out = symbols + n
out_real = out.real
out_imag = out.imag
out = torch.cat((out_real, out_imag),1)
#### End of error point ######
#Decoder process
out0 = self.decoder0(out)
out1 = self.decoder1(out0)
out1 += self.shortcut(out0)
out2 = self.decoder2(out1)
out2 += self.shortcut(out1)在添加### start of error point ### ... ### end of error point ###代码行之前,代码运行良好,分类工作正常。但是,添加代码后,最后一行会出现以下错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-8-a131419c404b> in <module>
6 for epoch in range(start_epoch, start_epoch+total_epoch):
7 start = time.time()
----> 8 train(epoch, scheduler)
9 end = time.time()
10 total_time += (end-start)
<ipython-input-4-d3b479d83256> in train(epoch, scheduler)
48 top5 = accuracy(outputs, targets, topk=(1, 5))
49
---> 50 loss.backward()
51 optimizer.step()
52 scheduler.step(epoch +batch_idx/iters)
~\Anaconda3\envs\1DCNN\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
219 retain_graph=retain_graph,
220 create_graph=create_graph)
--> 221 torch.autograd.backward(self, gradient, retain_graph, create_graph)
222
223 def register_hook(self, hook):
~\Anaconda3\envs\1DCNN\lib\site-packages\torch\autograd\__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
130 Variable._execution_engine.run_backward(
131 tensors, grad_tensors_, retain_graph, create_graph,
--> 132 allow_unreachable=True) # allow_unreachable flag
133
134
RuntimeError: Expected isFloatingType(grad.scalar_type()) || (input_is_complex == grad_is_complex) to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)该前向函数在一个复杂的AWGN信道上传输图像。因此,输入x是一个图像。### start of error point ### ... ### end of error point ###部件表示添加的复杂噪声。
这个问题是因为复值是在前向(X)函数的中间加上的,所以学习不起作用吗?
如果输入是真实的,但是我想把复杂的值放在前向函数的中间,那么我该怎么办?
发布于 2021-12-21 07:16:22
我认为这个错误信息是你最不担心的。
你想做什么?当你把你的特征看作是两个真实的部分时,添加复杂的噪声意味着什么?这和给out添加噪音有什么不同?
如果您真的想让您的解码器变得复杂,您应该将out作为一个复张量,并允许解码器的权重和快捷方式也是复杂的。这实际上会在你张量的真实部分和想象部分之间产生相互作用。现在,对于out的真实和想象部分,没有“复杂”的含义,它们只是额外的维度。
https://stackoverflow.com/questions/70430175
复制相似问题