keras-contrib icon indicating copy to clipboard operation
keras-contrib copied to clipboard

Index out of range using input dim 2,when implement crf

Open forglin opened this issue 6 years ago • 13 comments

I write code in keras trying to build a QA system,And I use a lstm layer to compute a representation for question,and Evidenve lstm for analyzing evidence. At last I trying to merge them together and feed them into another lstm layer to produce the "features" to CRF.

The CRF layer for sequence labeling which indicates whether each word in the evidence is at the beginning (B), inside (I) or outside (O) of the answer. The shape of texts_evi like this [11117,110,64]which is the same to texts_q.

I think I make some mistake about dimention of array.But I can't fix it.

`q_model = Sequential() q_model.add(LSTM(64,activation='relu',return_sequences=True,name='q_lstm',input_shape=(110,64))) q_input= Input(shape=(110,64)) q = q_model(q_input)

evi_model=Sequential() evi_model.add(LSTM(64,activation='relu',name='q_lstm',return_sequences=True,input_shape=(110,64))) evi_input=Input(shape=(110,64)) encode_evi=evi_model(evi_input)

merged = keras.layers.concatenate([q, encode_evi])

lstm=LSTM(64, activation='relu',return_sequences=False)(merged) output = CRF(110, sparse_target=True)(lstm)

model = Model(inputs=[q_input,evi_input],outputs=output) model.compile(loss='categorical_crossentropy', optimizer='adam') model.fit([texts_q,texts_evi],lable, batch_size=32, epochs=10,validation_split=0.2) ` the error is:

ValueError: Index out of range using input dim 2; input has only 2 dims for 'crf_1/strided_slice' (op: 'StridedSlice') with input shapes: [?,110], [3], [3], [3] and with computed input tensors: input[3] = <1 1 1>.

forglin avatar Mar 30 '18 08:03 forglin

Did you solve it?

spytensor avatar Sep 10 '18 02:09 spytensor

how to solve it?

Lanme avatar Oct 23 '18 10:10 Lanme

This is because the crf layer expects the labels in a different shape. Normally your labels would be of shape (num_samples, max_length) but the crf layer expects them in the form (num_samples, max_length, 1) .

An easy fix is to reshape your labels as follows: labels = labels.reshape((labels.shape[0], labels.shape[1], 1))

uwaisiqbal avatar Oct 31 '18 17:10 uwaisiqbal

This is because the crf layer expects the labels in a different shape. Normally your labels would be of shape (num_samples, max_length) but the crf layer expects them in the form (num_samples, max_length, 1) .

An easy fix is to reshape your labels as follows: labels = labels.reshape((labels.shape[0], labels.shape[1], 1))

Hi, I'm little confused about your answer, I have the same problem just like his, I want to try your method, only don't know where to change. Can you be more specific on that issue? Thants a lot

blog4me avatar Mar 18 '19 06:03 blog4me

Did you solve it? I have the very same problem, If you solve it, plz help me.

blog4me avatar Mar 18 '19 06:03 blog4me

I have the same problem. And solve it by y_train = np.expand_dims(y_train,2)

jinkaima avatar Mar 20 '19 03:03 jinkaima

I have similar problem, I cant solve it

HansyM avatar Aug 22 '19 07:08 HansyM

This is because the crf layer expects the labels in a different shape. Normally your labels would be of shape (num_samples, max_length) but the crf layer expects them in the form (num_samples, max_length, 1) . An easy fix is to reshape your labels as follows: labels = labels.reshape((labels.shape[0], labels.shape[1], 1))

Hi, I'm little confused about your answer, I have the same problem just like his, I want to try your method, only don't know where to change. Can you be more specific on that issue? Thants a lot

So you need to reshape your labels before you call model.fit You need to reshape your y_train data so that it has shape (num_samples, max_length, 1)

uwaisiqbal avatar Sep 02 '19 14:09 uwaisiqbal

Hi @uwaisiqbal , the solution you proposed does resolve the error mentioned in this issue. However, I am getting a new error on reshaping the labels as follows :

InvalidArgumentError: Matrix size-incompatible: In[0]: [158,1], In[1]: [4,4]
	 [[{{node loss_1/crf_2_loss/MatMul_1}}]]

Can you or anyone help me in resolving this?

DebanjanaKar avatar Sep 07 '19 16:09 DebanjanaKar

Hi @uwaisiqbal , the solution you proposed does resolve the error mentioned in this issue. However, I am getting a new error on reshaping the labels as follows :

InvalidArgumentError: Matrix size-incompatible: In[0]: [158,1], In[1]: [4,4]
	 [[{{node loss_1/crf_2_loss/MatMul_1}}]]

Can you or anyone help me in resolving this?

If you still have this problem. Please have a look at your previous layer output shape and then use Reshape layer to reshape the output. Then the problem should be solved. If NOT, try to use sparse_tartget=true. Hope this can help you.

ethanyuxinzhang avatar Feb 10 '20 15:02 ethanyuxinzhang

ValueError: Index out of range using input dim 2; input has only 2 dims for '{{node loss/dense_loss/strided_slice_2}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=3, ellipsis_mask=0, end_mask=3, new_axis_mask=0, shrink_axis_mask=0](dense_target, loss/dense_loss/strided_slice_2/stack, loss/dense_loss/strided_slice_2/stack_1, loss/dense_loss/strided_slice_2/stack_2)' with input shapes: [?,?], [3], [3], [3] and with computed input tensors: input[3] = <1 1 1>

while running this repo https://github.com/suyxing/ssd_resnet model = SSD_resnet.resnet_34(300, 300, 3, classes=NUM_CLASSES)

angyee avatar Aug 15 '20 16:08 angyee

Assalam o aliekum bro @uwaisiqbal I have the same problem as mentioned above and following is my code:

input = Input(shape=(75,)) emd=Embedding(input_dim=n_words + 1,output_dim=20, input_length=75,mask_zero=False)(input) bilstm = Bidirectional(LSTM(units=75, return_sequences=True, recurrent_dropout=0.1))(emd)

input2 = Input(shape=(75,)) flat_bilstm = Flatten()(bilstm) layersConcate = concatenate([flat_bilstm,input2])

model_crf =Dense(units=75, activation="relu")(layersConcate)

crf=CRF(8, sparse_target=True) out_ = crf(model_crf) ----------------> when I pass model_crf it generates the following error:

ValueError: Index out of range using input dim 2; input has only 2 dims for 'crf_1_3/strided_slice' (op: 'StridedSlice') with input shapes: [?,8], [3], [3], [3] and with computed input tensors: input[3] = <1 1 1>.

please help me I am kind of stuck in this problem

paktiger avatar Dec 25 '21 19:12 paktiger

Assalam o aliekum bro @uwaisiqbal I have the same problem as mentioned above and following is my code:

input = Input(shape=(75,)) emd=Embedding(input_dim=n_words + 1,output_dim=20, input_length=75,mask_zero=False)(input) bilstm = Bidirectional(LSTM(units=75, return_sequences=True, recurrent_dropout=0.1))(emd)

input2 = Input(shape=(75,)) flat_bilstm = Flatten()(bilstm) layersConcate = concatenate([flat_bilstm,input2])

model_crf =Dense(units=75, activation="relu")(layersConcate)

crf=CRF(8, sparse_target=True) out_ = crf(model_crf) ----------------> when I pass model_crf it generates the following error:

ValueError: Index out of range using input dim 2; input has only 2 dims for 'crf_1_3/strided_slice' (op: 'StridedSlice') with input shapes: [?,8], [3], [3], [3] and with computed input tensors: input[3] = <1 1 1>.

please help me I am kind of stuck in this problem

This problem is solved since the CRF method requires three-dimensional arrays but I was passing 2D only.

paktiger avatar Dec 28 '21 07:12 paktiger