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

时间:2019-01-11 05:58:41

标签: tensorflow machine-learning keras lstm

您好,我正在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 个答案:

答案 0 :(得分:1)

这里的问题是批大小是在图级别定义的。这里我使用batch_size =9进行图,是的,我通过验证拆分{{1 }}的批量为9,但是为了验证,我只留下了一个样本,因为.1是一个。

因此10的批处理大小无法传递到图形,因为它需要10*.1的批处理大小。这就是出现此错误的原因

对于解决方案,我放了1,然后通过使用9也很好地实现了精度。

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

干杯..