Python 如何在Tensorflow中实现元素一维插值?

Python 如何在Tensorflow中实现元素一维插值?,python,tensorflow,interpolation,Python,Tensorflow,Interpolation,我想对Tensorflow中张量的每个元素应用1D插值 例如,如果它是一个矩阵,我们可以使用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(

我想对Tensorflow中张量的每个元素应用1D插值

例如,如果它是一个矩阵,我们可以使用
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)
这不是最好的解决办法。希望tensor flow将在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
现在您可以这样使用它:

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))

您将获得期望的结果…

您是否发现一些改进?
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))