|
#i=0
#while(i<10) : i=i+1
#i = tf.constant(0)
#c = lambda i: tf.less(i, 10)
#b = lambda i: tf.add(i, 1)
#r = tf.while_loop(c, b, [i])
def sum(l=10) :
#i=0; sum=0
#while(i<10): i+=1; sum +=i
#print(sum)
i = tf.constant(0)
sum1 = tf.constant(0)
def c(sum,i) :
return tf.less(i, l)
def b(sum,i) :
i =tf.add(i,1)
sum =tf.add(sum,i)
return [sum,i]
sum1,i = tf.while_loop(c, b, [sum1,i])
return sum1
#s=sum(100)
#with tf.Session() as sess: print(sess.run(s))
nts=np.array([0,0,0,1,1,1,2,2,2,3,3,3])
nps=np.array([0,2,1,0,1,1,2,0,2,1,2,3])
labels=['ang','hap','neu','sad']
N=len(labels)
ts=tf.constant(nts)
ps=tf.constant(nps)
S=ts.get_shape()[0]
i=tf.constant(0)
cm = tf.Variable(tf.zeros(shape=(N,N)))
def c(cm,ts,ps,i) :
return tf.less(i,S)
def b(cm,ts,ps,i) :
t=ts[i]; p=ps[i];
m= tf.add(cm[t,p],1)
tf.assign(cm[t,p],m)
i =tf.add(i,1)
return [cm,ts,ps,i]
res = tf.while_loop(c, b, [cm,ts,ps,i])
with tf.Session() as sess: print(sess.run(res))
i=tf.constant(0)
while_condition = lambda i: tf.less(i, input_placeholder[1, 1])
def body(i):
# do something here which you want to do in your loop
# increment i
return [tf.add(i, 1)]
# do the loop:
r = tf.while_loop(while_condition, body, [i])
- import tensorflow as tf
data=tf.placeholder(tf.float32, shape=[2, 3])
def cond(data, output, i):
return tf.less(i, tf.shape(data)[0]) #until batch size
def body(data, output, i):
output = output.write(i, tf.add(data[i], 10))
return data, output, i + 1
# TensorArray is a data structure that support dynamic writing
output_ta = tf.TensorArray(dtype=tf.float32,
size=0,
dynamic_size=True,
element_shape=(data.get_shape()[1],)) #data sample shape
_, output_op, _ = tf.while_loop(cond, body, [data, output_ta, 0])
output_op = output_op.stack()
with tf.Session() as sess:
print(sess.run([data,output_op], feed_dict={data: [[1, 2, 3], [0, 0, 0]]}))
[array([[1., 2., 3.],
[0., 0., 0.]], dtype=float32),
array([[11., 12., 13.],
[10., 10., 10.]], dtype=float32)]
"""
|
|