索引= 2不在[0,1)中

编程入门 行业动态 更新时间:2024-10-23 21:39:12
本文介绍了索引= 2不在[0,1)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理seq2sql项目,并且成功构建了模型,但是在训练时出现错误.我没有使用任何Keras嵌入层.

I'm working on a seq2sql project and I successfully build a model but when training I get an error. I'm not using any Keras embedding layer.

M=13 #Question Length d=40 #Dimention of the LSTM C=12 #number of table Columns batch_size=9 inputs1=Input(shape=(M,100),name='question_token') Hq=Bidirectional(LSTM(d,return_sequences=True),name='QuestionENC')(inputs1) #this is HQ shape is (num_samples,13,80) inputs2=Input(shape=(C,3,100),name='col_token') col_lstm_layer=Bidirectional(LSTM(d,return_sequences=False),name='ColENC') def hidd(te): t=tf.Variable(initial_value=1,dtype=tf.int32) for i in range(batch_size): t=tf.assign(t,i) Z = tf.nn.embedding_lookup(te, t) print(col_lstm_layer(Z)) h=tf.reshape(col_lstm_layer(Z),[1,C,d*2]) if i==0: # cols_last_hidden=tf.Variable(initial_value=h) cols_last_hidden=tf.stack(h)#this is because it gives an error if we use tf.Variable here else: cols_last_hidden=tf.concat([cols_last_hidden,h],0)#shape of this one is (num_samples,num_col,80) 80 is last encoding of each column return cols_last_hidden cols_last_hidden=Lambda(hidd)(inputs2) Hq=Dense(d*2,name='QuestionLastEncode')(Hq) I=tf.Variable(initial_value=1,dtype=tf.int32) J=tf.Variable(initial_value=1,dtype=tf.int32) K=1 def get_col_att(tensors): global K,all_col_attention if K: t=tf.Variable(initial_value=1,dtype=tf.int32) for i in range(batch_size): t=tf.assign(t,i) x = tf.nn.embedding_lookup(tensors[0], t) # print("tensors[1]:",tensors[1]) y = tf.nn.embedding_lookup(tensors[1], t) # print("x shape",x.shape,"y shape",y.shape) y=tf.transpose(y) # print("x shape",x.shape,"y",y.shape) Ecol=tf.reshape(tf.transpose(tf.tensordot(x,y,axes=1)),[1,C,M]) if i==0: # all_col_attention=tf.Variable(initial_value=Ecol,name=""+i) all_col_attention=tf.stack(Ecol) else: all_col_attention=tf.concat([all_col_attention,Ecol],0) K=0 print("all_col_attention",all_col_attention) return all_col_attention total_alpha_sel_lambda=Lambda(get_col_att,name="Alpha")([Hq,cols_last_hidden]) total_alpha_sel=Dense(13,activation="softmax")(total_alpha_sel_lambda) # print("Hq",Hq," total_alpha_sel_lambda shape",total_alpha_sel_lambda," total_alpha_sel shape",total_alpha_sel.shape) def get_EQcol(tensors): global K if K: t=tf.Variable(initial_value=1,dtype=tf.int32) global all_Eqcol for i in range(batch_size): t=tf.assign(t,i) x = tf.nn.embedding_lookup(tensors[0], t) y = tf.nn.embedding_lookup(tensors[1], t) Eqcol=tf.reshape(tf.tensordot(x,y,axes=1),[1,C,d*2]) if i==0: # all_Eqcol=tf.Variable(initial_value=Eqcol,name=""+i) all_Eqcol=tf.stack(Eqcol) else: all_Eqcol=tf.concat([all_Eqcol,Eqcol],0) K=0 print("all_Eqcol",all_Eqcol) return all_Eqcol K=1 EQcol=Lambda(get_EQcol,name='EQcol')([total_alpha_sel,Hq])#total_alpha_sel(12x13) Hq(13xd*2) EQcol=Dropout(.2)(EQcol) L1=Dense(d*2,name='L1')(cols_last_hidden) L2=Dense(d*2,name='L2')(EQcol) L1_plus_L2=Add()([L1,L2]) pre=Flatten()(L1_plus_L2) Psel=Dense(12,activation="softmax")(pre) model=Model(inputs=[inputs1,inputs2],outputs=Psel) modelpile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy']) model.summary() earlyStopping=EarlyStopping(monitor='val_loss', patience=7, verbose=0, mode='auto') history=model.fit([Equestion,Col_Embeddings],y_train,epochs=50,validation_split=.1,shuffle=False,callbacks=[earlyStopping],batch_size=batch_size)

Equestion,Col_Embeddings和y_train的形状分别为(10、12、3、100),(10、13、100)和(10、12).

The shapes of the Equestion, Col_Embeddings, and y_train are (10, 12, 3, 100) ,(10, 13, 100) and (10, 12).

我搜索了此错误,但是在所有情况下,他们都错误地使用了嵌入层.即使我没有使用此错误,这里也出现了此错误.

I searched about this error but in all cases they have used an embedding layer incorrectly. Here I get this error even though I'm not using one.

indices = 2 is not in [0, 1) [[{{node lambda_3/embedding_lookup_2}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@col_token_2"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_col_token_2_0_1, lambda_3/Assign_2, lambda_3/embedding_lookup_2/axis)]]

推荐答案

这里的问题是批处理大小是在图级别定义的.在这里,我对图使用了batch_size =9,是的,我得到的批处理大小为通过验证拆分.1进行训练,以用于10的全部批次大小,但是对于验证,我只留下了一个样本,因为10*.1是一个.

The problem here was the batch size is defined at the graph level.here i have used batch_size =9 for the graph and yes i get batch size of 9 for training by the validation split .1 for the full batch size of 10 but for the validation i left only one sample because 10*.1 is one.

1的批处理大小不能传递到图形,因为它需要9的批处理大小.这就是为什么会出现此错误

So the batch size of 1 cannot be passed to the graph because it needs batch size of 9.that's why this error comes

关于解决方案,我放了batch_size=1,然后通过使用batch_size=1,它也能很好地工作.

As for the solution i put the batch_size=1 and then it works fine also got a good accuracy by using batch_size=1.

希望这会对某人有所帮助.

Hope this will help someone.

干杯..

更多推荐

索引= 2不在[0,1)中

本文发布于:2023-11-30 00:41:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!