How to repeat an unknown measurement in Keras for both backends

For example (I can do it with Theano without problems):

# log_var has shape --> (num, )
# Mean has shape --> (?, num)
std_var = T.repeat(T.exp(log_var)[None, :], Mean.shape[0], axis=0)

With TensorFlow, I can do this:

std_var = tf.tile(tf.reshape(tf.exp(log_var), [1, -1]), (tf.shape(Mean)[0], 1))

But I don’t know how to do the same for Keras, maybe like this:

std_var = K.repeat(K.reshape(K.exp(log_var), [1, -1]), Mean.get_shape()[0])

or

std_var = K.repeat_elements(K.exp(log_var), Mean.get_shape()[0], axis=0)

... because it Meanhas an unknown dimension on the 0 axis.

I need this for user level output:

return K.concatenate([Mean, Std], axis=1)
+4
source share
1 answer

Keras has an abstraction layer keras.backendthat you seem to have already found (you name it K). This layer provides all the features for Theano and TensorFlow that you will need.

Say your TensorFlow code that works

std_var = tf.tile(tf.reshape(tf.exp(log_var), [1, -1]), (tf.shape(Mean)[0], 1))

, :

std_var = K.tile(K.reshape(K.exp(log_var), (1, -1)), K.shape(Mean)[0])

Theano TensorFlow (-1 ), .

, TF. (1, -1), , 0 1. , :

std_var = K.tile(K.reshape(K.exp(log_var), (-1, num)), K.shape(Mean)[0])
+2

Source: https://habr.com/ru/post/1654043/


All Articles