代码之家  ›  专栏  ›  技术社区  ›  Dulmina

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

  •  -2
  • Dulmina  · 技术社区  · 7 年前

    嗨,我正在做SEQ2SQL项目,我成功地建立了一个模型,但当培训时,我得到以下错误,我也没有使用任何keras嵌入层。下面是我的代码

    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)
    model.compile(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)。我搜索了有关此错误的信息,但所有这些情况下,他们都错误地使用了嵌入层。但在这里,虽然我没有使用它,但我得到了这个错误

    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)]]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Dulmina    7 年前

    这里的问题是批大小是在图形级别定义的。 batch_size =9 对于图表,是的,我得到了 9 用于验证分割的培训 .1 整批尺寸为 10 但为了验证,我只留了一个样本,因为 10*.1 是一个。

    所以批量大小 1 无法传递到图表,因为它需要的批大小为 。这就是这个错误出现的原因。

    至于解决方案,我把 batch_size=1 然后它工作得很好,通过使用也得到了很好的精度 巴契尺寸=1 .

    希望这能帮助别人。

    干杯。。