import torch import math import torch.optim as optim import gc #torch.cuda.emptycache()…
- User
import torch import math import torch.optim as optim import gc
#torch.cuda.empty_cache() #gc.collect()
class ResidualAttentionTranspose(nn.Module): def init(self, dim): super(ResidualAttentionTranspose, self).init() self.linear1 = ResidualLinear(dim, dim) self.attenton = nn.MultiheadAttention(dim, 8, batch_first=True)
codedef forward(self, x): shape = x.shape x = x.flatten(2).transpose(1, 2) x = self.linear1(x) attn_output, attn_weights = self.attenton(x, x, x) return (x + attn_output).view(shape)class ResidualAttentionFlatten(nn.Module): def init(self, dim): super(ResidualAttentionFlatten, self).init() self.linear1 = ResidualLinear(dim, dim) encoder_layer = nn.TransformerEncoderLayer(d_model=dim, nhead=4) self.transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=3) #self.attenton = nn.MultiheadAttention(dim, 8, batch_first=True)
codedef forward(self, x): shape = x.shape x = x.flatten(2) x = self.linear1(x) #attn_output, attn_weights = self.attenton(x, x, x) attn_output = self.transformer_encoder(x) return (x + attn_output).view(shape)class PrintShape(nn.Module): def init(self): super(PrintShape, self).init()
codedef forward(self, x): #print(x.shape) return xclass PositionEmbedding(nn.Module): def init(self, dim): super(PositionEmbedding, self).init() self.register_buffer("position_ids", torch.arange(16384).expand((1, -1)), persistent=False) self.position_embedding = nn.Embedding(16384, dim)
codedef forward(self, x): shape = x.shape x = x.flatten(2).transpose(1, 2) embeddings = self.position_embedding(self.position_ids) x = x + embeddings return x.transpose(2, 1).view(shape)class MultiScaleConvModule(nn.Module): def init(self, in_channels, out_channels): super(MultiScaleConvModule, self).()
- Assistant
- User
so if i predicting outpuit with a lot of zeros and my input is also a lot of zeros, it can be problem? what to do?
- Assistant