0

I'm trying to convert the output of my python script that uses Tensorflow so that it can be passed as an input string to another file.

The output I get in the console is the following:

('Input from genesis: ', array([  7.07872450e-01,   3.32354023e+04,   5.85043602e+01,
         1.91101468e+00,   3.83828156e+05,   3.76164818e+01,
         2.11525035e+00,   3.73708814e-01,   9.32167812e+04], dtype=float32))
('Output from genesis: ', '[array([ 0.66993088,  1.        ,  1.        ,  0.87113315,  1.        ,\n        1.        ,  0.8923766 ,  0.59235483,  1.        ], dtype=float32)]')
Execution time: 2.57894515991 seconds

Is there a relatively easy way to format the array output as a string of the array? i.e. [ 1, 2, 3, 4]?

Bare in mind this is how it is inputted with the use of tf.Variable:

a = tf.Variable(tf.constant([random.uniform(0.,3.),random.uniform(0.,400000.),random.uniform(0.,128.),random.uniform(0.,3.),random.uniform(0.,400000.),random.uniform(0.,128.),random.uniform(0.,3.),random.uniform(0.,3.),random.uniform(0.,400000.)], dtype=tf.float32))

Cheers.

1 Answer 1

2

Tensorflow is returning your results as a numpy array. Your question is simply, "how do I save a numpy array as a string." To which a few options exist. The top two answers on the google search below are numpy.savetxt and numpy.array_str, both sound like simple enough solutions to your problem. Following those two entries are a few stack overflow articles that go into more detail on converting numpy arrays to strings. I suspect you'll find everything you need there.

https://www.google.com/search?q=save+numpy+array+as+string&oq=save+numpy+array+as+string

Save & Retrieve Numpy Array From String

Sign up to request clarification or add additional context in comments.

3 Comments

Hi David, I'm not using numpy for this. The output is a tensor of shape [1,9]
The output of a call to sess.run is a numpy array. Tensors are objects only within the tensorflow environment. Did you call sess.run? If not then tensorflow did no computation for you. You must call sess.run to ask tensorflow to do a computation for you. If you have a tensor you only have an object that defines part of a computation graph. You have a meta-definition, not a value. result = sess.run(mytensor) will give you the value of that tensor.
As you can tell I'm fresh on the scene with tensorflow (and python in general haha) I just tried numpy.savetxt and it worked a treat dude. Thanks for your help, to my first question as well!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.