'Ignoring NaN value in Tensorflow model

I am new to deep learning. In my project, I want to conduct deep learning predition using tensorflow but there are NaN in the Data. I want to preserve NaN at that position since I am doing pixel classification by making use of several bands in remote sensing. The model was successfully trained but the problem now is that the time of prediction is extremely long since there are billions pixels have to be classified (including the NaN). So, I am thinking if it is possible to skip the NaN data and only input that capable data into my neural network.

yPredict = np.array([])
for i in pbar(range(con.shape[0])):
    if con[i] == 1:
        yPredict = np.append(np.argmax(yPredict, model.predict(stacked[i, :, :], batch_size=1, verbose=1)), axis=1)
    elif con[i] == 0:
        yPredict = np.append(yPredict, np.nan)

Note: Con is a 1D array containing 0 or 1. Stacked is a 3D array:[number of pixels,1,bands]

But in fact, I found that this method needs quite a long time either because it contains a large number of comparisions. Therefore I am thinking if I can aggregate the 0 value and 1 value into groups,like: 000001110000 --> 0:5,1:3,0:5 so that in my looping I don't need to do so many times comparisons.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source