import math
import numpy as np
import tensorflow as tf
myx=np.array([2,4,5])
myy=np.array([10,3,7,8,6,4,11,18,1])
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))
calp=tf.constant(1)
with tf.device('/cpu:0'):
#minCord=tf.argmin(tfslic,0)
dist = tf.abs(tf.subtract(X,Y))
i = tf.placeholder(dtype='int32')
def condition(i):
return i < 2
def b(i):
dist = tf.abs(tf.subtract(X,Y))
tfslic=tf.slice(dist,[0,i],[myy.size,1])
minVal=tf.reduce_min(tfslic,0)
y = tf.cond(tf.less_equal(minVal, 1), lambda: tf.argmin(tfslic,0), lambda: 99999)
return i+1, y
i, r = tf.while_loop(condition, b, [i])
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
dmat=sess.run(i, feed_dict={X:Xxx, Y: Yyy, i:0})
sess.close()
print(dmat)
我一直收到以下错误:
^{pr2}$有人能帮我解决这个错误吗?我试着让这个张量流“while”循环起作用。在
基本上,我尝试用张量流框架来做一个贪婪的1:1匹配数组“myx”和“myy”。在
^{} 函数要求
pred
是标量(“秩0”)张量。在程序中,它是长度为1的向量(“秩1”)张量。在有很多方法可以解决这个问题。例如,可以使用
tf.reduce_min()
而不指定轴来计算tfslic
的全局最小值作为标量:…或者可以显式使用
^{pr2}$tf.reshape()
将参数设为tf.cond()
标量:我冒昧地稍微修改了一下你的程序,得到了一个有效的版本。按照注释查看哪些地方需要更改:
相关问题 更多 >
编程相关推荐