Vector Aggregation

Vector aggregation in the context of embeddings refers to the process of combining multiple vector representations into a single, unified vector. This technique is particularly useful in machine learning and data science, especially when dealing with embeddings from natural language processing (NLP), image processing, or any domain where objects are represented as high-dimensional vectors.

Understanding Vector Aggregation

Embeddings are dense vector representations of objects (like words, sentences, or images) that capture their underlying semantic properties in a way that is understandable by machine learning models. When dealing with multiple such embeddings, it might be necessary to aggregate them to produce a single representation that captures the collective properties of all the items in the set.

Applications in Machine Learning

Vector aggregation is extensively used across various machine learning applications.

Natural Language Processing

Sentence or Document Embedding: Individual word embeddings within a sentence or document can be aggregated to form a single vector representation of the entire text. This aggregated vector can then be used for tasks like text classification, sentiment analysis, or document clustering.

Information Retrieval: Aggregated embeddings can help in summarizing multiple documents or in query refinement, where the query and multiple documents' embeddings are aggregated to improve search results.

Image Processing

Feature Aggregation: In image recognition or classification, features extracted from different parts of an image (e.g., via convolutional neural networks) can be aggregated to form a global feature vector.

Recommender Systems

User or Item Profiles: Aggregating item embeddings that a user has interacted with can create a dense representation of a user's preferences. Similarly, aggregating user embeddings for a particular item can help in understanding the item’s appeal across different user segments.

Time Series Analysis

Temporal Data Aggregation: In scenarios where temporal dynamics are captured via embeddings at different time steps (e.g., stock prices, sensor data), these can be aggregated to form a representation of the overall trend or to capture cyclical patterns.

Benefits of Vector Aggregation

  • Dimensionality Reduction: Aggregation can reduce the complexity of handling multiple embeddings, making the data easier to manage and process.
  • Noise Reduction: Averaging and other aggregation methods can help mitigate the effect of noise in individual data points, leading to more robust models.
  • Improved Learning Efficiency: By summarizing data, aggregation can speed up learning processes and improve the performance of machine learning algorithms on large datasets.

Available Methods of Vector Aggregation

Example Data

CREATE TABLE documents (
body TEXT,
embedding FLOAT[] GENERATED ALWAYS AS (pgml.embed('intfloat/e5-small-v2', body)) STORED

Example of inserting text and its corresponding embedding

INSERT INTO documents (body)
VALUES -- embedding vectors are automatically generated
('Example text data'),
('Another example document'),
('Some other thing');


Adding up all the vectors element-wise. This method is simple and effective, preserving all the information from the original vectors, but can lead to large values if many vectors are summed.

SELECT id, pgml.sum(embedding)
FROM documents

Averaging (Mean)

Computing the element-wise mean of the vectors. This is probably the most common aggregation method, as it normalizes the scale of the vectors against the number of vectors being aggregated, preventing any single vector from dominating the result.

SELECT id, pgml.divide(pgml.sum(embedding), count(*)) AS avg
FROM documents

Weighted Average

Similar to averaging, but each vector is multiplied by a weight that reflects its importance before averaging. This method is useful when some vectors are more significant than others.

SELECT id, pgml.divide(pgml.sum(pgml.multiply(embedding, id)), count(*)) AS id_weighted_avg
FROM documents

Max Pooling

Taking the maximum value of each dimension across all vectors. This method is particularly useful for capturing the most pronounced features in a set of vectors.

SELECT id, pgml.max_abs(embedding)
FROM documents

Min Pooling

Taking the minimum value of each dimension across all vectors, useful for capturing the least dominant features.

SELECT id, pgml.min_abs(embedding)
FROM documents