{"id":213,"date":"2016-11-20T23:05:56","date_gmt":"2016-11-20T14:05:56","guid":{"rendered":"http:\/\/www.cl.cs.okayama-u.ac.jp\/?p=213"},"modified":"2017-03-28T23:08:42","modified_gmt":"2017-03-28T14:08:42","slug":"how-to-see-the-values-of-tensorarray","status":"publish","type":"post","link":"https:\/\/www.cl.cs.okayama-u.ac.jp\/?p=213","title":{"rendered":"How to see the values of TensorArray (2016\/11\/20)"},"content":{"rendered":"<p>I roughly understand the relations between TensorArray and Tensor in Tensorflow 0.80, then I would write my memos and codes. TensorArray is a special types of tensor that can modify its value in while_loop, and we cannot apply Session.run to an instance of TensorArray. This means that we cannot see the values in TensorArray. Thus we need to convert TensorArray to Tensor that Session.run can be applied to. The conversion is enable by TensorArray.pack(). A simple example of using TensorArray code is here.<\/p>\n<pre>import tensorflow as tf<br \/>\r\nimport numpy as np<br \/>\r\nfrom tensorflow.python.ops import tensor_array_ops<br \/>\r\n#Simple example of seeing values in TensorArray<br \/>\r\n# just adding [2.4, 3.5] to TensorArray three times<br \/>\r\n<br \/>def body(time,output_ta_l):<br \/>     output_ta_l = output_ta_l.write(time, [2.4,3.5])<br \/>     return time+1, output_ta_l<br \/><br \/>def condition(time,output_ta_l):<br \/>     return tf.less(time, 3)<br \/><br \/>time = tf.constant(0)<br \/>output_ta = tensor_array_ops.TensorArray(tf.float32, size=1, dynamic_size=True)<br \/><br \/>result = tf.while_loop(condition, body, [time,output_ta])<br \/>ltime, lout = result<br \/>final_out = lout.pack()<br \/>with tf.Session():<br \/>   tf.initialize_all_variables().run()<br \/>   print(ltime.eval())<br \/>   print(\"final_out==\",final_out.eval())<\/pre>\n<p>You can get the result below.<\/p>\n<pre>3<br \/>final_out== [[ 2.4000001  3.5      ]<br \/> [ 2.4000001  3.5      ]<br \/> [ 2.4000001  3.5      ]]<br \/><\/pre>\n<p>So if you convert TensorArray to Tensor with Tensor.Array.pack(), you can see the values with print.<\/p>\n<p>Reference:<\/p>\n<p><a class=\"external-link\" href=\"http:\/\/stackoverflow.com\/questions\/40339510\/tensorarray-initialization\/40349551\">http:\/\/stackoverflow.com\/questions\/40339510\/tensorarray-initialization\/40349551<\/a><\/p>\n<p><a class=\"external-link\" href=\"http:\/\/stackoverflow.com\/questions\/37441140\/how-to-use-tf-while-loop-in-tensorflow\">http:\/\/stackoverflow.com\/questions\/37441140\/how-to-use-tf-while-loop-in-tensorflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I roughly understand the relations between TensorArray and Tensor in Tensorflow 0.80, then I would write my me &hellip; <a href=\"https:\/\/www.cl.cs.okayama-u.ac.jp\/?p=213\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":2,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}