Safetensors 文档

Tensorflow API

您正在查看的是需要从源码安装。如果您想通过 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 (stros.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 (stros.PathLike) — 我们要保存到的文件名。
  • metadata (Dict[str, str], 可选, 默认为 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) — 您可能想要保存在文件头中的可选纯文本元数据。例如,它可以用于指定有关底层张量的更多信息。这纯粹是信息性的,不影响张量加载。

返回

字节

代表该格式的原始字节

将张量字典以 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 上更新