Skip to main content
Trains the index to optimize it for efficient similarity search queries. Training is essential for IVF-based indexes to achieve optimal query performance and accuracy.
index.train(
    batch_size=2048,
    max_iters=100,
    tolerance=1e-6,
    max_memory=0
)

Parameters

ParameterTypeDefaultDescription
batch_sizeint2048Number of vectors to process per training batch
max_itersint100Maximum number of training iterations
tolerancefloat1e-6Convergence tolerance for training completion
max_memoryint0Maximum memory usage in MB (0 = unlimited)
Training is a compute-intensive operation that may take several seconds to minutes depending on the index size and configuration.

Returns

None

Exceptions

  • Throws if the API request fails due to network connectivity issues.
  • Throws if authentication fails (invalid API key).
  • Throws if the encryption key is invalid for the specified index.
  • Throws if there are insufficient resources to complete training.
  • Throws if the index has no vectors to train on.
  • Throws if the index configuration is incompatible with training.
  • Throws if training parameters are out of valid ranges.
  • Throws if training fails to converge within the specified parameters.

Example Usage

Basic Index Training

# Train the index after adding the data
index.train()

Custom Training Parameters

# Train with custom parameters for large dataset
index.train(
    batch_size=4096,     # Larger batches for better performance
    max_iters=200,       # More iterations for better convergence
    tolerance=1e-7,      # Stricter convergence criteria
    max_memory=2048      # Limit memory usage to 2GB
)