简体   繁体   中英

How to convert a scalar dense layer output into a matrix with each element being equal to the scalar in tensorflow 2.0?

How to convert a scalar dense layer output of shape (none,1) into a matrix of shape (none,nx,ny,nc) with each element of the matrix being equal to the scalar in TensorFlow 2.0 using Keras?

Code snippet:

Y= tf.keras.layers.Dense(1,activation='relu')(X)
Z= Y*tf.keras.backend.ones(shape=(10,51,1))

Here Y is of shape (none,1) . I want Z to be of shape (none,10,51,1) but I am getting a shape of (10,51,1) . Somehow the batch size dimension (none) is lost when I multiply. Can anyone help?

You can do that like this:

Y = tf.keras.layers.Dense(1,activation='relu')(X)
Z = tf.keras.layers.Lambda(lambda y:
    tf.keras.backend.tile(y[:, tf.newaxis, tf.newaxis], (1, 10, 51, 1))(Y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM