public final 类 TextEmbedder
对文本执行嵌入提取。
此 API 需要具有(可选)TFLite 模型元数据的 TFLite 模型。
具有 int32 输入张量的模型需要元数据,因为它包含模型分词器的输入处理单元。具有字符串输入张量的模型不需要元数据。
- 输入张量
- 三个形状为
[batch_size x bert_max_seq_len]的输入张量 (kTfLiteInt32),分别表示输入 ID、掩码 ID 和片段 ID。此输入签名需要模型元数据中的 Bert Tokenizer 进程单元。 - 或一个形状为
[batch_size x max_seq_len]的输入张量 (kTfLiteInt32),表示输入 ID。此输入签名需要在模型元数据中包含 Regex Tokenizer 进程单元。 - 或者一个无形状或形状
[1]包含输入字符串的输入张量 (kTfLiteString)。
- 三个形状为
- 至少一个形状为
[1 x N]的输出张量 (kTfLiteFloat32/kTfLiteUint8),其中 N 是生成的嵌入中的维度数。
嵌套类
| 类别 | TextEmbedder.TextEmbedderOptions | 用于设置 TextEmbedder 的选项。 |
|
公共方法
| void |
close()
关闭并清理
TextEmbedder。 |
| 静态双精度 | |
| 静态 TextEmbedder |
createFromFile(Context context, String modelPath)
使用模型文件和默认的
TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。 |
| 静态 TextEmbedder |
createFromFile(Context context, File modelFile)
使用模型文件和默认的
TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。 |
| 静态 TextEmbedder |
createFromOptions(Context context, TextEmbedder.TextEmbedderOptions 选项)
通过
TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。 |
| TextEmbedderResult |
继承的方法
公共方法
public static double cosineSimilarity (Embedding u、Embedding v)
参数
| u | |
|---|---|
| v |
抛出
| IllegalArgumentException | (如果嵌入不同类型(浮点数与量化)、大小不同或 L2 范数为 0。 |
|---|
public static TextEmbedder createFromFile (Context context, String modelPath)
使用模型文件和默认的 TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。
参数
| context | 一个 Android ERROR(/Context)。 |
|---|---|
| modelPath | 指向包含资源中元数据的文本模型的路径。 |
抛出
如果 TextEmbedder 创建期间出现错误,则会发生该错误。
|
public static TextEmbedder createFromFile (Context context、File modelFile)
使用模型文件和默认的 TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。
参数
| context | 一个 Android ERROR(/Context)。 |
|---|---|
| modelFile | 文本模型 File 实例。 |
抛出
| IOException | 如果在打开 tflite 模型文件时发生 I/O 错误,则会发生该错误。 |
|---|---|
如果 TextEmbedder 创建过程中出错,则会发生该错误。 |
public static TextEmbedder createFromOptions (上下文上下文、TextEmbedder.TextEmbedderOptions 选项)
通过 TextEmbedder.TextEmbedderOptions 创建 TextEmbedder 实例。
参数
| context | 一个 Android ERROR(/Context)。 |
|---|---|
| 选项 | 一个 TextEmbedder.TextEmbedderOptions 实例。 |
抛出
如果 TextEmbedder 创建过程中出错,则会发生该错误。 |