Cómo diseñar canalizaciones tensoriales complejas de aprendizaje profundo utilizando Einops con visión, atención y ejemplos multimodales
sección(“6) empaquetar desempaquetar”) B, Cemb = 2, 128 class_token = torch.randn(B, 1, Cemb, dispositivo=dispositivo) image_tokens = torch.randn(B, 196, Cemb, dispositivo=dispositivo) text_tokens = torch.randn(B, 32, Cemb, dispositivo=dispositivo) show_shape(“class_token”, class_token) show_shape(“image_tokens”, image_tokens) show_shape(“text_tokens”, text_tokens) empaquetado, ps = paquete([class_token, image_tokens, text_tokens]”b * c”) show_shape(“empaquetado”, empaquetado) print(“packed_shapes (ps):”, ps) mezclador = nn.Sequential( nn.LayerNorm(Cemb), nn.Linear(Cemb, 4 * Cemb), nn.GELU(), nn.Linear(4 * Cemb, Cemb), ).to(dispositivo) mixto = mezclador(empaquetado) show_shape(“mixto”, mixto) class_out, image_out, text_out = desempaquetar(mixto, ps, “b * c”) show_shape(“class_out”, class_out) show_shape(“image_out”, image_out) show_shape(“text_out”, text_out) afirmar class_out.shape == class_token.shape afirmar image_out.shape == image_tokens.shape afirmar text_out.shape == text_tokens.shape sección(“7) capas”) clase PatchEmbed(nn.Module): def __init__(self, in_channels=3, emb_dim=192, patch=8): super().__init__() self.patch = parche self.to_patches = Rearrange(“bc (h p1) (w p2) -> b (hw) (p1 p2 c)”, p1=parche, p2=patch) self.proj = nn.Linear(in_channels * patch * patch, emb_dim) def forward(self, x): x = self.to_patches(x) return self.proj(x) class SimpleVisionHead(nn.Module): def __init__(self, emb_dim=192, num_classes=10): super().__init__() self.pool = Reduce(“btc -> bc”, reducción=”mean”) self.classifier = nn.Linear(emb_dim, num_classes) def forward(self, tokens): x = self.pool(tokens) return self.classifier(x) patch_embed = PatchEmbed(in_channels=3, emb_dim=192, patch=8).to(dispositivo) head = SimpleVisionHead(emb_dim=192, num_classes=10).to(dispositivo) imgs = torch.randn(4, 3, 32, 32, dispositivo=dispositivo) tokens = patch_embed(imgs) logits = head(tokens) show_shape(“tokens”, tokens) show_shape(“logits”, logits) sección(“8) práctico”) x = torch.randn(2, 32, 16, 16, dispositivo=dispositivo) g = 8 xg = reorganizar(x, “b (g cg) hw -> (bg) cg hw”, g=g) show_shape(“x”, x) show_shape(“xg”, xg) media = reducir(xg, “bg cg hw -> bg 1 1 1”, “media”) var = reducir((xg – media) ** 2, “bg cg hw -> bg 1 1 1”, “media”) xg_norm = (xg – media) / torch.sqrt(var + 1e-5) x_norm = reorganizar(xg_norm, “(bg) cg hw -> b (g cg) hw”, b=2, g=g) show_shape(“x_norm”, x_norm) z = torch.randn(3, 64, 20, 30, dispositivo=dispositivo) z_flat = reordenar(z, “bchw -> bc (hw)”) z_unflat = reorganizar(z_flat, “bc (hw) -> bchw”, h=20, w=30) afirmar (z – z_unflat).abs().max().item() < 1e-6 show_shape("z_flat", z_flat) sección("9) vistas") a = torch.randn(2, 3, 4, 5, dispositivo=dispositivo) b = rerange(a, "bchw -> bhwc”) print(“a.is_contiguous():”, a.is_contiguous()) print(“b.is_contiguous():”, b.is_contiguous()) print(“b._base es a:”, getattr(b, “_base”, Ninguno) es a) sección(“Listo ✅ Ahora tener patrones einops reutilizables para visión, atención y empaquetado de tokens multimodales”)