无效的参数错误Keras

时间:2018-07-12 06:13:21

标签: python keras

我试图为二进制分类任务创建一个神经网络模型。模型是这样的

LEARNING_RATE_INIT = 0.001
LEARNING_RATE_END = 0.0001
BATCH_SIZE = 20000
EPOCHS = 2

EMBEDDING_N = 50
DENSE_N = 1024

SPATIAL_DROPOUT_1D = 0.2
DROPOUT_1 = 0.2
DROPOUT_2 = 0.2

in_machine = Input(shape=[1], name='machine')
emb_machine = Embedding(max_machine, EMBEDDING_N)(in_machine)

in_windspeed = Input(shape=[1], name='windspeed')
emb_windspeed = Embedding(max_windspeed, EMBEDDING_N)(in_windspeed)

in_activepower = Input(shape=[1], name='activepower')
emb_activepower = Embedding(max_activepower, EMBEDDING_N)(in_activepower)

in_pitchangle = Input(shape=[1], name='pitchangle')
emb_pitchangle = Embedding(max_pitchangle, EMBEDDING_N)(in_pitchangle)

in_genspeed = Input(shape=[1], name='genspeed')
emb_genspeed = Embedding(max_genspeed, EMBEDDING_N)(in_genspeed)

in_temp = Input(shape=[1], name='temp')
emb_temp = Embedding(max_temp, EMBEDDING_N)(in_temp)

in_turbine = Input(shape=[1], name='turbine')
emb_turbine = Embedding(max_turbine, EMBEDDING_N)(in_turbine)


print('Create RNN Layers...')

fe = concatenate([
    (emb_machine), 
    (emb_windspeed), 
    (emb_activepower), 
    (emb_pitchangle), 
    (emb_genspeed), 
    (emb_temp), 
    (emb_turbine)
])

s_dout = SpatialDropout1D(SPATIAL_DROPOUT_1D)(fe)
x = Flatten()(s_dout)

x = Dropout(0.2)(Dense(1024,activation='relu')(x))
x = Dropout(0.2)(Dense(256,activation='relu')(x))

outp = Dense(1,activation='sigmoid')(x)

model = Model(inputs=[in_machine, in_windspeed, in_activepower, in_pitchangle,
    in_genspeed, in_temp, in_turbine], outputs=outp)

print('Model made')

它是这样编译的

model.compile(loss='binary_crossentropy', optimizer=optimizer_adam, metrics=['accuracy'])
model.summary()

模型架构如下所示

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
machine (InputLayer)            (None, 1)            0                                            
__________________________________________________________________________________________________
windspeed (InputLayer)          (None, 1)            0                                            
__________________________________________________________________________________________________
activepower (InputLayer)        (None, 1)            0                                            
__________________________________________________________________________________________________
pitchangle (InputLayer)         (None, 1)            0                                            
__________________________________________________________________________________________________
genspeed (InputLayer)           (None, 1)            0                                            
__________________________________________________________________________________________________
temp (InputLayer)               (None, 1)            0                                            
__________________________________________________________________________________________________
turbine (InputLayer)            (None, 1)            0                                            
__________________________________________________________________________________________________
embedding_48 (Embedding)        (None, 1, 50)        3700        machine[0][0]                    
__________________________________________________________________________________________________
embedding_49 (Embedding)        (None, 1, 50)        1200        windspeed[0][0]                  
__________________________________________________________________________________________________
embedding_50 (Embedding)        (None, 1, 50)        108000      activepower[0][0]                
__________________________________________________________________________________________________
embedding_51 (Embedding)        (None, 1, 50)        1500        pitchangle[0][0]                 
__________________________________________________________________________________________________
embedding_52 (Embedding)        (None, 1, 50)        78350       genspeed[0][0]                   
__________________________________________________________________________________________________
embedding_53 (Embedding)        (None, 1, 50)        3050        temp[0][0]                       
__________________________________________________________________________________________________
embedding_54 (Embedding)        (None, 1, 50)        100         turbine[0][0]                    
__________________________________________________________________________________________________
concatenate_6 (Concatenate)     (None, 1, 350)       0           embedding_48[0][0]               
                                                                 embedding_49[0][0]               
                                                                 embedding_50[0][0]               
                                                                 embedding_51[0][0]               
                                                                 embedding_52[0][0]               
                                                                 embedding_53[0][0]               
                                                                 embedding_54[0][0]               
__________________________________________________________________________________________________
spatial_dropout1d_3 (SpatialDro (None, 1, 350)       0           concatenate_6[0][0]              
__________________________________________________________________________________________________
flatten_17 (Flatten)            (None, 350)          0           spatial_dropout1d_3[0][0]        
__________________________________________________________________________________________________
dense_24 (Dense)                (None, 1024)         359424      flatten_17[0][0]                 
__________________________________________________________________________________________________
dropout_18 (Dropout)            (None, 1024)         0           dense_24[0][0]                   
__________________________________________________________________________________________________
dense_25 (Dense)                (None, 256)          262400      dropout_18[0][0]                 
__________________________________________________________________________________________________
dropout_19 (Dropout)            (None, 256)          0           dense_25[0][0]                   
__________________________________________________________________________________________________
dense_26 (Dense)                (None, 1)            257         dropout_19[0][0]                 
==================================================================================================
Total params: 817,981
Trainable params: 817,981
Non-trainable params: 0
__________________________________________________________________________________________________

在尝试拟合模型时,出现以下错误。

InvalidArgumentError: indices[19577,0] = -3 is not in [0, 2160)
     [[Node: embedding_50/GatherV2 = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@training_3/Adam/gradients/embedding_50/GatherV2_grad/Reshape"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_50/embeddings/read, embedding_50/Cast, embedding_48/GatherV2/axis)]]

此错误的原因是什么?该如何解决??

我的输入数据包含负值是问题所在。那么有没有办法合并负值??

1 个答案:

答案 0 :(得分:0)

您对Embedding的使用毫无意义,您说您的输入具有负值。如果您查看documentation for Embedding,它会显示:

  

将正整数(索引)转换为固定大小的密集向量。   例如。 [[4],[20]]-> [[0.25,0.1],[0.6,-0.2]]

这使我感到您的输入不是正整数,这意味着在这些输入上使用嵌入是没有意义的。嵌入用于语言建模,例如,将单词索引转换为向量,它应与任何类型的正整数输入一起使用,但不适用于常规输入。