Categorical Representation Learning: Morphism is All You Need

We provide a construction for categorical representation learning and introduce the foundations of"$\textit{categorifier}$". The central theme in representation learning is the idea of $\textbf{everything to vector}$. Every object in a dataset $\mathcal{S}$ can be represented as a vector in $\mathbb{R}^n$ by an $\textit{encoding map}$ $E: \mathcal{O}bj(\mathcal{S})\to\mathbb{R}^n$. More importantly, every morphism can be represented as a matrix $E: \mathcal{H}om(\mathcal{S})\to\mathbb{R}^{n}_{n}$. The encoding map $E$ is generally modeled by a $\textit{deep neural network}$. The goal of representation learning is to design appropriate tasks on the dataset to train the encoding map (assuming that an encoding is optimal if it universally optimizes the performance on various tasks). However, the latter is still a $\textit{set-theoretic}$ approach. The goal of the current article is to promote the representation learning to a new level via a $\textit{category-theoretic}$ approach. As a proof of concept, we provide an example of a text translator equipped with our technology, showing that our categorical learning model outperforms the current deep learning models by 17 times. The content of the current article is part of the recent US patent proposal (patent application number: 63110906).


INTRODUCTION
The rise of category theory was a great revolution of mathematics in the 20th century. Martin Kuppe once created a wonderful map of the mathematical landscape (see Fig. 1) in which category theory hovers high above the ground, providing a sweeping vista of the terrain. It enables us to see relationships between various fields that are otherwise imperceptible at ground level, attesting that seemingly unrelated areas of mathematics aren't so different after all. A category describes a collection of objects together with the relations (called morphisms) between them. The concept of category has provided a unified template for different branches of mathematics. Category theory is expected to be suitable for modeling datasets with relational structures, such as semantic relations among words, phrases and sentences in language datasets, or social relations among people or organizations in social networks. From the category theory perspective, relationships are everything. Objects must be defined and can only be defined through their interrelations. So far, the field of representation learning [1,2] has remained focused on learning object encodings, following the idea of "everything to vector". More recently, graph neural network models [3,4] and self-attentive mechanisms [5] have begun to model relationships between objects, but they still did not place relationships in the first place. This article aims to develop a novel machine learning framework, called categorical representation learning, that directly learns the representation of relations as feature matrices (or tensors). The learned relations will then be used in different tasks. For example, direct relations can be composed to reveal higher-order relations. Functor map between different categories can be learned by aligning the relations (morphisms). Relations can guide the algorithm to identify clusters of objects and to perform renormalization transformations to simplify the category.
1.1. Overview. The construction of the proposed architecture in the current article will contain three major steps: • Step 1: Mine the categorical structure from data. In this step the objective is to develop the categorical representation learning approach, which enables the machine to extract the representation of objects and morphisms from data. The approach developed in this step will be the foundation of the construction which provides the morphism representation to drive further applications. • Step 2: Align the categorical structures between datasets. The objective in this step is to develop the functorial learning approach, which can establish a functor between categories based on the learned categorical representations by aligning the morphism representations. The technique developed in this step will find applications in unsupervised (or semi-supervised) translation. • Step 3: Discover hierarchical structures with tensor categories. The objective in this step is to combine the categorical representation learning and functorial learning in the setting of a tensor category to learn the tensor functor that fuses simple objects into composite objects. The approach here will enable the algorithm to perform renormalization group transformations on categorical dataset, which progressively simplifies the category structure. This will open up broad applications in classification and generation tasks of categories.

AKNOWLEDGEMENT
We would like to thank Max Tegmark, Ehsan Kamali Nezhad, Lindy Blackburn for valuable discussions.

CATEGORIES AND CATEGORICAL EMBEDDING
A category C consists of a set of objects, Obj(C) , and a set of morphisms Hom(C) between objects. Each morphism f maping a source object a to a target object b, is denoted as f : a → b. The hom-class Hom(a, b) denotes the set of all morphisms from a to b. Morphisms benefit from composition laws. The composition is a binary operation; • : Hom(a, b) × Hom(b, c) → Hom(a, c) defined for every three objects a, b, c. The composition of morphisms f : Given a category C, for every object x ∈ Obj(C) there exists an identity morphism id x : x → x, such that for every morphism f : a → x and g : x → b, one has id x • f = f and g • id x = g, that is pre and post compositions of morphisms in the category with the identity morphism will leave them unchanged.
One can associate a vector space to a given category, as its representation space. For the latter one can think of such vector space as a category itself, and use a categorical functors as a map from a given category to the geometric space.
Take for instance a category C in which every object, a is mapped to a vector v a in an ambient vector space R, and every morphisms between two objects in C, f : a → b, is mapped to a morphism between vectors v a , v b in R. Since R is a vector space, a morphism between two vectors is then realized in R as a matrix M f : v a → v b . For our purposes, the object and morphism embeddings, discussed above, will be implemented by separate embedding layers in a neural network. The map f is a morphism from object a to object b, iff the embedding matrix can transform the embedding vectors as v b = M f v a under matrix-vector multiplication. The composition of morphisms is then realized as matrix-matrix multiplications as M g•f = M g M f , which is naturally associative. The identity morphism of an object x ∈ C is then represented as the projection operator M idx = v x v x /|v x | 2 , which preserves the vector representation v x of the object. In this manner, the category structure is then represented respectively by the object, and morphism embeddings in the feature space.  3.0.1. Fuzzy Morphisms. In machine learning, the relation between objects may not be strict, that is; M f v a will not match v b precisely, but only align with v b with a high probability. To account for the fuzziness of relations then, the statement of f : a → b should be replaced by the probability of f belonging to the class Hom(a, b). It can be written as which is parametrized by the logit z(a The logit is proposed to be modeled by such that if v b and M f v a align in similar directions, the logit will be positive, which results in a high probability for the morphism f to connect a to b. The objects a and b are said to be related (linked) if there exists at least one morphism connecting them. The linking probability is then proportional to the sum of the likelihood of each candidate morphism, . The probability is normalized by considering the unlinking likelihood as the unit. Thus the binary probability P (a → b) can be modeled by the sigmoid of a logit z(a → b) that aggregates the contributions from all possible morphisms, where it is assumed that each morphism contributes to the linking probability independently. More generally, the assumption of the independence of morphisms in determining the linking probability can be relaxed, such that the logits z(a f − → b) of different morphisms f is aggregated by a generic nonlinear function: where F may be realized by a deep neural network, and f denotes the concatenation of logit contributions from different morphisms.
3.0.2. Learning Embedding from Statistics. The key idea of unsupervised representation learning is to develop feature representations from data statistics. In the categorical representation learning, the object and morphism embeddings are learned from the concurrence statistics, which corresponds to the probability p(a, b) that a pair of objects (a, b) occurs together in the same composite object. For example, two concurrent objects could stand for two elements in the same compound, or two words in the same sentence, or two people in the same organization. Concurrence does not happen for no reason. Assuming that two objects appearing together in the same structure can always be attributed to the fact that they are related by at least one type of relations, then the linking probability P (a → b) should be maximized for the observed concurrent pair (a, b) in the dataset. The negative sampling method can be adopted to increase the contrast. For each observed (positive) concurrent pair (a, b), the object b will be replaced by a random object b , drawn from the negative sampling distribution p N (b ) among all possible objects. The replaced pair (a, b ) will be considered to be an unrelated pair of objects. Therefore the training objective is to maximize the linking probability P (a → b) for the positive sample (a, b) and also maximize the unlinking probability P (a b ) = 1 − P (a → b ) for a small set of negative samples (a, b ), With the model of P (a → b) in Eq. (4), the object embedding v a and morphism embedding M f can be trained by maximizing the objective function L. The negative sampling distribution p N (b ) can be engineered, but the most natural choice is to take the marginalized object distribution p N (b ) = p(b ) = a p(a, b ). The theoretical optimum [6] is achieved with the following logit which learns the point-wise mutual information (PMI) between objects a and b. If the objects were not related at all, their concurrence probability should factorize to the product of object frequencies, i.e. p(a, b) = p(a)p(b), implying zero PMI. So a non-zero PMI indicates non-trivial relations among objects: a positive relation (PMI> 0) enhances p(a, b) while a negative relation (PMI< 0) suppresses p(a, b) relative to p(a)p(b). By training the logit z(a → b) to approximate the PMI, the relations between objects are discovered and encoded as the feature matrix M f of morphisms.
It is noted that the definition of concurrence depends on the scope (or the context scale). For example, the concurrence of two words can be restricted in the scope of phrases or sentences or paragraphs. Different choices for the concurrence scope (say length of sentences, or phrases being considered) will affect the concurrent pair distribution p(a, b), which then leads to different results for the object and morphism embeddings. This implies that objects may be related by different types of morphisms in different scopes. The categorical representation learning approach mentioned above can be applied to uncover the morphism embeddings in a scope-dependent manner. Eventually, the different object and morphism embeddings across various scopes can further be connected by the renormalization functor, which maps the category structure between different scales, detail of which will be elaborated in later sections. The scope-dependent categorical representation learning will form the basis for the development of unsupervised renormalization technique for categories with hierarchical structures, which will find broad applications in translation, classification, and generation tasks.
3.0.4. Connection to Multi-Head Attention. The multi-head attention mechanism [5] consists of two steps. The first step is a dynamic link prediction by the "query-key matching", that the probability to establish an attention link from a to b is given by with f labeling the attention head. The logit is computed from the inner product between the query vector , Q f v b , and the key vector, K f v a , as which can be written in the form of Eq.
The second step is the value propagation along the attention link (weighted by the linking probability). Focusing on the first step of dynamic link prediction, the linking probability model in Eq. (4) resembles the multi-head attention mechanism with the morphism embedding M f = Q f K f given by the product of query and key matrices for each attention head. The proposal of the categorical representation learning is to keep the learned morphism matrix M f as encoding of relations in the feature space, which can be further used in other task applications.
The matrix M f can also be viewed as a metric in the feature space, which defines the inner product of object vectors. Different morphisms f correspond to different metrics M f , which distort the geometry of the feature space differently (bring object embeddings together or pushing them apart). When there are multiple relations in the action, the feature space is equipped with different metrics simultaneously, which resembles the idea of superposition of geometries in quantum gravity. If the single-head logit z(a is considered as an (negative) energy of two objects a and b embedded in a specific geometry, the aggregated multi-head logit analogous to the (negative) free energy that the objects will experience in the ensemble of fluctuating geometries.

Aligning the Categorical Structures between Data sets.
3.1.1. Tasks as Functors. Functor is a fundamental concept in the category theory. It denotes the structure-preserving map between two categories. A functor F from a source category C to a target category D is a mapping that associates to each object a in C an object F(a) in D, and associates to each morphism f : The definition can be illustrated with the following commutative diagram.
A functor between two categories not only maps objects to objects, but also preserves their relations, which is essential in category theory. Many machine learning tasks can be generally formulated as functors between two data categories. For instance, machine translation is a functor between two language categories, which not only maps words to words but also preserves the semantic relations between words. Image captioning is a functor from image to language categories, that transcribes objects in the image as well as their interrelations.
In what follows, we discuss Functorial learning as an approach, providing the means for the machine to learn the functorial maps between categories in an unsupervised or semisupervised manner.
3.1.2. Functorial Learning. In functorial learning, each functor F is represented by a transformation V F that transforms the vector embedding v a of each object a in the source category to the vector embedding v F (a) of the corresponding object F(a) in the target category and also transforms the matrix embedding M f of each morphism in the source category to the matrix embedding M F (f ) of the corresponding morphism F(f ) in the target category represented by the following commutative diagram In the case where the objects are embedded as unit vectors on a hypersphere, the functorial transformation V F will be represented by orthogonal matrices, whose inverses are simply given by the transpose matrices V F , which admit efficient implementation in the algorithm. The proposed representation for the functor automatically satisfies the functor axioms, as So as long as the optimal transformation V F can be found, our design will ensure that it parametrizes a legitimate functor between categories.
3.1.3. Universal Structure Loss. Now we explain how to find the optimal transformation V F . The most important requirement for a functor is to preserve the morphisms between two categories. Hence we propose to train the functor by minimizing a loss function which ensures the structural compatibility in Equation (11). We denote the loss function by universal structure loss, given the matrix representation M f , M F (f ) of morphisms in both the source and target categories, which were obtained from the categorical representation learning. The loss function is universal in the sense that it is independent of the specific task that the functor is trying to model. In this approach, the morphism embeddings are all that we need to drive the learning of functor transformation V F . This is precisely in line with the spirit of the category theory: objects are illusions, they must be defined and can only be defined by morphisms. Therefore "Morphism is all you need!" .
3.1.4. Alignment Loss. However, in reality, the learned morphism matrices M f may not be of full rank. In such cases, the solution of the transformation V F is not unique, because arbitrary transformation within the null space of the morphism matrix can be composed with V F without affecting the structure lost L struc . To overcome this difficulty, we propose to subsidize the structure loss with additional alignment loss over a few pair of aligned pairs of objects (a, F(a)), where A is only a subset of objects in the source category. This provides additional supervised signals to train V F by partially enforcing Eq. (10). The total loss will be a weighted combination of the structure loss and the alignment loss (14) L = L struc + λL align .
By minimizing the total loss, V F will be trained, and the functor between categories can be established. The advantage of the categorical approach is that the structural loss already puts constrains on the parameters in V F , so the effective parameter space for the alignment loss is reduced, such that the model can be trained with much less supervised samples when the category structure is rigid enough.

Renormalization as Tensor Bifunctor.
Renormalization group plays an essential role in analyzing the hierarchical structures in physics and mathematics. It provides an efficient approach to extract the essential information of a system by progressively coarse graining the objects in the system. The categorical representation learning and functorial learning proposed in the previous two sections provide solid foundation to develop hierarchical renormalization approach for machine learning. This will enable the machine to summarize the feature representation of composite objects from that of simple objects. The elementary step of a coarse graining procedure is to fuse two objects into one composite object (or "higher" object). Multiple objects can then be fused together in a pair-wise manner progressively. In category theory, the pair-wise fusion of objects is formulated as a tensor bifunctor ⊗ : C × C → C. The tensor bifunctor maps each pair of objects (a, b) to a composite object a ⊗ b and each pair of morphisms (f, g) to a composite morphism f ⊗ g while preserving the morphisms between objects, as presented in the following diagram.
The category equipped with the tensor bifunctor is called a tensor category (or monoidal category). Objects and morphisms of different hierarchies are treated within the same framework systematically. This enables the algorithm to model multi-scale structure in the data set with the universal approach of categorical representation learning.
3.2.2. Representing Tensor Bifunctor. As a special case of general functors, the tensor bifunctor can be represented by a fusion operator Θ, such that the object embeddings are fused by and the morphism embeddings are fused by following the general scheme in Eq. (10) and Eq. (11). The tensor product of the vector and matrix representations are implemented as Kronecker products. Θ can be viewed as an operator which projects the tensor-product feature space back to the original feature space.
To simplify the construction, we will assume that the representation of the tensor bifunctor is strict in the feature space, meaning that the fusion is strictly associative without non-trivial natural isomorphisms, Such a strict representation will always be possible given a large enough feature space dimension, since every monoidal category is equivalent to a strict monoidal category. To impose the strict associativity in the learning algorithm, we propose to fuse objects in different orders, such that the machine will not develop any preference over a particular fusion tree and will learn to construct an associative fusion operator Θ. With the fusion operator, we establish the embedding for all composite objects (and their morphisms) in the category given the embedding of fundamental objects (and their morphisms).

Multi-Scale Categorical Representation
Learning. The tensor bifunctor learning can be combined with the categorical representation learning as an integrated learning scheme, which allows the algorithm to mine the category structure at multiple scales. If the data set has naturally-defined levels of scopes, one can learn the objects and morphism embeddings in different scopes. The objective is to learn the concurrence of objects within their scope according to the loss function Eq. (6). For each pair of objects (a, b) drawn from the same scope, we want to maximize the linking probability P (a → b). For randomly sampled pairs of objects (a, b ), we want to maximize the unlinking probability P (a b ) = 1 − P (a → b ). The linking probability P (a → b) is modeled by Eq. (4), based on the object and morphism embeddings. The vector embedding v a of a composite object a is constructed by recursively applying Θ to fuse from fundamental objects as in Eq. (18).
A more challenging situation is that the data set has no naturally-defined levels of scopes, such that the hierarchical representation must be established with a bootstrap approach. We assume that at least a set of elementary objects can be specified in the data set (such as words in the language data set), illustrated as small circles in Fig. 2(a). We first apply the categorical representation learning approach to learn the linking probability P (a → b) between objects. The algorithm first samples different clusters of objects from the data set within a certain scale. The linking probability is enhanced for the pair of objects concurring in the same cluster, and is suppressed otherwise, as shown in Fig. 2(b). In this way, the algorithm learns to find the embedding v a for every fundamental object a. After a few rounds of training, fuzzy morphisms among objects will be established, as depicted in Fig. 2(c). The thicker link represents higher linking probability P (a → b) and stronger relations. In later rounds of training, when the sampled cluster contains a pair of objects (a, b) connected by a strong relation (with P (a → b) beyond a certain threshold), it will be considered as a composite object a ⊗ b, as yellow groups in Fig. 2(d). The embedding v a⊗b of composite object will be calculated from that of the fundamental objects as v a⊗b = Θ(v a ⊗ v b ), based on the fusion operator Θ given by the tensor bifunctor model. The model will continue to learn the linking probability P (a ⊗ b → c) between the composite object a ⊗ b and the other object c in the cluster, as red polygons in Fig. 2(d). In this way, the fusion operator Θ will get trained together with the object and morphism embeddings. This will establish higher morphisms between composite objects like Fig. 2(e). The approach can then be carried on progressively to higher levels, which will eventually enable the machine to learn the hierarchical structures in the data set and establish the representations for objects, morphisms and tensor bifunctors all under the same approach.

APPENDIX A. PRELIMINARY RESULTS
A.1. Learning Chemical Compounds.
Data Set. To demonstrate the proposed categorical learning framework, we apply our approach to the inorganic chemical compound data set. The data set for our proof of concept (POC) contains 61023 inorganic compounds, covering 89 elements in the periodic table Fig. 3(a). The data set can be modeled as a category, which contains elements as fundamental objects, as well as functional groups and compounds as composite objects. The morphisms represent the relations (such as chemical bonds) between atoms or groups of atoms. We assume that the concurrence of two elements in a compound is due to the underlying relations, such that the morphisms can emerge from learning the elements' concurrence. On the data set level, we collect the point-wise mutual information (PMI) PMI(a, b) = log p(a, b) − log p(a) − log p(b), where p(a, b) is the probability for the pair of elements a, b to appear in the same chemical compound, and p(a) is the marginal distribution. Performing a principal component analysis of the PMI and taking the leading three principal components, we can obtain three-dimensional vector encodings of elements, as shown in Fig. 3(b). We observe that elements of similar chemical properties are close to each other, because they share similar context in the compound. This observation indicates that our algorithm is likely to uncover such relations among elements from the data set.
A.2. Unsupervised/Semisupervised Translation. To demonstrate the categorical representation learning and the functorial learning in sections 1 and 2, we designed an unsupervised translation task with the chemical compound data set. We take the data set in English and translate each element into Chinese. Fig. 3(c,d) shows some samples from both the English and Chinese data set. The task of unsupervised (or semisupervised) translation is to learn to translate chemical compounds from one language to another without aligned samples (or with only a few aligned samples). The unsupervised translation is possible since the chemical relation between elements are identical in both languages. The categorical representation learning can capture these relations and represent them as morphism embeddings. By aligning the morphism embeddings, using the funtorial learning approach, the translator can be learned as a functor that maps between the English and Chinese compound categories. The translation functor is required to map elements to elements while preserving their relations.
We demonstrate the semisupervised translation. We assign 15 elements as supervised data and provide the aligned English-Chinese element pairs to the machine. By minimizing the structure and alignment loss together. with our categorical learning model. Recently, text translation models with transformers have had impressive achievements on the datasets with long inputs (usually more than 250 tokens). However we do not use transformers, due to the name of compounds not being long and hence transformers do not have specific supremacy over their counterparts with recurrent neural network (RNN) based cells in this case. Therefore our choice is to design a seq-2-seq model which includes two blocks of Encoder and Decoder with general recurrent unit (GRU) cells which benefits from the attention mechanism (see Figure 5). We add attention mechanism to the model as there is no significant language pattern in the compounds, hence the connection between Encoder and Decoder must be empowered with attention mechanism so that almost all the information in the Encoder can be expressed to the Decoder. Finally, we apply the teacher-force technique to provide aid to the Decoder for translating the English name of compounds to the Chinese counterparts, more robustly. ., x T } ∈ X and y = {y 1 , y 2 , .., y T } ∈ Y respectively. For each element in x, the Encoder E reads x t and encapsulates its information in the hidden states h t = E(h t−1 , x t−1 ). After reading all elements in x, the Encoder outputs the last hidden layer h T = E(h T −1 , x T −1 ) to be passed to the first cell in Decoder. Therefore for each element y t in y, the hidden state of the decoder cell at t is given by s t = D(s t−1 , y y , c t ).

Semi-Supervised
In this formula, the attention mechanism is expressed as the context vector c t . Conceptually, the context vector c t = T i=1 α t,i h i carries out the sum of information of the encoder cells (h i ) weighted by the alignment score α t,i . Here the score function α t,i indicates how much information from each input x i should be contributed to the output y t and can have different forms [5,7]. Finally, given the output of Decoder cellsŷ, its similarity will be compared to the true translation y thorough the cross entropy loss function: with θ E and θ D the trainable parameters in the Encoder block and the Decoder block respectively. The Equation 19 can be seen as a metric function that measures the distance between y andŷ. Note that the categorical learning model has only 16600 parameters and we believe it is only fair if only we compare the deep learning model with similar number of parameters. Table 1 elaborates on our results for deep learning models and our categorical learning model. These results indicate that the deep learning models are unable to outperform our model unless the number of parameters they use is 17 times more than our categorical learning model, in which case the deep learning model has the capability of achieving a better performance. This comparison is particularly interesting, once one decreases the number of parameters to about 25,000. Our experiments clearly indicate that the categorical learning methods completely outperform the deep learning models.