首页 > 解决方案 > 新的 TensorFlow 库

问题描述

如何制作新的 tensorflow 库?例如我想写一些张量流代码,矩阵乘法:

# File custom_matmul.py

import tensorflow as tf

matrixA = ...
matrixB = ...

matrixC = tf.matmul(...)

在另一个 python 文件中,我想做这样的事情:

import tensorflow as tf

tf.custom_matmul...

或者使库成为库层的一部分

tf.layers.custom_matmul

更简单的是,我想制作自己的库并将其集成到标准 tensorflow 库/框架中。

标签: pythontensorflow

解决方案


我建议您制作自己的模块,以您想要的方式导入和扩展 tensorflow,而不是更改源代码。例如

import tensorflow as tf
import mytf

然后你可以做

mytf.layers.custom_matmul

你甚至可以在 mytf 中将你自己的属性暴露给 tensorflow。我不建议这样做,因为它会让任何阅读您的代码的人(包括将来可能包括您自己)感到困惑,但是您可以

tf.layers.custom_matmul = MyCustomMatMul()

然后,您可以按照问题中的描述使用 tf.layers.custom_matmul 。再次,不推荐。


推荐阅读