myx=np.array([2.4,0.2,0.5,1.6])
myy=np.array([10.1,3.2,7.5,8.6,1,0.1,11,18])
我想写一个程序,用贪婪算法把“myx”和“myy”的每个元素匹配起来。匹配将为1:1,卡钳距离<;=0.5,无需更换。 算法很简单:
^{pr2}$问题是我不想在“np”或“panda”中使用任何东西,而只想使用Tensorflow框架。我已经写了程序的一部分,但发现它比我原来想的要难。有人能帮我吗?顺便说一下,这是我第一次尝试使用tensorflow。。在
import math
import numpy as np
import tensorflow as tf
#myx = np.random.uniform(low=0.5, high=3.5, size=(5000,))
#myy=np.random.uniform(low=0.8, high=5.0, size=(50000,))
myx=np.array([2.4,0.2,0.5,1.6])
myy=np.array([10.1,3.2,7.5,8.6,1,0.1,11,18])
Xxx=np.transpose(np.repeat(myx[:, np.newaxis], myy.size , axis=1))
Yyy=np.repeat(myy[:, np.newaxis], myx.size , axis=1)
X = tf.placeholder(tf.float64, shape=(myy.size,myx.size))
Y = tf.placeholder(tf.float64, shape=(myy.size,myx.size))
# define a caliper
calp=tf.constant(0.5,tf.float64)
with tf.device('/cpu:0'):
dist = tf.abs(tf.subtract(X,Y))
# Use an explicit shape for `i`.
i = tf.placeholder(dtype='int64', shape=[])
# Add a second unused argument to `condition()`.
def condition(i, *arg):
return i <= myx.size-1
# Add a second unused argument to `b()`.
def b(i, temp, _):
tfslic = dist[0:myy.size, i]
# Drop the `axis` argument from `tf.reduce_min()`
minVal=tf.reduce_min(tfslic)
y = tf.cond(
tf.less_equal(minVal, calp),
# Reshape the output of `tf.argmin()` to be a scalar.
lambda: tf.argmin(tfslic, 0),
# Explicitly convert the false-branch value to `tf.int64`.
lambda: tf.constant(99999, dtype=tf.int64))
'''
:::::::::::::PROBLEM START HERE:::::::::::
For each tf.while_loop, with index "i"
if the minimum value of distance matrix dist[,i] <= caliper
then output the first min value index occurs i.e. (y,i)
Then set dist[y,]=[None, None, None, None]
Given the example matix "myx","myy";
The while loop should output match pair indx [[0,None],[1,5],[2,4],[3,None]]
'''
varDist=tf.Variable(dist)
temp = tf.cond(
tf.less_equal(minVal, calp),
# Set dist matrix row y to [None, None, None, None].
lambda: tf.assign(varDist[y,],[9999.,9999.,9999.,9999.]),
# Do nothing.
lambda: tf.Variable(dist))
return i+1, y, temp
# Add a dummy initial value for the second loop variable.
# Rename the first return value to `i_out` to avoid clashing with `i` above.
i_out, r, dist= tf.while_loop(condition, b, [i, dist, tf.constant(0, dtype=tf.int64)])
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
dmat = sess.run(dist, feed_dict={X:Xxx, Y: Yyy,i:0})
sess.close()
print(dmat)
让我重新表述一下这个问题,看看我是否理解正确。问题是,对于
myx
中的每个元素x
,找到myy
中最接近x
的元素,如果距离小于卡尺,则将其匹配。在匹配结果也可以表示为一个列表(长度与}中的索引,如果最小距离大于卡尺。在
myx
)相同,该列表中的每个元素都是myy
或{如果您同意上面的重新表述,那么有一种解决问题的等效方法,如函数
npstyle()
所示,可以很容易地转换为tf_style()
。在相关问题 更多 >
编程相关推荐