python - 新的 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 库/框架中。
解决方案
我建议您制作自己的模块,以您想要的方式导入和扩展 tensorflow,而不是更改源代码。例如
import tensorflow as tf
import mytf
然后你可以做
mytf.layers.custom_matmul
你甚至可以在 mytf 中将你自己的属性暴露给 tensorflow。我不建议这样做,因为它会让任何阅读您的代码的人(包括将来可能包括您自己)感到困惑,但是您可以
tf.layers.custom_matmul = MyCustomMatMul()
然后,您可以按照问题中的描述使用 tf.layers.custom_matmul 。再次,不推荐。
推荐阅读
- android - 使用带有通知的 workmanager 时延迟太长
- python - 如何保留我不想分组的数据框的某些字段?
- powershell - 在 Docker 构建过程中打印一些东西
- sapui5 - 我可以将选择对话框中的“确定”按钮样式更改为强调吗?
- load-balancing - HAProxy ACL 白名单 IPs CIDR 表示法
- python - AttributeError:“区域”对象没有属性“半径”
- web-services - 从 Salesforce 调用 Web 服务的设计问题
- reactjs - 做出反应。弗罗米克。Material-ui 表单提交事件测试失败使用 react-testing-library
- reactjs - 如何将起始值传递给输入?
- sql - 是否有一种解决方法可以避免在 sql 中使用 TRUNC(Date) 以从索引中受益?