### Install Spline-Based Transformer Source: https://github.com/lucidrains/spline-based-transformer/blob/main/README.md Install the package via pip. ```bash $ pip install spline-based-transformer ``` -------------------------------- ### Image Autoencoder Wrapper Usage Source: https://github.com/lucidrains/spline-based-transformer/blob/main/README.md Example of using the ImageAutoencoderWrapper to process image data with the Spline-Based Transformer. ```python import torch from spline_based_transformer import ( SplineBasedTransformer, ImageAutoencoderWrapper ) model = ImageAutoencoderWrapper( image_size = 256, patch_size = 32, spline_transformer = SplineBasedTransformer( dim = 512, enc_depth = 6, dec_depth = 6 ) ) images = torch.randn(2, 3, 256, 256) loss = model(images, return_loss = True) loss.backward() # after much training recon_images, control_points = model(images, return_latents = True) assert images.shape == recon_images.shape # changing the control points control_points += 1 controlled_recon_images = model.decode_from_latents(control_points) assert controlled_recon_images.shape == images.shape ``` -------------------------------- ### Generate Variable Length Outputs and Interpolate Control Points Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Demonstrates generating sequences of different lengths from control points and performing linear interpolation between two sets of control points. ```python short_recon = model.decode_from_latents(control_points, num_times=256) long_recon = model.decode_from_latents(control_points, num_times=2048) print(f"Short output: {short_recon.shape}") # torch.Size([1, 256, 512]) print(f"Long output: {long_recon.shape}") # torch.Size([1, 2048, 512]) # Interpolate between two sets of control points data2 = torch.randn(1, 1024, 512) _, control_points2 = model(data2, return_latents=True) alpha = 0.5 interpolated_controls = (1 - alpha) * control_points + alpha * control_points2 interpolated_recon = model.decode_from_latents(interpolated_controls, num_times=1024) print(f"Interpolated reconstruction: {interpolated_recon.shape}") ``` -------------------------------- ### Initialize and Train Spline-Based Transformer Source: https://github.com/lucidrains/spline-based-transformer/blob/main/README.md Basic usage for initializing the model, calculating loss, and manipulating latent control points. ```python import torch from spline_based_transformer import SplineBasedTransformer model = SplineBasedTransformer( dim = 512, enc_depth = 6, dec_depth = 6 ) data = torch.randn(1, 1024, 512) loss = model(data, return_loss = True) loss.backward() # after much training recon, control_points = model(data, return_latents = True) assert data.shape == recon.shape # mess with the control points, which should preserve continuity better control_points += 1 controlled_recon = model.decode_from_latents(control_points, num_times = 1024) assert controlled_recon.shape == data.shape ``` -------------------------------- ### Initialize and Train SplineBasedTransformer Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Configure the autoencoder model and perform training or inference on sequential data. ```python import torch from spline_based_transformer import SplineBasedTransformer # Initialize the model with encoder and decoder depths model = SplineBasedTransformer( dim=512, # Input/output dimension enc_depth=6, # Number of encoder layers dec_depth=6, # Number of decoder layers model_dim=512, # Internal model dimension (defaults to dim) dim_head=64, # Dimension per attention head heads=8, # Number of attention heads dropout=0.1, # Dropout rate num_control_points=4 # Number of B-spline control points ) # Create sample sequential data: batch=2, sequence_length=1024, features=512 data = torch.randn(2, 1024, 512) # Training mode: compute reconstruction loss loss = model(data, return_loss=True) loss.backward() print(f"Reconstruction loss: {loss.item():.4f}") # Inference mode: get reconstruction and control points recon, control_points = model(data, return_latents=True) print(f"Input shape: {data.shape}") # torch.Size([2, 1024, 512]) print(f"Reconstruction shape: {recon.shape}") # torch.Size([2, 1024, 512]) print(f"Control points shape: {control_points.shape}") # torch.Size([2, 4, 512]) # Verify reconstruction matches input shape assert data.shape == recon.shape ``` -------------------------------- ### Configure Custom Encoder and Decoder Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Customize the underlying transformer architecture by passing encoder_kwargs and decoder_kwargs to the SplineBasedTransformer. ```python import torch from spline_based_transformer import SplineBasedTransformer # Advanced configuration with custom encoder/decoder kwargs model = SplineBasedTransformer( dim=512, enc_depth=8, dec_depth=8, model_dim=768, # Different internal dimension dim_head=64, heads=12, dropout=0.1, num_control_points=4, always_mlp_project=True, # Force MLP projection even if dims match encoder_kwargs={ 'ff_mult': 4, # Feed-forward expansion multiplier 'pre_norm': True, # Pre-layer normalization }, decoder_kwargs={ 'ff_mult': 4, 'pre_norm': True, } ) data = torch.randn(1, 2048, 512) loss = model(data, return_loss=True) print(f"Loss with custom config: {loss.item():.4f}") recon, control_points = model(data, return_latents=True) print(f"Output shape: {recon.shape}") # torch.Size([1, 2048, 512]) ``` -------------------------------- ### ImageAutoencoderWrapper Usage Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Adapts the SplineBasedTransformer for image data using patch-based tokenization. Supports both training and inference modes. ```python import torch from spline_based_transformer import SplineBasedTransformer, ImageAutoencoderWrapper # Create base spline transformer spline_transformer = SplineBasedTransformer( dim=512, enc_depth=6, dec_depth=6 ) # Wrap for image processing model = ImageAutoencoderWrapper( image_size=256, # Input image size (height=width) patch_size=32, # Size of each patch (must divide image_size) channels=3, # Number of image channels (RGB=3) spline_transformer=spline_transformer ) # Create batch of images images = torch.randn(2, 3, 256, 256) # Training mode loss = model(images, return_loss=True) loss.backward() print(f"Image reconstruction loss: {loss.item():.4f}") # Inference mode recon_images, control_points = model(images, return_latents=True) print(f"Input images: {images.shape}") # torch.Size([2, 3, 256, 256]) print(f"Reconstructed: {recon_images.shape}") # torch.Size([2, 3, 256, 256]) print(f"Control points: {control_points.shape}") # torch.Size([2, 4, 512]) # Verify shapes match assert images.shape == recon_images.shape ``` -------------------------------- ### Process Variable-Length Sequences Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Handle sequences of different lengths within a single batch using the lens parameter. ```python import torch from spline_based_transformer import SplineBasedTransformer model = SplineBasedTransformer( dim=256, enc_depth=4, dec_depth=4 ) # Batch of 3 sequences with different actual lengths max_length = 512 data = torch.randn(3, max_length, 256) # Specify actual lengths for each sequence (must be >= 2) lens = torch.tensor([512, 384, 256]) # Training with variable lengths loss = model(data, lens=lens, return_loss=True) loss.backward() print(f"Variable-length reconstruction loss: {loss.item():.4f}") # Inference with variable lengths recon, control_points = model(data, lens=lens, return_latents=True) print(f"Control points shape: {control_points.shape}") # torch.Size([3, 4, 256]) ``` -------------------------------- ### BibTeX Citation Source: https://github.com/lucidrains/spline-based-transformer/blob/main/README.md Citation for the Spline-Based Transformer paper. ```bibtex @misc{Chandran2024, author = {Prashanth Chandran, Agon Serifi, Markus Gross, Moritz Bächer}, url = {https://la.disneyresearch.com/publication/spline-based-transformers/} } ``` -------------------------------- ### Manipulate Control Points Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Modify learned control points to generate new reconstructions while preserving B-spline continuity. ```python import torch from spline_based_transformer import SplineBasedTransformer model = SplineBasedTransformer( dim=512, enc_depth=6, dec_depth=6 ) # Encode data to get control points data = torch.randn(1, 1024, 512) recon, control_points = model(data, return_latents=True) # Manipulate control points (e.g., add offset, scale, interpolate) modified_control_points = control_points + 0.5 # Add constant offset scaled_control_points = control_points * 1.2 # Scale values # Decode from modified control points # num_times specifies the output sequence length modified_recon = model.decode_from_latents( modified_control_points, num_times=1024 ) print(f"Modified reconstruction shape: {modified_recon.shape}") # torch.Size([1, 1024, 512]) ``` -------------------------------- ### Manipulate Image Control Points Source: https://context7.com/lucidrains/spline-based-transformer/llms.txt Perform controlled image generation and interpolation by modifying the latent control points of the ImageAutoencoderWrapper. ```python import torch from spline_based_transformer import SplineBasedTransformer, ImageAutoencoderWrapper spline_transformer = SplineBasedTransformer( dim=512, enc_depth=6, dec_depth=6 ) model = ImageAutoencoderWrapper( image_size=256, patch_size=32, spline_transformer=spline_transformer ) # Encode two different images image1 = torch.randn(1, 3, 256, 256) image2 = torch.randn(1, 3, 256, 256) _, control_points1 = model(image1, return_latents=True) _, control_points2 = model(image2, return_latents=True) # Modify control points modified_controls = control_points1 + 1.0 modified_image = model.decode_from_latents(modified_controls) print(f"Modified image shape: {modified_image.shape}") # torch.Size([1, 3, 256, 256]) # Interpolate between two images via control points for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]: interpolated_controls = (1 - alpha) * control_points1 + alpha * control_points2 interpolated_image = model.decode_from_latents(interpolated_controls) print(f"Alpha {alpha}: {interpolated_image.shape}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.