How to extract a value from scalar tensor?

697 views
Skip to first unread message

ALI Q SAEED

unread,
Nov 23, 2021, 6:24:18 PM11/23/21
to Keras-users
Hi there,
if i have a scalar tensor t1 which is as follow :
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.23798278], dtype=float32)>

How could i extract the value (0.23798278) as a regular float variable without using .numpy or eval(), because .numpy is a pythonic function and not supported by tensorflow during training. and eval() requires session to be executed which in turn requires disabling eager execution as i am using tf 2.x.

Please, any help is appreciated.
Thanks.

Ukjae Jeong

unread,
Nov 23, 2021, 10:15:08 PM11/23/21
to Keras-users
Hi,

If you want to use that tensor as scalar in tensorflow graph, how about using tf.squeeze or tf.reshape([], tensor).

Thanks.
2021년 11월 24일 수요일 오전 8시 24분 18초 UTC+9에 p10...@siswa.ukm.edu.my님이 작성:

ALI Q SAEED

unread,
Nov 24, 2021, 2:24:15 AM11/24/21
to Keras-users
Hi,
Thanks for your replay.
Infact, i already have a tensor as scalar of one value,  and i need to extract this value to feed it into a function that takes single value as regular float variable not as 1-D tensor/scalar shape.

Is that possible, please ?
Thanks

Lance Norskog

unread,
Nov 24, 2021, 2:36:29 AM11/24/21
to ALI Q SAEED, Keras-users
Where in the computation are you calling this?
You can get the output of the model during training. I have done this by creating a loss function and stashing the output in a Callback.


PENAFIAN: E-mel ini dan apa-apa fail yang dikepilkan bersamanya ("Mesej") adalah ditujukan hanya untuk kegunaan penerima(-penerima) yang termaklum di atas dan mungkin mengandungi maklumat sulit. Anda dengan ini dimaklumkan bahawa mengambil apa jua tindakan bersandarkan kepada, membuat penilaian, mengulang hantar, menghebah, mengedar, mencetak, atau menyalin Mesej ini atau sebahagian daripadanya oleh sesiapa selain daripada penerima(-penerima) yang termaklum di atas adalah dilarang. Jika anda telah menerima Mesej ini kerana kesilapan, anda mesti menghapuskan Mesej ini dengan segera dan memaklumkan kepada penghantar Mesej ini menerusi balasan e-mel. Pendapat, rumusan, dan sebarang maklumat lain di dalam Mesej ini yang tidak berkait dengan urusan rasmi Universiti Kebangsaan Malaysia (UKM) adalah difahami sebagai bukan dikeluar atau diperakui oleh mana-mana pihak yang disebut.

DISCLAIMER : This e-mail and any files transmitted with it ("Message") is intended only for the use of the recipient(s) named above and may contain confidential information. You are hereby notified that the taking of any action in reliance upon, or any review, retransmission, dissemination, distribution, printing or copying of this Message or any part thereof by anyone other than the intended recipient(s) is strictly prohibited. If you have received this Message in error, you should delete this Message immediately and advise the sender by return e-mail. Opinions, conclusions and other information in this Message that do not relate to the official business of The National University of Malaysia (UKM) shall be understood as neither given nor endorsed by any of the aforementioned.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/ce0be00e-3c7d-4724-a83a-986fd8cf4935n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

ALI Q SAEED

unread,
Nov 24, 2021, 7:58:56 AM11/24/21
to Keras-users
After a conv layer, i call a function to do some manipulation to every individual feature map. let's take an example:

Suppose a function fun1 located after a conv1 layer,  inside func1 i extract  max_value  from the first feature map    as :    x  =  tf.reduce_max(feature_map1) ,    in this case,  x is a 1-D tensor(scalar) that contains only one value (the maximum pixel value).

then i need to extract this value of x and put it in non-tensor variable to feed it to a another function i.e Discretization() which doesn't accept tensors as input but regular variables (i.e float, int).

Hope this is clear.
Thanks for taking time to ready and assist.

Lance Norskog

unread,
Nov 24, 2021, 3:44:59 PM11/24/21
to ALI Q SAEED, Keras-users
All of the computation in a model has to be done in tensors. Models are analyzed and the tensor graph is translated to compiled code that is optimized for the different platforms. The code does not support lifting data out in the middle.

It is possible that you can code your data augmentation in Numpy, and use the experimental Numpy support to use it in a model. I have not seen any guides for this.



ALI Q SAEED

unread,
Nov 24, 2021, 5:55:42 PM11/24/21
to Keras-users
Thanks lance for taking time to clarify issues. 
Appreciated (^_^). 

Reply all
Reply to author
Forward
0 new messages