Tensorflow array value assignment

799 views
Skip to first unread message

chaitanya ganesh

unread,
Oct 31, 2016, 12:05:42 AM10/31/16
to Discuss
I am writing a program to distribute the tensorflow task into 4 nodes. My program consists of assigning values to a simple numpy array. Then I convert them into tensorflow arrays to do a basic arithmetic addition operation. I store the outputs in another tensorflowarray. Finally convert this array into tensor to print it in the output. 

import tensorflow as tf
import numpy as np
import datetime

u1 = tf.TensorArray(dtype=tf.float64,size=101)
u2 = tf.TensorArray(dtype=tf.float64,size=101)
u3 = tf.TensorArray(dtype=tf.float64,size=101)
u4 = tf.TensorArray(dtype=tf.float64,size=101)
u5 = tf.Variable(np.zeros(101))
u = np.zeros(101)
a = tf.Variable(0, dtype=tf.float64)
L = 100.0

x = tf.constant(2, dtype=tf.float64)

t1 = datetime.datetime.now()
with tf.device("/job:local/task:3"):

for j in range(101):
# u[j] = (np.sin(2*np.pi*j/L))
u[j] = j
u1 = u1.write(j,u[j])
u2 = u2.write(j,u[j])
u3 = u3.write(j,u1.read(j)+u2.read(j))
u4 = u3.pack()
assign = tf.assign(u5, u3(100))


model = tf.initialize_all_variables()

with tf.Session("grpc://localhost:2222") as sess:
sess.run(model)
print sess.run(u4)
print sess.run(assign)
# print (u4.eval())
t2 = datetime.datetime.now()

print('\n')
print "Multi node computation time: " + str(t2-t1)


I want to store the last variable of u3 tensorarray/u4 tensor into the first element of another tensorarray of size 101. The above written program gives the following error:-

    assign = tf.assign(u5, u3(100))
TypeError: 'TensorArray' object is not callable

Please help.

Eugene Brevdo

unread,
Oct 31, 2016, 12:17:32 AM10/31/16
to chaitanya ganesh, Discuss
While I'm not sure you're using TensorArrays in their intended way (i.e., there may be significantly faster ways to do what you want), I think the syntax error you're seeing here is that you should call u3.read(100) instead of u3(100).

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/eb22e645-ba62-470b-ae1f-b797250aff3a%40tensorflow.org.

chaitanya ganesh

unread,
Nov 1, 2016, 1:43:26 AM11/1/16
to Discuss
I have made a few improvements to my code and it's significantly faster and serves the purpose of learning the basic behavior of tf variables.

import tensorflow as tf
import numpy as np
import datetime

u1 = tf.Variable(0, dtype=tf.float64)
u2 = tf.Variable(0, dtype=tf.float64)
u = np.zeros(101)
a = tf.Variable(0, dtype=tf.float64)
L = 100.0
t1 = datetime.datetime.now()

for i in range(2):
for j in range(101):
u[j] = np.sin(2*np.pi*j/L)
u1 = tf.scan(lambda a, u: u + u, u)
u2 = u1[-1]
model = tf.initialize_all_variables()

with tf.Session() as sess:
sess.run(model)
print sess.run(u1)
print sess.run(u2)           
t2 = datetime.datetime.now()

print('\n')
print "Multi node computation time: " + str(t2-t1)

Please suggest any improvements if possible.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.

Eugene Brevdo

unread,
Nov 1, 2016, 12:17:19 PM11/1/16
to chaitanya ganesh, Discuss
running u2 separately from u1 forces a rerun of u1 and any other dependencies to calculate u2.  instead do:

print sesison.run([u1, u2])

to avoid 2x the calculations.

also:

remove these lines:

u1 = tf.Variable(0, dtype=tf.float64)
u2 = tf.Variable(0, dtype=tf.float64)
u = np.zeros(101)
a = tf.Variable(0, dtype=tf.float64)

and change this line:

for j in range(101):
u[j] = np.sin(2*np.pi*j/L)

to:

u = tf.sin(2*np.pi*np.arange(0, 101)/L)

finally, when you time things, do an initial session.run to "burn in" the calculation, and then only time on multiple iterations of repeated session.run calls.



To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

chaitanya ganesh

unread,
Nov 2, 2016, 2:30:55 AM11/2/16
to Discuss
Thanks a lot for the advice. I have made the changes. This has greatly decreased the complexity and clutter in the code. Now, I am trying to use the scan function but I am stuck at an indexing problem. 

In simple python, I am trying to do the following operation:-

for i in range(1,25):
     u [i] = uold [i] - K * ( uold [i] - uold [i-1] )

In tensorflow, I am encountering an indexing issue due to "(uold1[i]-uold1[i-1])". Currently I have written the statement as:-

     u = tf.map_fn ( lambda u: uold - K * ( uold - uold ), uold )

In the current equation the second term is always zero. I am not sure how to change it to get the desired output.  
Reply all
Reply to author
Forward
0 new messages