Package: x-mlps
X-MLPsAn MLP model that provides a flexible foundation to implement, mix-and-match, and test various state-of-the-art MLP building blocks and architectures.
Built on JAX and Haiku.Installationpipinstallx-mlpsNote: X-MLPs will not install JAX for you (seeherefor install instructions).Getting StartedTheXMLPclass provides the foundation from which all MLP architectures are built on, and is the primary class you use.
Additionally, X-MLPs relies heavily on factory functions to customize and instantiate the building blocks that make up a particularXMLPinstance.
Fortunately, this library provides several SOTA MLP blocks out-of-the-box as factory functions.
For example, to implement the ResMLP architecture, you can implement the follow model function:importhaikuashkimportjaxfromeinopsimportrearrangefromx_mlpsimportXMLP,Affine,resmlp_block_factorydefcreate_model(patch_size:int,dim:int,depth:int,num_classes:int=10):defmodel_fn(x:jnp.ndarray,is_training:bool)->jnp.ndarray:# Reformat input image into a sequence of patchesx=rearrange(x,"(h p1) (w p2) c -> (h w) (p1 p2 c)",p1=patch_size,p2=patch_size)returnXMLP(num_patches=x.shape[-2],dim=dim,depth=depth,block=resmlp_block_factory,# Normalization following the stack of ResMLP blocksnormalization=lambdanum_patches,dim,depth,**kwargs:Affine(dim,**kwargs),num_classes=num_classes,)(x,is_training=is_training)# NOTE: Operating directly on batched data is supported as well.returnhk.vmap(model_fn,in_axes=(0,None))model=create_model(patch_size=4,dim=384,depth=12)model_fn=hk.transform(model)rng=jax.random.PRNGKey(0)params=model_fn.init(rng,jnp.ones((1,32,32,3)),False)It's important to note theXMLPmoduledoes notreformat input data for you (e.g., to a sequence of patches).
As such, you must reformat data manually before feeding it to anXMLPmodule.
Theeinopslibrary, which is installed by X-MLPs, provides functions that can help here (e.g.,rearrange).Note: Like the core Haiku modules, all modules implemented in X-MLPs support batched data and being vectorized viavmap.X-MLPs Architecture DetailsX-MLPs uses a layered approach to construct arbitrary MLP networks. There are three core modules used to create a network's structure:XSublayer- bottom level module which wraps arbitrary feedforward functionality.XBlock- mid level module consisting of one or moreXSublayermodules.XMLP- top level module which represents a generic MLP network, and is composed of a stack of repeatedXBlockmodules.To support user-defined modules, each of the above modules support passing arbitrary keyword arguments to child modules.
This is accomplished by prepending arguments with one or more predefined prefixes (including user defined prefixes).
Built-in prefixes include:"block_" - arguments fed directly to theXBlockmodule."sublayers_" - arguments fed to allXSublayers in eachXBlock."sublayers{i}_" - arguments fed to the i-thXSublayerin eachXBlock(where 1 <= i <= # of sublayers)."ff_" - arguments fed to the feedforward module in aXSublayer.Prefixes must be combined in order when passing them to theXMLPmodule (e.g., "block_sublayer1_ff_").XSublayerTheXSublayermodule is a flexible sublayer wrapper module providing skip connections and pre/post-normalization to an arbitrary child module (specifically, arbitrary feedforward modules e.g.,XChannelFeedForward).
Child module instances are not passed directly, rather a factory function which creates the child module is instead.
This ensures that individual sublayers can be configured automatically based on depth.XBlockTheXBlockmodule is a generic MLP block. It is composed of one or moreXSublayermodules, passed as factory functions.XMLPAt the top level is theXMLPmodule, which represents a generic MLP network.
NXBlockmodules are stacked together to form a network, created via a common factory function.Built-in MLP ArchitecturesThe following architectures have been implemented in the form ofXBlocks and have corresponding factory functions.ResMLP -resmlp_block_factoryMLP-Mixer -mlpmixer_block_factorygMLP -gmlp_block_factoryS²-MLP -s2mlp_block_factoryFNet -fft_block_factoryImportantly, the components that make up these blocks are part of the public API, and thus can be easily mixed and
matched. Additionally, several component variations have been made based on combining ideas from current research.
This includes:fftlinear_block_factory- an FNet block variation: combines a 1D FFT for patch mixing plus a linear layer.create_shift1d_op- A 1D shift operation inspired by S²-MLP, making it appropriate for sequence data.create_multishift1d_op- Likecreate_shift1d_op, but supports multiple shifts of varying sizes.See their respective docstrings for more information.LICENSESeeLICENSE.Citations@article{Touvron2021ResMLPFN,title={ResMLP: Feedforward networks for image classification with data-efficient training},author={Hugo Touvron and Piotr Bojanowski and Mathilde Caron and Matthieu Cord and Alaaeldin El-Nouby and Edouard Grave and Gautier Izacard and Armand Joulin and Gabriel Synnaeve and Jakob Verbeek and Herv'e J'egou},journal={ArXiv},year={2021},volume={abs/2105.03404}}@article{Tolstikhin2021MLPMixerAA,title={MLP-Mixer: An all-MLP Architecture for Vision},author={Ilya O. Tolstikhin and Neil Houlsby and Alexander Kolesnikov and Lucas Beyer and Xiaohua Zhai and Thomas Unterthiner and Jessica Yung and Daniel Keysers and Jakob Uszkoreit and Mario Lucic and Alexey Dosovitskiy},journal={ArXiv},year={2021},volume={abs/2105.01601}}@article{Liu2021PayAT,title={Pay Attention to MLPs},author={Hanxiao Liu and Zihang Dai and David R. So and Quoc V. Le},journal={ArXiv},year={2021},volume={abs/2105.08050}}@article{Yu2021S2MLPSM,title={S2-MLP: Spatial-Shift MLP Architecture for Vision},author={Tan Yu and Xu Li and Yunfeng Cai and Mingming Sun and Ping Li},journal={ArXiv},year={2021},volume={abs/2106.07477}}@article{Touvron2021GoingDW,title={Going deeper with Image Transformers},author={Hugo Touvron and Matthieu Cord and Alexandre Sablayrolles and Gabriel Synnaeve and Herv'e J'egou},journal={ArXiv},year={2021},volume={abs/2103.17239}}@inproceedings{Huang2016DeepNW,title={Deep Networks with Stochastic Depth},author={Gao Huang and Yu Sun and Zhuang Liu and Daniel Sedra and Kilian Q. Weinberger},booktitle={ECCV},year={2016}}@article{LeeThorp2021FNetMT,title={FNet: Mixing Tokens with Fourier Transforms},author={James P Lee-Thorp and Joshua Ainslie and Ilya Eckstein and Santiago Onta{\~n}{\'o}n},journal={ArXiv},year={2021},volume={abs/2105.03824}}