1

I read a data from file txt which contain info about location like this: "... Location: tensor([13., 16.]) Location: tensor([11., 1.]) ... " My question is how to convert it in real tensor to make a plot. I am reading data like this:

for line in file:
     M_l = re.search(r"Location: (.*)", line)
     location = M_l.group(1)
     plt.plot(location, 'r+')
Kermit
  • 4,922
  • 4
  • 42
  • 74
  • Possible duplicate of https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python – ForceBru Aug 26 '19 at 15:00

1 Answers1

0

Try the following snippet. You have to extract the actual lists (which are between parenthesis) that corresponds to your tensors, which are in string format. So convert this string list to python list using ast. Then use tf.convert_to_tensor to convert to a tensor (only if necessary?). If you do want to convert to a tensor, then you have to run the tensor inside a session and get the value and plot. If you do not require to generate a tensor, you can just plot the list without using tensorflow sessions.

import tensorflow as tf
import re
import ast


file = open("test.txt","r")
for line in file:
    M_l = re.search(r"Location: (.*)", line)
    location = M_l.group(1)
    location=ast.literal_eval(re.search('\(([^)]+)', location).group(1))

    l=tf.convert_to_tensor(location, dtype=tf.float32)

    with tf.Session() as sess:

        plt.plot(sess.run(l), 'r+')
Kermit
  • 4,922
  • 4
  • 42
  • 74
Achintha Ihalage
  • 2,310
  • 4
  • 20
  • 33