gpt4 book ai didi

python - 如何在 Tensorflow 中实现逐元素一维插值?

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:31 25 4
gpt4 key购买 nike

我想对 Tensorflow 中张量的每个元素应用一维插值。

例如,如果是矩阵,我们可以使用interp1d

from scipy.interpolate import interp1d
q = np.array([[2, 3], [5, 6]]) # query
x = [1, 3, 5, 7, 9] # profile x
y = [3, 4, 5, 6, 7] # profile y
fn = interp1d(x, y)
# fn(q) == [[ 3.5, 4.], [5., 5.5]]

如果我们有一个张量q

q = tf.placeholder(shape=[2,2], dtype=tf.float32)

如何获得等效的逐元素一维插值?谁能帮忙?

最佳答案

我正在为此使用包装器:

import numpy as np
import tensorflow as tf
from scipy.interpolate import interp1d


x = [1, 3, 5, 7, 9]
y = [3, 4, 5, 6, 7]
intFn = interp1d(x, y)

def fn(m):
return intFn(m).astype(np.float32)

q = tf.placeholder(shape=[2,2], dtype=tf.float32)
q1 = np.array([[2, 3], [5, 6]]).astype(np.float32)

f1 = tf.py_func(fn, [q], tf.float32)

with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
result = sess.run(f1, feed_dict={q:q1})

print(result)

不是最好的解决方案。希望 tensorflow 将在 numpy 和 scipy 中实现更多功能 ...

编辑:

我编写了一个可能有用的简单 tensorflow 函数。不幸的是,这一次只会执行一个值。然而,如果它很有趣,这可能是可以改进的东西......

def interpolate( dx_T, dy_T, x, name='interpolate' ):

with tf.variable_scope(name):

with tf.variable_scope('neighbors'):

delVals = dx_T - x
ind_1 = tf.argmax(tf.sign( delVals ))
ind_0 = ind_1 - 1

with tf.variable_scope('calculation'):

value = tf.cond( x[0] <= dx_T[0],
lambda : dy_T[:1],
lambda : tf.cond(
x[0] >= dx_T[-1],
lambda : dy_T[-1:],
lambda : (dy_T[ind_0] + \
(dy_T[ind_1] - dy_T[ind_0]) \
*(x-dx_T[ind_0])/ \
(dx_T[ind_1]-dx_T[ind_0]))
))

result = tf.multiply(value[0], 1, name='y')

return result

给定几个张量,这将创建一个合成张量。这是一个示例实现。首先创建一个图...

tf.reset_default_graph()
with tf.variable_scope('inputs'):
dx_T = tf.placeholder(dtype=tf.float32, shape=(None,), name='dx')
dy_T = tf.placeholder(dtype=tf.float32, shape=(None,), name='dy')
x_T = tf.placeholder(dtype=tf.float32, shape=(1,), name='inpValue')

y_T = interpolate( dx_T, dy_T, x_T, name='interpolate' )
init = tf.global_variables_initializer()

现在你可以像这样使用它了:

x = [1, 3, 5, 7, 9]              # profile x
y = [3, 4, 5, 6, 7] # profile y
q = np.array([[2, 3], [5, 6]])

with tf.Session() as sess:
sess.run(init)

for i in q.flatten():
result = sess.run(y_T,
feed_dict={
'inputs/dx:0' : x,
'inputs/dy:0' : y,
'inputs/inpValue:0' : np.array([i])
})

print('{:6.3f} -> {}'.format(i, result))

你会得到想要的结果......

关于python - 如何在 Tensorflow 中实现逐元素一维插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com