-1

How can we use a layer from tf.contrib.layers in keras ?

I tried to mix the tf layer inside my other keras layers.

input = Input(shape=state_shape)
conv = Conv2D(64, (6, 6), strides=(1, 1), activation='relu')(input)
spatial_softmax = tf.contrib.layers.spatial_softmax(conv, name='spatial_softmax', data_format='NHWC')
fc = Dense(128, activation='relu')(spatial_softmax)
fc = Dense(128, activation='relu')(fc)
output = Dense(action_size, activation='softmax')(fc)

Model(inputs=input, outputs=output)

But when I run the code I get the following error

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

Any idea?

Thanks!

Edit: It's not the same question as here, I am asking for a layer with trainable weights and not a deterministic layer. Do the gradients will backprop to the layer ?

Wisym
  • 11
  • 2

1 Answers1

0

I believe you would need to put that inside a Lambda layer:

spatial_softmax = Lambda(lambda x: spatial_softmax(x, name='spatial_softmax',
                         data_format='NHWC'))(conv)
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • As the temperature is a learnable Variable inside the spatial_softmax layer, will it work with a Lambda ? – Wisym Mar 15 '18 at 14:53
  • @Wisym Which temperature are you talking about? – Dr. Snoopy Mar 15 '18 at 14:54
  • It is a variable inside the spatial_softmax https://www.tensorflow.org/api_docs/python/tf/contrib/layers/spatial_softmax https://github.com/tensorflow/tensorflow/blob/r1.6/tensorflow/contrib/layers/python/layers/layers.py#L2754 – Wisym Mar 15 '18 at 15:01