Flatten functional API -- can't pass output to Dense -- shape calculation issue?

1,169 views
Skip to first unread message

David S

unread,
Jan 2, 2017, 1:30:08 AM1/2/17
to Keras-users
I am trying to use the keras functional API to flatten a  batch of images and apply a dense layer. However when I do

flatten()(img_batch)

I get something that doesn't know its shape, that is, I get

In [34]: keras.layers.Flatten()(yy)
Out[34]: <tf.Tensor 'Reshape:0' shape=(?, ?) dtype=float32>


Above, yy is a batch of images that are (76,56,1), I'd hope that the shape from Flatten() would be (?, 4256)

I think this is why the subsequent call to the Dense layer behaves as follows:

 
File "xtcav_new.py", line 182, in autoencoder
    encoded
= Dense(encoding_dim, activation='relu')(flat)
 
File "/home/davidsch/miniconda2/envs/mlearn/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in __call__
   
self.build(input_shapes[0])
 
File "/home/davidsch/miniconda2/envs/mlearn/lib/python2.7/site-packages/keras/layers/core.py", line 752, in build
    constraint
=self.W_constraint)
 
File "/home/davidsch/miniconda2/envs/mlearn/lib/python2.7/site-packages/keras/engine/topology.py", line 415, in add_weight
    weight
= initializer(shape, name=name)
 
File "/home/davidsch/miniconda2/envs/mlearn/lib/python2.7/site-packages/keras/initializations.py", line 59, in glorot_uniform
    s
= np.sqrt(6. / (fan_in + fan_out))
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'



I was trying to modify the keras autoencoder blog at https://blog.keras.io/building-autoencoders-in-keras.html for a dataset I have. This is keras 1.2.0 and Tensorflow 0.12.

best,

David


Message has been deleted

Benjamin Striner

unread,
Jan 9, 2017, 10:27:22 PM1/9/17
to Keras-users
So I assume yy is (None, 76,56,1), such as gray 76x56 images. The below works for me:

from keras.layers import Dense, Input, Flatten
from keras.models import Model
a = Input((76,56,1))
b = Flatten()(a)
c = Dense(100)(b)
m = Model(a,c)
m.summary()

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to
====================================================================================================
input_1 (InputLayer)             (None, 76, 56, 1)     0
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 4256)          0           input_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 100)           425700      flatten_1[0][0]
====================================================================================================
Total params: 425,700
Trainable params: 425,700
Non-trainable params: 0
Reply all
Reply to author
Forward
0 new messages