Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use tf operations in keras models

I am trying to us tensorflow operations within a keras model and I am quite confused about the mechanism and what Lambda layers do to tf tensors.

So this works:

a = keras.layers.Input(shape=[1, 2], dtype='float', name='a')
s= keras.layers.Lambda(lambda x: tf.transpose(tf.transpose(x)))(a)
model = keras.models.Model(inputs=a, outputs=s)

but this does not work:

a = keras.layers.Input(shape=[1, 2], dtype='float', name='a')
s = tf.transpose(tf.transpose(a))
s = keras.layers.Lambda(lambda x: x)(s)
model = keras.models.Model(inputs=a, outputs=s)

and it says:

AttributeError: 'Tensor' object has no attribute '_keras_history'

so is it always necessary to pack up tf operations within a layer?

Question 2 (was why I came up the previous one): do we have to pack with a custom layer to do matrix multiplication in keras?

thanks.

like image 943
Fangzhou Zhai Avatar asked Nov 01 '25 20:11

Fangzhou Zhai


1 Answers

Question 1: Yes, it is necessary to wrap tf operations with a layer, because keras models require certain functions/variables that aren't included with tensorflow ops. In this case, _keras_history is a property that is only produced by wrapping the op with a layer.

Question 2: Is the matrix multiplication traHave you considered using a keras Dense layer, with use_bias=False? If you want to use a constant for the weight vector, you could set the kernel_initializer={constant}, and trainable=False.

like image 81
kww Avatar answered Nov 03 '25 09:11

kww



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!