简体   繁体   English

如何将tensorflow占位符变量转换为numpy数组?

[英]How to convert tensorflow placerholder variable to numpy array?

I would like to use scipy interpolation function in the tensorflow code. 我想在张量流代码中使用scipy插值函数。

Here is the example snippet similar to my situation. 这是与我的情况类似的示例代码段。

import tensorflow as tf
from scipy import interpolate

def interpolate1D(Xval,Fval,inp): 
    Xval = np.array(Xval)
    Fval = np.array(Fval)
    f = interpolate.interp1d(Xval, Fval, fill_value="extrapolate")
    z = f(inp)
    return z

properties = {
    'xval': [200,400,600,800,1100],
    'fval': [100.0,121.6,136.2,155.3,171.0]
}

tensor = tf.placeholder("float")

interpolate = interpolate1D(properties['xval'],properties['fval'], tensor)


Once I get the interpolate I'll convert it into tensor using tf.convert_to_tensor(interpolate) 一旦获得interpolate我将使用tf.convert_to_tensor(interpolate)将其转换为张量

Here interpolate.interp1d is just an example. 这里interpolate.interp1d只是一个例子。 I'll be using other interpolation methods and output of those methods will be fed into another neuron. 我将使用其他插值方法,这些方法的输出将输入到另一个神经元中。

I understand placeholder is empty variable so technically it's not possible to convert into numpy array. 我知道placeholder是空变量,因此从技术上讲,它不可能转换为numpy数组。 Also, I cannot use this interpolation function outside the tensorflow graph because in some situations I need to use output of a neural network as a input to interpolation function. 另外,我无法在张量流图之外使用此插值函数,因为在某些情况下,我需要使用神经网络的输出作为插值函数的输入。

Overall, I would like to use scipy interpolation function with in the tensor graph. 总的来说,我想在张量图中使用scipy插值函数。

You could use tf.py_func to use the SciPy function inside your graph, but a better option would be to implement the interpolation in TensorFlow. 您可以使用tf.py_func在图形中使用SciPy函数,但是更好的选择是在TensorFlow中实现插值。 There is no function in the library that does this out of the box, but it is not difficult to implement it. 库中没有开箱即用的功能,但是实现它并不困难。

import tensorflow as tf

# Assumes Xval is sorted
def interpolate1D(Xval, Fval, inp):
    # Make sure input values are tensors
    Xval = tf.convert_to_tensor(Xval)
    Fval = tf.convert_to_tensor(Fval)
    inp = tf.convert_to_tensor(inp)
    # Find the interpolation indices
    c = tf.count_nonzero(tf.expand_dims(inp, axis=-1) >= Xval, axis=-1)
    idx0 = tf.maximum(c - 1, 0)
    idx1 = tf.minimum(c, tf.size(Xval, out_type=c.dtype) - 1)
    # Get interpolation X and Y values
    x0 = tf.gather(Xval, idx0)
    x1 = tf.gather(Xval, idx1)
    f0 = tf.gather(Fval, idx0)
    f1 = tf.gather(Fval, idx1)
    # Compute interpolation coefficient
    x_diff = x1 - x0
    alpha = (inp - x0) / tf.where(x_diff > 0, x_diff, tf.ones_like(x_diff))
    alpha = tf.clip_by_value(alpha, 0, 1)
    # Compute interpolation
    return f0 * (1 - alpha) + f1 * alpha

properties = {
    'xval': [200.0, 400.0, 600.0, 800.0, 1100.0],
    'fval': [100.0, 121.6, 136.2, 155.3, 171.0]
}

with tf.Graph().as_default(), tf.Session() as sess:
    tensor = tf.placeholder("float")
    interpolate = interpolate1D(properties['xval'], properties['fval'], tensor)
    print(sess.run(interpolate, feed_dict={tensor: [40.0, 530.0, 800.0, 1200.0]}))
    # [100.   131.09 155.3  171.  ]

The expected answer for this question is the use of tf.py_func and thanks to @jdehesa for mentioning that. 这个问题的预期答案是使用tf.py_func并感谢@jdehesa提到了这一点。

For users, who are expecting the solution for this question is mentioned below 对于期望该问题解决方案的用户,请参见下文

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

properties = {
    'xval': [200,400,600,800,1100],
    'fval': [100.0,121.6,136.2,155.3,171.0]
}

Xval = np.array(properties['xval'])
Fval = np.array(properties['fval'])
f = interpolate.interp1d(Xval, Fval, fill_value="extrapolate")

def comp(x):
    return np.float32(f(x))

tensor = tf.placeholder("float32")
interpolate = tf.py_func(comp, [tensor], tf.float32)

with tf.Session() as sess:
    print(sess.run(interpolate, feed_dict = {tensor: [230]}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM