def model_build(i):
    # create model
    model = Sequential()
    model.add(Dense(i, input_dim=35, init='uniform', activation='relu' , activity_regularizer=regularizers.activity_l1(10e-5)))
    model.add(Dense(35, init='uniform', activation='relu'))
    # Compile model
    model.compile(optimizer='adam', loss='mean_squared_error')
    # Fit the model
    model.fit(train_array, train_array, nb_epoch=150, batch_size=20, validation_data=(validation_array, validation_array))
    # evaluate the model
    decoded_output = model.predict(test_array)

    print test_array[0]
    print decoded_output[0]

    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(metrics.mean_squared_error(test_array[i], decoded_output[i]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_array["C35_g"]
    quntile = 0.80

    threshold = get_percentile_threshold(quntile, recons_err)
    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 1:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 1:
                tn += 1
            else:
                fn += 1

    recall = 100 * float(tp) / (tp + fn)
    print "Threshold :", threshold
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"
    print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
    print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
    print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(tp) / (2 * tp + fp + fn)

    return recall
Exemple #2
0
def model_build(i):

    sp_pcs = decomposition.PCA(n_components=i)
    sp_pcs.fit(train_array)
    code = sp_pcs.transform(test_array)
    out_put = sp_pcs.inverse_transform(code)

    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(metrics.mean_squared_error(test_array[i],
                                                     out_put[i]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_array["C42_normal."]
    quntile = 0.80

    threshold = get_percentile_threshold(quntile, recons_err)
    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 1:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 1:
                tn += 1
            else:
                fn += 1

    recall = 100 * float(tp) / (tp + fn)
    print "Threshold :", threshold
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"
    print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
    print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
    print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(
        tp) / (2 * tp + fp + fn)

    return recall
def model_build(i):
    global z_log_var
    global z_mean
    global latent_dim

    latent_dim = i
    x = Input(shape=(original_dim, ))
    h = Dense(intermediate_dim, activation='relu')(x)
    z_mean = Dense(latent_dim)(h)
    z_log_var = Dense(latent_dim)(h)

    # note that "output_shape" isn't necessary with the TensorFlow backend
    z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var])

    # we instantiate these layers separately so as to reuse them later
    decoder_h = Dense(intermediate_dim, activation='relu')
    decoder_mean = Dense(original_dim, activation='sigmoid')
    h_decoded = decoder_h(z)
    x_decoded_mean = decoder_mean(h_decoded)

    vae = Model(x, x_decoded_mean)
    vae.compile(optimizer='rmsprop', loss=vae_loss)

    vae.fit(train_array,
            train_array,
            shuffle=True,
            nb_epoch=nb_epoch,
            batch_size=batch_size,
            validation_data=(validation_array, validation_array))

    # build a model to project inputs on the latent space
    # encoder, from inputs to latent space
    encoder = Model(x, z_mean)

    # generator, from latent space to reconstructed inputs
    decoder_input = Input(shape=(latent_dim, ))
    _h_decoded = decoder_h(decoder_input)
    _x_decoded_mean = decoder_mean(_h_decoded)
    generator = Model(decoder_input, _x_decoded_mean)

    decoded_output = vae.predict(test_array, batch_size=batch_size)

    for i in range(len(test_array[0])):
        print test_array[0][i], " ==> ", decoded_output[0][i]

    enc = encoder.predict(test_array, batch_size=batch_size)
    decoded_output = generator.predict(enc, batch_size=batch_size)

    for i in range(len(test_array[0])):
        print test_array[0][i], " ==> ", decoded_output[0][i]

    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(
            metrics.mean_squared_error(test_array[i], decoded_output[i]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_array["C42_normal."]
    quntile = 0.95

    threshold = get_percentile_threshold(quntile, recons_err)
    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 1:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 1:
                tn += 1
            else:
                fn += 1

    recall = 100 * float(tp) / (tp + fn)
    print "Threshold :", threshold
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"
    print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
    print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
    print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(
        tp) / (2 * tp + fp + fn)
Exemple #4
0
def model_build(i):

    # autoencoder = Sequential()
    # autoencoder.add(Convolution1D(64, 3, activation='relu', border_mode='same', input_dim=118))
    # autoencoder.add(MaxPooling1D(2, border_mode='same'))
    # autoencoder.add(Convolution1D(32, 3, activation='relu', border_mode='same'))
    # autoencoder.add(MaxPooling1D(2, border_mode='same'))
    # autoencoder.add(Convolution1D(32, 3 , activation='relu', border_mode='same'))
    # # autoencoder.add(MaxPooling1D(2, border_mode='same'))
    # # autoencoder.add(Convolution1D(32, 3, activation='relu', border_mode='same'))
    # # autoencoder.add(Flatten())
    # # autoencoder.add(Dense(12, input_dim=32, init='uniform', activation='relu', activity_regularizer=regularizers.activity_l1(10e-5)))
    # autoencoder.add(UpSampling1D())
    # autoencoder.add(Convolution1D(32, 3, activation='relu', border_mode='same'))
    # autoencoder.add(UpSampling1D())
    # autoencoder.add(Convolution1D(64, 3, activation='relu'))
    # autoencoder.add( UpSampling1D())
    # autoencoder.add(Convolution1D(118, 3, activation='sigmoid', border_mode='same'))
    #
    # autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

    input_img = Input(shape=(1, 118))

    x = Convolution1D(32, 200, activation='relu',
                      border_mode='same')(input_img)
    # x = MaxPooling1D(2, border_mode='same')(x)
    # x = Convolution1D(8, 3, activation='relu', border_mode='same')(x)
    # x = MaxPooling1D(2, border_mode='same')(x)
    # x = Convolution1D(8, 3, activation='relu', border_mode='same')(x)
    encoded = MaxPooling1D(2, border_mode='same')(x)

    # at this point the representation is (8, 4, 4) i.e. 128-dimensional

    x = Convolution1D(32, 16, activation='relu', border_mode='same')(encoded)
    # x = UpSampling1D(1)(x)
    # x = Convolution1D(8, 3, activation='relu', border_mode='same')(x)
    # x = UpSampling1D(1)(x)
    # x = Convolution1D(16, 3, activation='relu')(x)
    # x = UpSampling1D(1)(x)
    decoded = Convolution1D(118, 35, activation='sigmoid',
                            border_mode='same')(x)

    autoencoder = Model(input_img, decoded)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

    decoded_imgs = autoencoder.predict(test_array)

    print decoded_imgs.shape
    decoded_imgs = np.reshape(decoded_imgs, (len(decoded_imgs), 1, 118))
    print decoded_imgs.shape

    print test_array[0][0]
    print decoded_imgs[0][0]

    # for i in range(1000):
    #     print test_array[0][0][i], " ==> ", decoded_imgs[0][0][i]
    #
    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(
            metrics.mean_squared_error(test_array[i][0], decoded_imgs[i][0]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_array["C42_normal."]
    quntile = 0.95

    threshold = get_percentile_threshold(quntile, recons_err)

    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 1:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 1:
                tn += 1
            else:
                fn += 1

    recall = 100 * float(tp) / (tp + fn)
    print "Threshold :", threshold
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"
    print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
    print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
    print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(
        tp) / (2 * tp + fp + fn)
Exemple #5
0
def model_build(i):
    # this is the size of our encoded representations
    encoding_dim = i  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats

    # this is our input placeholder
    input_img = Input(shape=(8, ))
    # "encoded" is the encoded representation of the input
    encoded = Dense(
        encoding_dim,
        activation='tanh',
        activity_regularizer=regularizers.activity_l1(10e-4))(input_img)
    # "decoded" is the lossy reconstruction of the input
    decoded = Dense(8, activation='tanh')(encoded)

    # this model maps an input to its reconstruction
    autoencoder = Model(input=input_img, output=decoded)

    # this model maps an input to its encoded representation
    encoder = Model(input=input_img, output=encoded)

    # create a placeholder for an encoded (32-dimensional) input
    encoded_input = Input(shape=(encoding_dim, ))
    # retrieve the last layer of the autoencoder model
    decoder_layer = autoencoder.layers[-1]
    # create the decoder model
    decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

    autoencoder.compile(optimizer='adam', loss='mean_squared_error')
    # autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

    hist = autoencoder.fit(train_array,
                           train_array,
                           nb_epoch=100,
                           batch_size=100,
                           shuffle=True,
                           validation_data=(validation_array,
                                            validation_array))

    # encode and decode some digits
    # note that we take them from the *test* set
    encoded_imgs = encoder.predict(test_array)
    decoded_imgs = decoder.predict(encoded_imgs)

    for i in range(len(test_array[0])):
        print test_array[0][i], " ==> ", decoded_imgs[0][i]

    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(
            metrics.mean_squared_error(test_array[i], decoded_imgs[i]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_frame["anomaly"]
    quntile = 0.995

    threshold = get_percentile_threshold(quntile, recons_err)

    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 0:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 0:
                tn += 1
            else:
                fn += 1

    print "maximum error in test data set : ", max(recons_err)
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"

    if tp + fp != 0:
        recall = 100 * float(tp) / (tp + fn)
        print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
        print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
        print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(
            tp) / (2 * tp + fp + fn)

    return recall
def model_build(i):
    global test_array
    global test_frame

    inputs = Input(shape=(timesteps, input_dim))
    encoded = LSTM(latent_dim)(inputs)

    decoded = RepeatVector(timesteps)(encoded)
    decoded = LSTM(input_dim, return_sequences=True)(decoded)

    sequence_autoencoder = Model(inputs, decoded)
    encoder = Model(inputs, encoded)

    # sequence_autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    sequence_autoencoder.compile(optimizer='adam', loss='mean_squared_error')

    sequence_autoencoder.fit(train_array,
                             train_array,
                             nb_epoch=10,
                             batch_size=100,
                             shuffle=True,
                             validation_data=(validation_array,
                                              validation_array))

    decoded_output = sequence_autoencoder.predict(test_array)
    decoded_output = np.reshape(decoded_output,
                                (len(decoded_output) * timesteps, input_dim))

    test_array = np.reshape(test_array,
                            (len(test_array) * timesteps, input_dim))
    # print decoded_output

    for i in range(len(test_array[0])):
        print test_array[0][i], " ==> ", decoded_output[0][i]
    #
    recons_err = []
    for i in range(len(test_array)):
        recons_err.append(
            metrics.mean_squared_error(test_array[i], decoded_output[i]))

    tp = 0
    fp = 0
    tn = 0
    fn = 0
    lbl_list = test_frame["Class"]
    quntile = 0.998

    threshold = get_percentile_threshold(quntile, recons_err)
    for i in range(len(recons_err)):
        if recons_err[i] > threshold:
            if lbl_list[i] == 0:
                fp += 1
            else:
                tp += 1
        else:
            if lbl_list[i] == 0:
                tn += 1
            else:
                fn += 1

    print "maximum error in test data set : ", max(recons_err)
    print "TP :", tp, "/n"
    print "FP :", fp, "/n"
    print "TN :", tn, "/n"
    print "FN :", fn, "/n"

    if tp + fp != 0:
        recall = 100 * float(tp) / (tp + fn)
        print "Recall (sensitivity) true positive rate (TP / (TP + FN)) :", recall
        print "Precision (TP / (TP + FP) :", 100 * float(tp) / (tp + fp)
        print "F1 score (harmonic mean of precision and recall (sensitivity)) (2TP / (2TP + FP + FN)) :", 200 * float(
            tp) / (2 * tp + fp + fn)