python - Tensorflow 张量中的自定义函数
问题描述
假设我想通过以下方式在afunction
上应用:tensorflow tensor
|2 7 4| |f(2) f(7) f(4)|
|6 7 2| ---> |f(6) f(7) f(2)| ---> with f(x)=2+e^x
|6 8 8| |f(6) f(8) f(8)|
我如何才能在 tensorflow 层中实现这一点,在 GPU 支持下,并行化能够获得所需的速度。一种方法tf.py_func
很简单,使用 numpy,但速度很慢。我更喜欢一个完全tensorflow
基于的解决方案 --> tf GPU speedup
。
解决方案
我想我可以自己回答这个问题,我的函数如下所示:
Function of interest: f(a1,a2,a3,x)=a1+a2*e^(x/a3)
对应的tensorflow代码:
import tensorflow as tf
# Input data
img = tf.reshape(tf.range(5*5*1, dtype=tf.float32), [1, 5, 5, 1])
a1=2.5
a2=10
a3=75
img=tf.math.divide(img,a3)
img=tf.math.exp(img)
img=tf.math.multiply(img,a2)
img=tf.math.add(img,a1)
检查结果:
print(img)
推荐阅读
- kaizala - 删除 Kaizala 组而不丢失 Kaizala 管理员管理门户中的作业
- schema - 关系代数的除法运算
- scala - 元组scala的不可变迭代器映射
- python - Pandas:查找由开始列和停止列定义的多个范围内的值
- mysql - 我想优化 mysql 查询
- git - 在不影响安全的情况下设置 git 代理
- jhipster - 在具有 OAUTH2 基础安全性的单片 Jhipster APP 中公开 api
- javascript - 在 App.get 我想有两个不同的响应(渠道)
- python - 烧瓶记录到文件 - 在本地工作但不在服务器上
- installation - Librato 安装失败并显示“错误:读取配置文件失败!”