I am getting an error that I can't figure out when I run my neural network in Keras as soon as I introduce a class weight

大兔子大兔子 提交于 2020-06-09 05:39:26

问题


Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, 35, 32)            96        
_________________________________________________________________
batch_normalization (BatchNo (None, 35, 32)            128       
_________________________________________________________________
dropout (Dropout)            (None, 35, 32)            0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 34, 64)            4160      
_________________________________________________________________
batch_normalization_1 (Batch (None, 34, 64)            256       
_________________________________________________________________
dropout_1 (Dropout)          (None, 34, 64)            0         
_________________________________________________________________
flatten (Flatten)            (None, 2176)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                139328    
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 65        
=================================================================
Total params: 144,033
Trainable params: 143,841
Non-trainable params: 192

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-14-09a8a255f317> in <module>()
----> 1 history = model.fit(X_Train,Y_Train, epochs = epochs, validation_data =(X_Test,Y_Test), verbose=1,class_weight=class_weights1)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  indices[31] = -9223372036854775808 is not in [0, 2)
     [[{{node GatherV2}}]]
     [[IteratorGetNext]]
     [[IteratorGetNext/_2]]
  (1) Invalid argument:  indices[31] = -9223372036854775808 is not in [0, 2)
     [[{{node GatherV2}}]]
     [[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_6381]

Function call stack:
train_function -> train_function

Somehow I'm getting a massive negative number? My dataset is all percentile data so numbers between 0 and 1.

Also here is the code to set up the model:

class_weights1 = {0: 1.,
                1: 50.}

epochs = 1
model = Sequential()
model.add(Conv1D(filters = 32, kernel_size = 2, activation='relu',input_shape=(36,1)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv1D(filters = 64, kernel_size = 2, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(1,activation='sigmoid'))

And then the code to compile and run it:

model.compile(optimizer=Adam(lr=0.005),loss='binary_crossentropy',metrics=['accuracy'])

history = model.fit(X_Train,Y_Train, epochs = epochs, validation_data =(X_Test,Y_Test), verbose=1,class_weight=class_weights1)

I know this is a problem with my class_weight because when I remove it, everything works (but the model is terrible because the data is imbalanced and I need to increase the weight of the minority class to adjust for this).


回答1:


I figured it out, it was because there were some NaN in Y_Train...



来源:https://stackoverflow.com/questions/62067455/i-am-getting-an-error-that-i-cant-figure-out-when-i-run-my-neural-network-in-ke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!