Skip to content

Instantly share code, notes, and snippets.

@mvivarelli
mvivarelli / gist:11ead87de00ae1b8b9269b6694f6e3c1
Created October 20, 2021 08:44
Turbofan Engine stateful LSTM model
#model
model = Sequential()
#input
model.add(LSTM(units=50, return_sequences=True, activation='tanh', batch_size=batch_size, stateful=True, input_shape = (x_train_final.shape[1], x_train_final.shape[2])))
model.add(Dropout(0.2))
#hidden layer 1
model.add(LSTM(units=60, return_sequences=True, activation='tanh', stateful=True))
model.add(Dropout(0.2))
#hidden layer 2
model.add(LSTM(units=60, return_sequences=True, activation='tanh', stateful=True))
@mvivarelli
mvivarelli / gist:998689b00349c2007393f6d6eb3b8b0f
Last active November 10, 2021 10:23
weighted_categorical_crossentropy
def weighted_categorical_crossentropy(weights):
weights = K.variable(weights)
def loss(y_true, y_pred):
# scale predictions so that the class probas of each sample sum to 1
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
# clip to prevent NaN's and Inf's
y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
# calc