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)
I am keep getting error of:
ValueError: Shape must be rank 0 but is rank 1 for 'while_50/cond/Switch'
(op: 'Switch') with input shapes: [1], [1].
Can someone help me with this error? I am trying to get this tensor-flow "while" loop work.
Basically I am try to do a greedy 1 to 1 match array "myx" with "myy" using tensor-flow framework.
The tf.cond(pred, true_fn, false_fn)
function requires that pred
be a scalar ("rank 0") tensor. In your program, it is a vector ("rank 1") tensor of length 1.
There are many ways to fix this. For example, you can use tf.reduce_min()
without specifying an axis to compute the global minimum of tfslic
as a scalar:
minVal = tf.reduce_min(tfslic)
...or you can explicitly use tf.reshape()
to make the argument to tf.cond()
scalar:
y = tf.cond(tf.less_equal(tf.reshape(minVal, []), 1), ...)
I took the liberty of modifying your program slightly to get a version that works. Follow the comments to see where changes were necessary:
with tf.device('/cpu:0'):
dist = tf.abs(tf.subtract(X,Y))
# Use an explicit shape for `i`.
i = tf.placeholder(dtype='int32', shape=[])
# Add a second unused argument to `condition()`.
def condition(i, _):
return i < 2
# Add a second unused argument to `b()`.
def b(i, _):
dist = tf.abs(tf.subtract(X,Y))
# Could use `tfslic = dist[0:myy.size, i]` here to avoid later reshapes.
tfslic = tf.slice(dist, [0,i], [myy.size,1])
# Drop the `axis` argument from `tf.reduce_min()`
minVal=tf.reduce_min(tfslic)
y = tf.cond(
tf.less_equal(minVal, 1),
# Reshape the output of `tf.argmin()` to be a scalar.
lambda: tf.reshape(tf.argmin(tfslic, 0), []),
# Explicitly convert the false-branch value to `tf.int64`.
lambda: tf.constant(99999, dtype=tf.int64))
return i+1, y
# 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 = tf.while_loop(condition, b, [i, tf.constant(0, dtype=tf.int64)])
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Fetch the value of `i_out`.
dmat = sess.run(i_out, feed_dict={X:Xxx, Y: Yyy, i:0})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.