0

I'm trying to reshape tensor using Reshape layer:

from keras.layers.convolutional import  Conv2D,  MaxPooling2D,AveragePooling2D
from keras import backend as K
from keras.models import Model
from keras.layers import  Input
from keras.layers.core import Activation, Reshape
from keras.layers import Dense,Reshape,Lambda,Dropout
import numpy as np
from keras.layers.embeddings import Embedding
Dict_size=32
EmbedSz=16
img_sz=100
channels=3
input=Input(shape=(img_sz,img_sz,channels))
H=Conv2D(Dict_size, 3, 3, activation='relu', border_mode='same')(input) 
H=Lambda(lambda x:K.argmax(x, axis=3),output_shape=lambda s:  (img_sz,img_sz,))(H)
H=Reshape((1,img_sz*img_sz))(H)
model=Model(inputs=input,outputs=H)
#model.compile( optimizer= 'adam', metrics=[ 'accuracy' ],loss='mse')
ar=np.random.rand(1,100,100,3)
pr=model.predict(ar)
print(pr.shape)
print(pr)$

But getting this Error! File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 379, in _fix_unknown_dimension raise ValueError(msg) ValueError: total size of new array must be unchanged


I did not change the size !!

Adel Saleh
  • 13
  • 3

1 Answers1

0

You simply forgot to add the batch-size dimension to your Lambda layer:

H= Lambda(lambda x: K.argmax(x, axis=3), output_shape=lambda s: (None, img_sz,img_sz,))(H)
#                                                                  ^
#                                                                  |

So, just add None to the output_shape.

Pedia
  • 1,432
  • 2
  • 11
  • 17