简体   繁体   中英

keras concatenate embedding layers gets index error

I am doing a multiple embedding, and need to concatenate all the embedded layers together for training. However, I keep getting the indices[1,0] = 7 is not in [0.7) error.

Here is what I did:

models = []

i0 = Input(shape=(1,),name='model_store')

model_store = Embedding(1115,10,input_length=1)(i0)

model_store = Reshape(target_shape=(10,))(model_store)

models.append(model_store)

i1 = Input(shape=(1,),name='model_dow')

model_dow = Embedding(7,6,input_length=1)(i1)

model_dow = Reshape(target_shape=(6,))(model_dow)

models.append(model_dow)

i2 = Input(shape=(1,),name='model_promo')

model_promo = Dense(1,input_dim=1)(i2)

models.append(model_promo)

# there are 8 embedding and 3 dense layers in models.

# then, I do:

net = Concatenate()(models)

net = Dense(1000,kernel_initializer='uniform',activation='relu')(net)

# another dense layer

output = Dense(1,activation='relu')(net)

model = Model(inputs = [i0,i1,i2,...i10],outputs = output)

model.compile(loss='mean_absolute_error',optimizer='adam')

but when I do model.fit(), i get the indices[] = something not in [) error.

The inputs that go into i0,i1,...,i10 are like array([[1],[2],[3],...]), all length 1 inputs.

I have also tried to replace the Reshape() layers with Flatten() layers, but got the same error.

Someone,please help.

Well, i found the problem.

It's that I didn't feed the data in correct shape. In the Sequential API, the input data for multiplue network inputs should be a list of ndarrays (dict may also work). While it says a list of ndarrays should still work for Functional API, it didn't work in my case, probably due to some order issue. I used dictionary of ndarrys with input names ('model_store','model_dow'...) as keys , and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM