Safetensors 文档

Tensorflow API

您正在查看 main 版本,该版本需要从源码安装。如果您想要常规的 pip 安装,请查看最新的稳定版本 (v0.5.0-rc.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Tensorflow API

safetensors.tensorflow.load_file

< >

( filename: typing.Union[str, os.PathLike] ) Dict[str, tf.Tensor]

参数

  • filename (str, 或 os.PathLike)) — 包含张量的文件名

返回值

Dict[str, tf.Tensor]

包含名称作为键,值作为 tf.Tensor 的字典

将 safetensors 文件加载为 tensorflow 格式。

示例

from safetensors.tensorflow import load_file

file_path = "./my_folder/bert.safetensors"
loaded = load_file(file_path)

safetensors.tensorflow.load

< >

( data: bytes ) Dict[str, tf.Tensor]

参数

  • data (bytes) — safetensors 文件的内容

返回值

Dict[str, tf.Tensor]

包含名称作为键,值作为 CPU 上的 tf.Tensor 的字典

从纯字节加载 safetensors 文件为 tensorflow 格式。

示例

from safetensors.tensorflow import load

file_path = "./my_folder/bert.safetensors"
with open(file_path, "rb") as f:
    data = f.read()

loaded = load(data)

safetensors.tensorflow.save_file

< >

( tensors: typing.Dict[str, tensorflow.python.framework.tensor.Tensor] filename: typing.Union[str, os.PathLike] metadata: typing.Optional[typing.Dict[str, str]] = None ) None

参数

  • tensors (Dict[str, tf.Tensor]) — 输入张量。张量需要是连续且密集的。
  • filename (str, 或 os.PathLike)) — 我们要保存到的文件名。
  • metadata (Dict[str, str], 可选,默认为 None) — 您可能想要保存在标头中的可选纯文本元数据。例如,它可以用于更详细地说明底层张量。这纯粹是信息性的,不会影响张量的加载。

返回值

None

将张量字典保存为 safetensors 格式的原始字节。

示例

from safetensors.tensorflow import save_file
import tensorflow as tf

tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
save_file(tensors, "model.safetensors")

safetensors.tensorflow.save

< >

( tensors: typing.Dict[str, tensorflow.python.framework.tensor.Tensor] metadata: typing.Optional[typing.Dict[str, str]] = None ) bytes

参数

  • tensors (Dict[str, tf.Tensor]) — 输入张量。张量需要是连续且密集的。
  • metadata (Dict[str, str], 可选,默认为 None) — 您可能想要保存在标头中的可选纯文本元数据。例如,它可以用于更详细地说明底层张量。这纯粹是信息性的,不会影响张量的加载。

返回值

bytes

表示格式的原始字节

将张量字典保存为 safetensors 格式的原始字节。

示例

from safetensors.tensorflow import save
import tensorflow as tf

tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
byte_data = save(tensors)
< > 在 GitHub 上更新