Aggregate TimeDistributed layers by sum or average

305 views
Skip to first unread message

Alexander Schindler

unread,
Apr 27, 2017, 5:43:46 AM4/27/17
to Keras-users
Hi,

I'm using the TimeDistributed wrapper to process a set of images grouped together in one tensor. The goal is to have the model predict a cummulative maximum, similar to a majority vote prediction.

But, I have problems implementing the accumulation.

The TimeDistributed wrapper outputs the softmax layers for the shared CNN-stacks. The wrapper's output shape is (None, 5, 10). My intention was to merge this layer along the 1st axis to get the desired cummulated results with an expected shape of (None, 10).

inp = Input(shape=(5, 1, 40, 40))
   
# ===== frequency filter =====

cnn
= TimeDistributed(Convolution2D(16,3,3, border_mode='same', activation="relu"))(inp)
cnn
= TimeDistributed(MaxPooling2D(pool_size=(2,2))(cnn)
cnn
= TimeDistributed(Convolution2D(32,3,3, border_mode='same', activation="relu"))(cnn)
cnn
= TimeDistributed(MaxPooling2D(pool_size=(2,2))(cnn)
cnn
= TimeDistributed(Convolution2D(64, 3, 3, border_mode='same', activation="relu"))(cnn)
cnn
= TimeDistributed(MaxPooling2D(pool_size=(2,2)))(cnn)
cnn
= TimeDistributed(Flatten())(cnn)  
cnn
= TimeDistributed(Dense(512, activation="relu"))(cnn)
cnn
= TimeDistributed(Dense(10,  activation='softmax'))(cnn)

# output shape of cnn == (None, 5, 10)

mean
= merge(cnn, mode='average', concat_axis=0) # <= this fails

model
= Model(input=inp, output=mean)

# expected output shape of mode == (None, 10)

Unfortunately this does not work. I get the following error:

TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)

Can anybody please point me in the right direction?

Best,
Alex

Daπid

unread,
Apr 27, 2017, 7:01:29 AM4/27/17
to Alexander Schindler, Keras-users
merge takes a list of inputs, not a single tensor, you should use GlobalAveragePooling1D instead.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/0782a1df-69bc-4187-8401-3d9478fc88d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Schindler

unread,
Apr 27, 2017, 5:49:28 PM4/27/17
to Keras-users, slyc...@gmail.com
Hi David,

thanks for your help. The GlobalAveragePooling1D layer works perfectly.

Alex.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages