<!--
{
  "availability" : [
    "macOS: 10.15.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CreateML",
  "identifier" : "/documentation/CreateML/MLWordEmbedding",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Create ML"
    ],
    "preciseIdentifier" : "s:8CreateML15MLWordEmbeddingV"
  },
  "title" : "MLWordEmbedding"
}
-->

# MLWordEmbedding

A map of strings in a vector space that enable your app to find similar
strings by looking at a string’s neighbors.

```
struct MLWordEmbedding
```

## Overview

Use an [`MLWordEmbedding`](/documentation/CreateML/MLWordEmbedding) to configure and save a word embedding to a file,
which you then add to your project in Xcode. Your project uses that word
embedding file at runtime to create an
<doc://com.apple.documentation/documentation/NaturalLanguage/NLEmbedding>
instance, which finds similar strings based on the proximity of their
vectors.

You configure a word embedding with a dictionary, keyed by strings which
make up the *vocabulary* of the word embedding. The value for each string is
an array of doubles, which represents a vector. The length of the arrays is
arbitrary but all arrays in a word embedding must be the same length. The
length of the arrays determine the number of dimensions in the vector space.
For example, the following listing creates a word embedding with four
dimensions and a vocabulary of two strings.

```swift
let wordEmbedding = try! MLWordEmbedding(dictionary: [
    "Hello"   : [0.0, 1.2, 5.0, 0.0],
    "Goodbye" : [0.0, 1.3, -6.2, 0.1]
])
```

Once you’ve configured an [`MLWordEmbedding`](/documentation/CreateML/MLWordEmbedding), save it to an `.mlmodel` file
to include in your app.

```swift
try wordEmbedding.write(toFile: "~/Desktop/WordEmbedding.mlmodel")
```

A word embedding file can efficiently store many strings and their vectors.

## Topics

### Creating a word embedding

[`init(dictionary:parameters:)`](/documentation/CreateML/MLWordEmbedding/init(dictionary:parameters:))

Creates a word embedding.

[`MLWordEmbedding.ModelParameters`](/documentation/CreateML/MLWordEmbedding/ModelParameters-swift.struct)

The model configuration parameters.

[`modelParameters`](/documentation/CreateML/MLWordEmbedding/modelParameters-swift.property)

The model configuration parameters.

### Testing a word embedding

[`prediction(from:maxCount:maxDistance:distanceType:)`](/documentation/CreateML/MLWordEmbedding/prediction(from:maxCount:maxDistance:distanceType:))

Predicts neighbors.

[`distance(between:and:distanceType:)`](/documentation/CreateML/MLWordEmbedding/distance(between:and:distanceType:))

Calculates the distance between two strings in the vocabulary space.

  <doc://com.apple.documentation/documentation/NaturalLanguage/NLDistanceType>

[`contains(_:)`](/documentation/CreateML/MLWordEmbedding/contains(_:))

Returns a Boolean value indicating whether the vocabulary contains the given string.

[`vector(for:)`](/documentation/CreateML/MLWordEmbedding/vector(for:))

Accesses the vector associated with the given string in the vocabulary.

### Saving a word embedding

[`write(to:metadata:)`](/documentation/CreateML/MLWordEmbedding/write(to:metadata:))

Exports the word embedding as a Core ML model file at the specified URL.

[`write(toFile:metadata:)`](/documentation/CreateML/MLWordEmbedding/write(toFile:metadata:))

Exports the word embedding as a Core ML model file at the specified file path.

### Describing a word embedding

[`dimension`](/documentation/CreateML/MLWordEmbedding/dimension)

The number of dimensions in the vocabulary embedding space.

[`vocabularySize`](/documentation/CreateML/MLWordEmbedding/vocabularySize)

The number of strings in the vocabulary.

[`model`](/documentation/CreateML/MLWordEmbedding/model)

The word embedding contained within a Core ML model file.

[`description`](/documentation/CreateML/MLWordEmbedding/description)

A text representation of the word embedding.

[`debugDescription`](/documentation/CreateML/MLWordEmbedding/debugDescription)

A text representation of the word embedding that’s suitable for output during debugging.

[`playgroundDescription`](/documentation/CreateML/MLWordEmbedding/playgroundDescription)

A description of the word embedding shown in a playground.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)