How to reform sliced tensor ?

93 views
Skip to first unread message

ALI Q SAEED

unread,
Nov 28, 2021, 9:39:31 AM11/28/21
to Keras-users
Hi there,
i am trying to slice a conv layer of shape = (None, 32, 48, 48),  using 
sliced_tensor = x[0,k,:,:] 

then i need to find the max/min value of the sliced_tensor which is of shape =(48, 48).

here is my function :

custom fun(tensor)
      sliced_tensor =   tensor [0,1:,:]
      max_var = tf.reduce_max(sliced_tensor, axis=[0,1])
      min_var = tf.reduce_min(sliced_tensor, axis=[0,1])
      mid_val= (max_var-min_var)/2             # here i got the error msg as shown below

      ''''' other computations ''''

      return new_tensor


inputs = tf.keras.layers.Input(shape=(1,48,48))
conv1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same',data_format='channels_first')(inputs)
custom fun( conv1 )

error msg : 
TypeError: Cannot interpret '<KerasTensor: shape=() dtype=float32 (created by layer 'tf.math.reduce_max_15')>' as a data type

i know there are no values initialized to the layers, but i need to check whether my code is executable on model fit or not. 

please, can i do such things during model training ? and how to fix this problem ?

thanks for reading, any help is appreciated.

Lance Norskog

unread,
Nov 28, 2021, 4:31:22 PM11/28/21
to ALI Q SAEED, Keras-users
This is what the Lambda layer is for.



If 'new_tensor' does not have the same shape as the input, you need to give its shape as the output_shape= parameter.

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/f4951f4a-afe8-44b1-8188-a6545333ae95n%40googlegroups.com.


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

ALI Q SAEED

unread,
Nov 29, 2021, 10:25:53 AM11/29/21
to Keras-users
Thanks  Lance for the amazing links, they are really helpful.

please, if you have other good tutorials or links in your  pocket  about tf.py_function, building models using subclassing and debugging model during training (to understand what is going on under the hood). i would be so grateful if you share them.
I read keras and tensorflow webpages however i found them a bit complex.

Thanks.

Lance Norskog

unread,
Nov 29, 2021, 4:52:09 PM11/29/21
to ALI Q SAEED, Keras-users
I have found https://machinelearningmastery.com/ to be really helpful. Kevin Brownlow has been writing tutorials for years now, and I have debugged a few problems by finding his version of something.

2 years ago, I was in a Peets in Silicon Valley and overheard two guys talking about his site.

Dennis S

unread,
Nov 29, 2021, 5:04:32 PM11/29/21
to Lance Norskog, ALI Q SAEED, Keras-users
Its Brownlee





--
Thanks,

Dennis

Lance Norskog

unread,
Nov 29, 2021, 5:05:42 PM11/29/21
to Dennis S, ALI Q SAEED, Keras-users
Aaaaaah, sorry :)

Lance Norskog

unread,
Nov 29, 2021, 5:07:59 PM11/29/21
to Dennis S, ALI Q SAEED, Keras-users
In fact, his name is Jason Brownlee!

ALI Q SAEED

unread,
Nov 30, 2021, 4:07:03 AM11/30/21
to Keras-users
Thanks Lance, 
You are the best (^_*).    

Reply all
Reply to author
Forward
0 new messages