Ejemplo n.º 1
0
currentPath = str(Path().absolute())
filePath = currentPath + '\\testData\\'
dataList = os.listdir('testData')

# Initialize labels array
y = np.zeros(len(dataList))
X = np.empty((1, 990), float)

# Call the method to adjust the dataset and split it into train/test parts
X_test, y_test = prepare_dataset(X, y)

# Setup the model
model = Sequential([
    InputLayer((990, )),
    Dense(64),
    Dropout(0.5),
    ReLU(),
    Dense(32),
    Dropout(0.5),
    ReLU(),
    Dense(1, activation='sigmoid')
])
model.summary()
# Load weights
model.load_weights("nnData/weights4.h5")

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])

score = model.evaluate(X_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1] * 100))
Ejemplo n.º 2
0
def main():
    args = get_cmd_args()
    #set the vocabulary size and load and test the training data
    vocabulary_size = args.vocab_size
    max_words = args.max_words
    embedding_size = args.embedding_size
    batch_size = args.batch_size
    num_epochs = args.epochs

    (x_train, y_train), (x_test,
                         y_test) = imdb.load_data(num_words=vocabulary_size)
    print('Loaded dataset with {} training samples, {} test samples'.format(
        len(x_train), len(x_test)))

    print('---review---')
    print(x_train[6])
    print('---label---')
    print(y_train[6])

    #use the dictionary returned by imdb.get_word_index() to map review back to the original words
    word2id = imdb.get_word_index()
    id2word = {i: word for word, i in word2id.items()}
    print('---review with words---')
    print([id2word.get(i, ' ') for i in x_train[6]])
    print('---label---')
    print(y_train[6])

    # sen1 = "This is test sentence"
    # sen2id = sen1.get_word_index()
    # id2wordsen1 = {i: word for word, i in sen2id.items()}
    # print([id2word.get(i, ' ') for i in sen1])

    #maximum review length and minimum review length
    print('Maximum review length: {}'.format(
        len(max((x_train + x_test), key=len))))

    #print minimum review length
    print('Minimum review length: {}'.format(
        len(min((x_test + x_test), key=len))))

    #set max words to 500
    x_train = sequence.pad_sequences(x_train, maxlen=max_words)
    x_test = sequence.pad_sequences(x_test, maxlen=max_words)

    #keras model with LSTM layer
    model = Sequential()
    model.add(
        Embedding(vocabulary_size, embedding_size, input_length=max_words))
    model.add(LSTM(100))
    model.add(Dense(1, activation='sigmoid'))

    model.summary()

    #Evaluate and train model
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    #specify the batch size and the number of epochs

    x_valid, y_valid = x_train[:batch_size], y_train[:batch_size]
    x_train2, y_train2 = x_train[batch_size:], y_train[batch_size:]

    model.fit(x_train2,
              y_train2,
              validation_data=(x_valid, y_valid),
              batch_size=batch_size,
              epochs=num_epochs)

    #scores[1] will correspond to accuracy is we pass metrics=['accuracy]

    scores = model.evaluate(x_test, y_test, verbose=0)
    print('Test accuracy:', scores[1])
Ejemplo n.º 3
0
dimData = np.prod(train_images.shape[1:])
train_data = train_images.reshape(train_images.shape[0], dimData)
test_data = test_images.reshape(test_images.shape[0], dimData)

#convert data to float and scale values between 0 and 1
train_data = train_data.astype('float')
test_data = test_data.astype('float')
#scale data
train_data /= 255.0
test_data /= 255.0
#change the labels frominteger to one-hot encoding
train_labels_one_hot = to_categorical(train_labels)
test_labels_one_hot = to_categorical(test_labels)

#creating network
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(dimData, )))
model.add(Dense(512, activation='tanh'))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(train_data,
                    train_labels_one_hot,
                    batch_size=256,
                    epochs=3,
                    verbose=1,
                    validation_data=(test_data, test_labels_one_hot))
Ejemplo n.º 4
0
def ConvNet():

    net = Sequential(name='ConvNet')
    net.add(
        Dense(units=500,
              input_shape=(None, None, 3),
              activation='relu',
              name='DenseIn'))
    for idx in range(1, 3):
        net.add(
            Conv2D(filters=int(128 / idx),
                   kernel_size=(2, 2),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01),
                   name=('LoopConv1_' + str(idx))))
        net.add(
            Conv2D(filters=int(64 / idx),
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01),
                   name=('LoopConv2_' + str(idx))))
        net.add(Activation('relu', name=('ReluAct' + str(idx * 1))))
        net.add(
            Conv2D(filters=int(64 / idx),
                   kernel_size=(5, 5),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01),
                   name=('LoopConv3_' + str(idx))))
        net.add(
            MaxPool2D(pool_size=(2, 2),
                      strides=(2, 2),
                      name=('MaxPool' + str(idx * 1))))
        net.add(
            Dense(units=150,
                  activation='relu',
                  name=('DenseOut' + str(idx * 1))))
    # encoder end
    for idx in range(2, 0, -1):
        net.add(Dense(units=150, activation='relu'))
        net.add(UpSampling2D(size=(2, 2)))
        net.add(
            Conv2D(filters=int(64 / idx),
                   kernel_size=(5, 5),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01)))
        net.add(Activation('relu'))
        net.add(
            Conv2D(filters=int(64 / idx),
                   kernel_size=(3, 3),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01)))
        net.add(
            Conv2D(filters=int(128 / idx),
                   kernel_size=(2, 2),
                   padding='same',
                   kernel_initializer='he_uniform',
                   kernel_regularizer=l2(0.01)))
    net.add(Dense(units=500, activation='relu'))
    net.add(
        Conv2D(filters=3,
               kernel_size=(2, 2),
               activation='relu',
               padding='same'))

    return net
Ejemplo n.º 5
0
    def createRegularizedModel(self, inputs, outputs, hiddenLayers,
                               activationType, learningRate):
        bias = True
        dropout = 0
        regularizationFactor = 0.01
        model = Sequential()
        if len(hiddenLayers) == 0:
            model.add(
                Dense(self.output_size,
                      input_shape=(self.input_size, ),
                      kernel_initializer='lecun_uniform',
                      bias=bias))
            model.add(Activation("linear"))
        else:
            if regularizationFactor > 0:
                model.add(
                    Dense(hiddenLayers[0],
                          input_shape=(self.input_size, ),
                          kernel_initializer='lecun_uniform',
                          W_regularizer=l2(regularizationFactor),
                          bias=bias))
            else:
                model.add(
                    Dense(hiddenLayers[0],
                          input_shape=(self.input_size, ),
                          kernel_initializer='lecun_uniform',
                          bias=bias))

            if (activationType == "LeakyReLU"):
                model.add(LeakyReLU(alpha=0.01))
            else:
                model.add(Activation(activationType))

            for index in range(1, len(hiddenLayers)):
                layerSize = hiddenLayers[index]
                if regularizationFactor > 0:
                    model.add(
                        Dense(layerSize,
                              kernel_initializer='lecun_uniform',
                              W_regularizer=l2(regularizationFactor),
                              bias=bias))
                else:
                    model.add(
                        Dense(layerSize,
                              kernel_initializer='lecun_uniform',
                              bias=bias))
                if (activationType == "LeakyReLU"):
                    model.add(LeakyReLU(alpha=0.01))
                else:
                    model.add(Activation(activationType))
                if dropout > 0:
                    model.add(Dropout(dropout))
            model.add(
                Dense(self.output_size,
                      kernel_initializer='lecun_uniform',
                      bias=bias))
            model.add(Activation("linear"))
        optimizer = optimizers.RMSprop(lr=learningRate, rho=0.9, epsilon=1e-06)
        model.compile(loss="mse", optimizer=optimizer)
        model.summary()
        return model
Ejemplo n.º 6
0
def param_model():
    '''
    frame = {{choice([1, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24])}}
    hide_num = {{choice([1, 2, 3])}}
    hide_unit = {{choice([2, 4, 8, 16])}}
    lstm_unit = {{choice([2, 4, 8, 16])}}
    lr = {{choice([-2, -3, -4, -5, -6])}}
# {'frame': 1, 'hide_num': 1, 'hide_unit': 8, 'hide_unit_1': 8, 'lr': -4}
    lr = 10**lr
    '''
    frame = 8
    hide_num = 2
    hide_unit = 8
    lstm_unit = 8
    lr = 0.0001
    l2 = 0.0005

    model = Sequential()
    # model.add(LSTM(lstm_unit, batch_input_shape=(None, frame, 6), return_sequences=False, dropout=0.5, recurrent_dropout=0.5))
    # model.add(Dropout(0.5, batch_input_shape=(None, frame, 6)))
    model.add(Dense(lstm_unit, batch_input_shape=(None, frame, 3)))
    model.add(Flatten())
    model.add(Activation('relu'))
    for _ in range(hide_num):
        model.add(Dense(hide_unit))
        model.add(Activation('relu'))
    model.add(Dense(1))
    model.add(Activation('linear'))

    plot_model(model, to_file='model.png', show_shapes=True)
    model.compile(loss="mean_squared_error", optimizer="adam")

    # es = EarlyStopping(patience=30, monitor='loss', verbose=1, mode='auto')
    es = EarlyStopping(patience=5)
    rlr = ReduceLROnPlateau()

    file_list = glob.glob("./roted_data/rot*.csv")

    file_num = 0
    for train_file in file_list:
        log_path = './logs_curve/log_{}_{}_{}_{}_{}-{}/'.format(
            frame, hide_num, hide_unit, lstm_unit, lr, file_num)
        filepath = './saves_curve/models_{}_{}_{}_{}_{:.0f}-{}/'.format(
            frame, hide_num, hide_unit, lstm_unit, lr * 1000000, file_num)
        os.makedirs(filepath, exist_ok=True)
        tb = TensorBoard(log_dir=log_path)
        cp = ModelCheckpoint(filepath=filepath + 'model_{epoch:02d}.hdf5',
                             monitor='val_loss',
                             verbose=1,
                             save_best_only=True,
                             mode='auto')

        df = pd.read_csv(train_file)
        time_data = df.loc[:, 'dt[s]'].values
        pos_data = df.loc[:, 'pos_x':'pos_z'].values
        accel_data = df.loc[:, 'accel_x':'accel_z'].values

        width = frame
        times = time_data
        poses = pos_data
        accels = accel_data
        data, target = [], []
        prev_time = np.vstack((np.zeros(
            (1, 1)), times[:frame - 1].reshape(frame - 1, 1)))
        v = np.zeros_like(accels)
        i = 0
        for a in accels:
            if i > 0:
                dt = times[i] - times[i - 1]
                v[i] = v[i - 1] + a * 9.8 * dt
            else:
                v[i] = a * 9.8 * times[0]
            i = i + 1

        prev_time = np.vstack((np.zeros(
            (1, 1)), times[:frame - 1].reshape(frame - 1, 1)))
        for i in range(len(times) - width):
            time = times[i:i + width].reshape(width, 1)
            DT = (time - prev_time)
            temp = v[i:i + width, :]
            data.append(temp)
            dt = times[i + width] - times[i + width - 1]
            delta = poses[i + width] - poses[i + width - 1]
            dist = math.sqrt(delta[0] * delta[0] + delta[2] * delta[2])
            temp = dist / dt
            target.append(temp)
            prev_time = time
        pos_input, pos_target = np.array(data), np.array(target)

        print(pos_input[:, 0, 3:7])
        model.fit(pos_input,
                  pos_target,
                  epochs=1000,
                  verbose=1,
                  batch_size=10,
                  callbacks=[tb, es, cp],
                  validation_split=0.1,
                  shuffle=True)
        file_num = file_num + 1

    test_file = file_list[2]

    df = pd.read_csv(test_file)
    time_data = df.loc[:, 'dt[s]'].values
    pos_data = df.loc[:, 'pos_x':'pos_z'].values
    accel_data = df.loc[:, 'accel_x':'accel_z'].values

    width = frame
    times = time_data
    poses = pos_data
    accels = accel_data
    data, target = [], []
    prev_time = np.vstack((np.zeros(
        (1, 1)), times[:frame - 1].reshape(frame - 1, 1)))
    v = np.zeros_like(accels)
    i = 0
    for a in accels:
        if i > 0:
            dt = times[i] - times[i - 1]
            v[i] = v[i - 1] + a * 9.8 * dt
        else:
            v[i] = a * 9.8 * times[0]
        i = i + 1

    prev_time = np.vstack((np.zeros(
        (1, 1)), times[:frame - 1].reshape(frame - 1, 1)))
    for i in range(len(times) - width):
        time = times[i:i + width].reshape(width, 1)
        temp = v[i:i + width, :]
        data.append(temp)
        dt = times[i + width] - times[i + width - 1]
        delta = poses[i + width] - poses[i + width - 1]
        dist = math.sqrt(delta[0] * delta[0] + delta[2] * delta[2])
        temp = dist / dt
        target.append(temp)
        prev_time = time
    test_pos_input, test_pos_target = np.array(data), np.array(target)

    pred = np.array([[0.0]] * df.shape[0])
    pred[:frame] = test_pos_input[0, :]
    # evaluation
    total_loss = 0

    i = frame
    for pos_input in test_pos_input:
        pred[i] = model.predict(np.array([pos_input]))
        total_loss = total_loss + abs(test_pos_target[i - frame] - pred[i])
        i = i + 1

    pred = pred[frame:]
    total_loss = total_loss / test_pos_input.shape[0]
    print(test_pos_target.shape)
    print(pred.shape)
    print("total loss:\t{0}".format(total_loss))
    print("total loss sum:\t{0}".format(np.sum(total_loss)))

    return {'loss': np.sum(total_loss), 'status': STATUS_OK, 'model': model}
Ejemplo n.º 7
0
def CNN(conn, x_train, x_test, y_train, y_test, epochs, batch, learning_rate,
        n, project, uid, ptype):

    model = Sequential()

    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dense(n, activation='softmax'))

    model.compile(Adam(learning_rate=learning_rate),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(x_train,
                        y_train,
                        epochs=epochs,
                        batch_size=batch,
                        verbose=1)

    predicted = model.predict(x_test)

    predicted = np.argmax(predicted, axis=1)
    y_test = np.argmax(y_test, axis=1)

    print("Accuracy score : ", str(accuracy_score(y_test, predicted)))
    print("F1 score : ", str(f1_score(y_test, predicted, zero_division=1)))
    print("Confusion Matrix : ")
    print(confusion_matrix(y_test, predicted))

    losses = history.history['loss']

    plt.plot(losses)
    plt.show()

    message_to_send = "Saving Model...".encode("UTF-8")
    conn.send(len(message_to_send).to_bytes(2, byteorder='big'))
    conn.send(message_to_send)

    print("Saving Model ....\n")
    model.save(project + ".h5")

    message_to_send = "Converting Model...".encode("UTF-8")
    conn.send(len(message_to_send).to_bytes(2, byteorder='big'))
    conn.send(message_to_send)

    print("Conerting Model to .tflite ....\n")
    convert(project)

    message_to_send = "Pushing Model to cloud...".encode("UTF-8")
    conn.send(len(message_to_send).to_bytes(2, byteorder='big'))
    conn.send(message_to_send)

    print("pushing model to cloud...\n")
    push_model(project, uid, ptype)
    print("Pushing to cloud successful...\n")
Ejemplo n.º 8
0
 def _build_model:
     from keras import Sequential
     model = Sequential()
     model.add(Embedding(self.max_int))
     model.compile('rmsprop', 'mse')
     self._model = model
Ejemplo n.º 9
0
class CNN_run:

    model = Sequential()
    model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               activation="relu",
               padding="same",
               input_shape=(64, 64, 1)))
    model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(rate=0.25))
    model.add(
        Conv2D(filters=64,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(
        Conv2D(filters=64,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(rate=0.25))
    model.add(Flatten())
    model.add(Dense(1024, activation="relu"))
    model.add(BatchNormalization())
    model.add(Dropout(rate=0.4))
    model.add(Dense(2, activation="softmax"))
    #SVG(model_to_dot(model).create(prog='dot', format='svg'))

    model.compile(Adam(lr=0.001),
                  loss="binary_crossentropy",
                  metrics=["accuracy"])

    gen = ImageDataGenerator(rescale=1. / 255,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True)
    #gen=ImageDataGenerator()

    train_batches = gen.flow_from_directory(
        "/Users/dwp167/PycharmProjects/CNN_cs445/input/chest_xray/chest_xray/train",
        model.input_shape[1:3],
        color_mode="grayscale",
        shuffle=True,
        seed=1,
        batch_size=16)
    valid_batches = gen.flow_from_directory(
        "/Users/dwp167/PycharmProjects/CNN_cs445/input/chest_xray/chest_xray/val",
        model.input_shape[1:3],
        color_mode="grayscale",
        shuffle=True,
        seed=1,
        batch_size=16)
    test_batches = gen.flow_from_directory(
        "/Users/dwp167/PycharmProjects/CNN_cs445/input/chest_xray/chest_xray/test",
        model.input_shape[1:3],
        shuffle=False,
        color_mode="grayscale",
        batch_size=8)

    history1 = model.fit_generator(train_batches,
                                   steps_per_epoch=163,
                                   epochs=30,
                                   validation_data=valid_batches,
                                   validation_steps=624)
    test_accu = model.evaluate_generator(test_batches, steps=624)
    print('The testing accuracy is:', test_accu[1] * 100, '%')

    #  history1 = model.fit_generator(test_batches,validation_data=valid_batches,epochs=3)

    p = model.predict_generator(test_batches, verbose=True)

    pre = DataFrame(p)
    pre["filename"] = test_batches.filenames
    pre["label"] = (pre["filename"].str.contains("PNEUMONIA")).apply(int)
    pre['pre'] = (pre[1] > 0.5).apply(int)

    recall_score(pre["label"], pre["pre"])

    roc_auc_score(pre["label"], pre[1])

    true_positive_rate, false_positive_rate, threshold = roc_curve(
        pre["label"], pre[1])
    roc = DataFrame([true_positive_rate, false_positive_rate]).T
    #roc.plot(x=0,y=1)

    plt.plot(history1.history['accuracy'])
    plt.plot(history1.history['val_accuracy'])
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Training set', 'Validation set'], loc='upper left')
    plt.show()

    plt.plot(history1.history['val_loss'])
    plt.plot(history1.history['loss'])
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Training set', 'Test set'], loc='upper left')
    plt.show()
Ejemplo n.º 10
0
data_generator(normalGlob, abNormalGlob, 2)

# In[34]:

img_height = 256
img_width = 384

# build the VGG16 network
model = applications.VGG16(weights='imagenet',
                           include_top=False,
                           input_shape=(img_height, img_width, 3))

# In[11]:

top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))

# In[12]:

# model.add(top_model) this throws error alternative is below

new_model = Sequential()  #new model
for layer in model.layers:
    new_model.add(layer)

new_model.add(top_model)  # now this works
def create_models(X_train, list_labels):
    # build convolution model
    # input shape = (128, 128, 1)
    model = Sequential()
    input_shape = X_train.shape[1:]

    model.add(Conv2D(24, (5, 5), strides=(1, 1), input_shape=input_shape))
    model.add(MaxPooling2D((4, 2), strides=(4, 2)))
    model.add(Activation('relu'))

    model.add(Conv2D(48, (5, 5), padding="valid"))
    model.add(MaxPooling2D((4, 2), strides=(4, 2)))
    model.add(Activation('relu'))

    model.add(Conv2D(48, (5, 5), padding="valid"))
    model.add(Activation('relu'))

    model.add(Flatten())
    model.add(Dropout(rate=0.5))

    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(rate=0.5))

    model.add(Dense(len(list_labels)))
    model.add(Activation('softmax'))
    model.summary()

    MAX_EPOCHS = 50
    MAX_BATCH_SIZE = 20
    # learning rate reduction rate
    MAX_PATIENT = 2

    # saved model checkpoint file
    best_model_file = "./best_model_trained.hdf5"

    # callbacks
    # removed EarlyStopping(patience=MAX_PATIENT)
    callback = [
        ReduceLROnPlateau(patience=MAX_PATIENT, verbose=1),
        ModelCheckpoint(filepath=best_model_file,
                        monitor='loss',
                        verbose=1,
                        save_best_only=True)
    ]

    # compile
    model.compile(optimizer="Adam",
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])

    # train
    print('training started.... please wait!')

    checkpoint = ModelCheckpoint(best_model_file,
                                 monitor='val_acc',
                                 verbose=0,
                                 save_best_only=True,
                                 mode='max')
    callbacks_list = [checkpoint]

    history = model.fit(x=X_train,
                        y=y_train,
                        epochs=MAX_EPOCHS,
                        batch_size=MAX_BATCH_SIZE,
                        verbose=1,
                        validation_data=(X_val, y_val),
                        callbacks=callbacks_list)
    history = model.fit(X_train,
                        y_train,
                        batch_size=MAX_BATCH_SIZE,
                        nb_epoch=MAX_EPOCHS,
                        verbose=1)

    print('training finished')

    # quick evaludate model
    print('Evaluate model with test data')
    score = model.evaluate(x=X_test, y=y_test)

    print('test loss:', score[0])
    print('test accuracy:', score[1])
Ejemplo n.º 12
0
def MLP(name, input_dir, best_dir, output):

    if not os.path.exists(best_dir):
        os.makedirs(best_dir)
    best_dir_dat = "/".join((best_dir, name))
    if not os.path.exists(best_dir_dat):
        os.makedirs(best_dir_dat)

    colnames = "HType,ABType,dimension,learnFac,margin,constr,LType,MLP_acc,MLP_wF1,MLP_epoch"
    with open(output, "w") as file:
        file.write(colnames)
        file.write("\n")

    models = sorted(os.listdir(input_dir))
    for model in models:
        modelpath = "/".join((input_dir, model))
        files = sorted(os.listdir(modelpath))

        # create model subdir to store best MLP models
        best_subdir = "/".join((best_dir_dat, model))
        if not os.path.exists(best_subdir):
            os.makedirs(best_subdir)

        for i, file in enumerate(files):
            print(i)

            # embedding datasets
            labelpath = "/".join((modelpath, file))
            dataset = pd.read_csv(labelpath, index_col=0)

            # specify file path to store best MLP model [for later]
            filepath = best_subdir + "/" + file[:-4] + ".hdf5"

################################################################################
############################# DATA SPLIT ##############################
################################################################################

            lb = preprocessing.LabelBinarizer()
            lb.fit(list(dataset["pgroup"]))

            X_train = dataset[dataset["split"] == "LRN"].iloc[:,1:-2].values
            y_train = dataset[dataset["split"] == "LRN"].iloc[:,-1].values
            # get weights first
            weights = compute_class_weight("balanced", np.unique(y_train), y_train)
            # then transform
            y_train = lb.transform(y_train)

            X_valid = dataset[dataset["split"] == "VLD"].iloc[:,1:-2].values
            y_valid = dataset[dataset["split"] == "VLD"].iloc[:,-1].values
            y_valid = lb.transform(y_valid)

            X_test = dataset[dataset["split"] == "TST"].iloc[:,1:-2].values
            y_test = dataset[dataset["split"] == "TST"].iloc[:,-1].values
            y_test = lb.transform(y_test)

################################################################################
############################# CLASSIFIER STRUCTURE ##############################
################################################################################

            classifier = Sequential()

            dim = len(dataset.iloc[0,1:-2])
            nodes = dim*2

            # Hidden layer
            classifier.add(Dense(nodes, activation="sigmoid",
            kernel_initializer="uniform", input_dim=dim))

            # Output layer
            classifier.add(Dense(8, activation="softmax",
            kernel_initializer="uniform"))

            # compile the model
            sgd = optimizers.SGD(lr=0.01, decay=0.0, momentum=0.0, nesterov=False)
            classifier.compile(optimizer=sgd, loss="categorical_crossentropy",
            metrics=["accuracy"])

################################################################################
############################# MODEL FITTING ##############################
################################################################################

            # checkpoint best model
            checkpoint = ModelCheckpoint(filepath, monitor="val_acc",
            verbose=0, save_best_only=True, mode="auto")

            # model settings and fit
            history = classifier.fit(X_train, y_train, validation_data=(X_valid, \
            y_valid), epochs=5000, verbose=0, callbacks=[checkpoint], \
            class_weight=weights)

################################################################################
############################# MAKE PREDICTIONS ##############################
################################################################################

            #load best model
            final_model = load_model(filepath)

            # get accuracy
            scores = final_model.evaluate(X_test, y_test, verbose=0)

            # get weighted F1-by-class
            le = preprocessing.LabelEncoder()
            le.fit(list(dataset["pgroup"]))
            y_test2 = dataset[dataset["split"] == "TST"].iloc[:,-1].values
            y_test2 = le.transform(y_test2)
            y_pred = final_model.predict_classes(X_test, verbose=0)
            weighted_f1 = f1_score(y_test2, y_pred, average="weighted")

            # get best epoch
            acc_history = history.history["val_acc"]
            best_epoch = acc_history.index(max(acc_history)) + 1

            K.clear_session() # destroy TF graph to avoid loop slowing down

################################################################################
############################# ASSEMBLE W/ CONFIG ##############################
################################################################################

            # get model type (H1-4, A/B)
            modelType = model.split("-")[1] # ["H1A"]
            HType = modelType[0:2]
            ABType = modelType[-1]
            # get dimension
            filenamesplit = file.split("-")
            dimension = int([s for s in filenamesplit if "D00" in s][0][1:])
            # get learnFac
            learnFac = int([s for s in filenamesplit if "LF0" in s][0][3:])
            # get margin
            margin = float([s for s in filenamesplit if "LM" in s][0][2:])
            # get constraint
            constr = [s for s in filenamesplit if "_VALUE" in s][0][:-6].lower()
            # get LType
            LType = filenamesplit[-1][:2]

            with open(output, "a") as file:
                file.write("%s,%s,%d,%d,%.1f,%s,%s,%.17f,%.17f,%d" % (HType, ABType, dimension, learnFac, margin, constr, LType, scores[1], weighted_f1, best_epoch))
                file.write("\n")
Ejemplo n.º 13
0
def main():
    # labels = pd.read_csv(data_dir+'labels.csv')
    # num_classes = len(labels.groupby('breed'))
    # selected_labels = labels.groupby('breed').count().sort_values(by='id',ascending=False).head(num_classes).index.values
    # labels = labels[labels['breed'].isin(selected_labels)]
    # labels['target'] = 1
    # labels['rank'] = labels.groupby('breed').rank()['id']
    # labels_pivot = labels.pivot('id', 'breed', 'target').reset_index().fillna(0) # values必须是breed和target对应的值
    # np.random.seed(SEED)
    # # rnd = np.random.random(len(labels))
    # # is_train = rnd < 0.8
    # # is_val = rnd >= 0.8
    # y_train = labels_pivot[selected_labels].values
    # # ytr = y_train[is_train]
    # # yv = y_train[is_val]
    # ytr = y_train
    #
    # x_train = np.zeros((len(labels), INPUT_SIZE, INPUT_SIZE, 3), dtype='float32')
    # for i, img_id in tqdm(enumerate(labels['id'])):
    #     # print i, img_id
    #     img = read_img(img_id, 'train', (INPUT_SIZE,INPUT_SIZE))
    #     x = xception.preprocess_input(np.expand_dims(img.copy(), axis=0))
    #     x_train[i] = x
    # print('Train Images shape: {} size: {:,}'.format(x_train.shape, x_train.size))
    #
    # num_tests = len(listdir(data_dir + '/test/'))
    # x_test = np.zeros((num_tests,INPUT_SIZE, INPUT_SIZE, 3), dtype='float32')
    # test_id = []
    # for i in range(num_tests):
    #     img_file_name = listdir(data_dir + '/test/')[i]
    #     img_id = img_file_name[0:len(img_file_name)-4]
    #     img = read_img(img_id, 'test',(INPUT_SIZE,INPUT_SIZE))
    #     x = xception.preprocess_input(np.expand_dims(img.copy(), axis=0))
    #     x_test[i] = x
    #     test_id.append(img_id)
    #
    # # xtr = x_train[is_train]
    # xtr = x_train
    # # xv = x_train[is_val]
    #
    # xception_bottleneck = xception.Xception(weights='imagenet', include_top=False, pooling=POOLING)
    # train_x_bf = xception_bottleneck.predict(xtr, batch_size=32, verbose=1)
    # valid_x_bf = xception_bottleneck.predict(x_test, batch_size=32, verbose=1)
    #
    # inception_bottleneck = inception_v3.InceptionV3(weights='imagenet', include_top=False, pooling=POOLING)
    # train_i_bf = inception_bottleneck.predict(xtr, batch_size=32, verbose=1)
    # valid_i_bf = inception_bottleneck.predict(x_test, batch_size=32, verbose=1)
    #
    # train_x = np.hstack([train_x_bf, train_i_bf])
    # test_x = np.hstack([valid_x_bf, valid_i_bf])
    # data = {'train_x':train_x, 'train_y':ytr, 'test_x':test_x}
    # with open('x_inception_fc_data.pickle', 'wb') as handle:
    #     pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL)
    with open(data_dir + 'xicpt_data.pickle', 'rb') as handle:
        load_data = pickle.load(handle)

    fc = Sequential()
    fc.add(Dense(500, activation='relu', input_shape=(4096, )))
    fc.add(Dropout(0.2))
    fc.add(BatchNormalization())
    fc.add(Dense(load_data['num_class'], activation='softmax'))
    fc.compile(loss='categorical_crossentropy',
               optimizer='sgd',
               metrics=['accuracy'])
    fc.fit(load_data['train_x'],
           load_data['train_y'],
           batch_size=32,
           epochs=10,
           verbose=1)
    valid_probs = fc.predict(load_data['test_x'])

    # without formatting
    df1 = {'id': load_data['test_id']}
    res1 = pd.DataFrame(data=df1)
    res2 = pd.DataFrame(columns=load_data['selected_labels'], data=valid_probs)
    res = pd.concat([res1, res2], axis=1)
    # res.to_csv("./x_icpt_fc"+str(dropout)+"_"+str(output)+"_"+str(opt)+"_"+str(batch)+".csv", index=False)

    # format as the sample submission
    sample_submission = pd.read_csv(data_dir + 'sample_submission.csv')
    sample_ids = list(sample_submission['id'])
    sample_breeds = list(sample_submission.columns.values)[1:]
    reorder_df = res.set_index('id')
    reorder_df = reorder_df.loc[sample_ids].reset_index().reindex(
        columns=['id'] + sample_breeds)
    reorder_df.to_csv("./xicpt_fcbnorm.csv", index=False)
Ejemplo n.º 14
0
plt.figure(figsize=(10, 10))

for c in range(16):
    plt.subplot(4, 4, c + 1)
    plt.imshow(x_train[c].reshape(28, 28), cmap='gray')

plt.show()
print(y_train[:16])
"""# Modeling"""

model = Sequential([
    Conv2D(input_shape=(28, 28, 1), kernel_size=3, filters=32),
    MaxPool2D(strides=2),
    Conv2D(kernel_size=3, filters=64),
    MaxPool2D(strides=2),
    Conv2D(kernel_size=3, filters=128),
    Flatten(),
    Dense(128, activation='relu'),
    Dropout(0.3),
    Dense(10, activation='softmax')
])

model.compile(optimizer=optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()
"""## Training"""

history = model.fit(x_train, y_train, epochs=10, validation_split=0.25)
"""# Evaluation"""
Ejemplo n.º 15
0
y_test = scalerY.transform(y_test)

#neural network training definition
layers = [
    Dense(100, input_shape=(exampleData.shape[1], )),
    Dropout(0.1),
    Dense(100, activation='relu'),
    Dropout(0.1),
    Dense(100, activation='relu'),
    Dropout(0.1),
    Dense(100, activation='relu'),
    Dropout(0.1),
    Dense(1)
]

model = Sequential(layers)

#train the model
model.compile(optimizer="SGD",
              loss='mean_squared_error',
              metrics=['mean_absolute_error'])
model.fit(X_train, y_train, epochs=1000, verbose=0)

#evaluate the model (still scalled wierdly)
#print(model.evaluate(x=X_train, y=y_train, verbose=0)) #to check for overfitting
print(model.evaluate(x=X_test, y=y_test, verbose=0))  #acutal score

#print 8 potential squares
print(scalerY.inverse_transform(y_test[0:8]))
print(scalerY.inverse_transform(model.predict(X_test[0:8])))
Ejemplo n.º 16
0
def transfer_and_fine_tune(sentences_train, sentences_test, targets_train, targets_test):
    """
        Create and train multi level perceptron with Keras API and save it.

        :param1 sentences_train: train sentences.
        :param2 sentences_test: test sentences.
        :param3 targets_train: train target embeddings.
        :param4 targets_test: test target embeddings.
    """

    from keras import Sequential
    from keras.callbacks import EarlyStopping
    from keras.callbacks import ModelCheckpoint
    from keras.layers.core import Dense, Activation
    from keras.models import model_from_json
    from keras.optimizers import Adam

    print("GUSE version")

    # transfer learning
    print("TRANSFER LEARNING STEP:")

    model = Sequential()
    encoderlayer = hub.KerasLayer(c.GUSE_PATH, input_shape=[], dtype=tf.string, trainable=False)
    model.add(encoderlayer)
    n_hidden = encoderlayer.output_shape[1]
    model.add(Dense(int(n_hidden)))
    model.add(Activation('relu'))
    model.add(Dense(int(n_hidden * SCALE_1)))
    model.add(Activation('relu'))
    model.add(Dense(int(n_hidden * SCALE_1 * SCALE_1)))
    model.add(Activation('relu'))
    model.add(Dense(len(targets_train[0]), activation='linear'))

    # compile model
    model.compile(loss=cosine_distance, optimizer='adam', metrics=['cosine_proximity'])

    es = EarlyStopping(monitor='val_loss', mode='min', verbose=c.LOG_LEVEL, patience=c.PATIENCE)

    best_weights_file = c.TL_MODEL_WEIGHTS_FILE_NAME
    mc = ModelCheckpoint(best_weights_file, monitor='val_loss', mode='min', verbose=c.ONE_LINE_PER_EPOCH,
                         save_best_only=True)
    # train model testing it on each epoch
    model.fit(sentences_train, targets_train, validation_data=(sentences_test, targets_test),
              batch_size=c.BATCH_SIZE, epochs=c.MAX_EPOCHS, verbose=c.ONE_LINE_PER_EPOCH, callbacks=[es, mc])
    # serialize model to JSON
    model_json = model.to_json()
    with open(c.TL_MODEL_JSON_FILE_NAME, "w") as json_file:
        json_file.write(model_json)
    print("model saved")

    # fine tuning
    print("FINE TUNING STEP:")
    # Model reconstruction from JSON file
    with open(c.TL_MODEL_JSON_FILE_NAME, 'r') as f:
        json = f.read()
    model = model_from_json(json, custom_objects={'KerasLayer': hub.KerasLayer})

    # Load weights into the new model
    model.load_weights(c.TL_MODEL_WEIGHTS_FILE_NAME)

    model.trainable = True
    # compile model
    model.compile(loss=cosine_distance, optimizer=Adam(3e-5), metrics=['cosine_proximity'])

    es = EarlyStopping(monitor='val_loss', mode='min', verbose=c.LOG_LEVEL, patience=c.PATIENCE)

    best_weights_file = c.FT_MODEL_WEIGHTS_FILE_NAME
    mc = ModelCheckpoint(best_weights_file, monitor='val_loss', mode='min', verbose=c.ONE_LINE_PER_EPOCH,
                         save_best_only=True)
    # train model testing it on each epoch
    model.fit(sentences_train, targets_train, validation_data=(sentences_test, targets_test),
              batch_size=c.BATCH_SIZE, epochs=c.MAX_EPOCHS, verbose=c.ONE_LINE_PER_EPOCH, callbacks=[es, mc])
    # serialize model to JSON
    model_json = model.to_json()
    with open(c.FT_MODEL_JSON_FILE_NAME, "w") as json_file:
        json_file.write(model_json)
    print("model saved")
Ejemplo n.º 17
0
test_images = test_images / 255.0

plt.figure(figsize=(10, 10))
for i in range(25):
    plt.subplot(5, 5, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
# plt.show()
print(train_images.shape, train_labels.shape)
print(train_images[0])
print(train_labels[:10])
model1 = Sequential([layers.Flatten(input_shape=(28, 28)),
                     layers.Dense(128, activation='relu'),
                     layers.Dense(10, activation='softmax')])
print(model1.summary())
model1.compile(optimizer='adam',
               loss='sparse_categorical_crossentropy',
               metrics=['accuracy'])
model1.fit(train_images, train_labels, epochs=5)
test_loss, test_accuracy = model1.evaluate(test_images, test_labels, verbose=2)
print("test accuracy:", test_accuracy)

##################################################

predictions = model1.predict(test_images)

num_rows = 5
num_cols = 3
Ejemplo n.º 18
0
def SegNet():
    model = Sequential()
    #encoder
    model.add(
        Conv2D(64, (3, 3),
               strides=(1, 1),
               input_shape=(img_w2, img_h2, 3),
               padding='same',
               activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #(128,128)
    model.add(
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #(64,64)
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #(32,32)
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #(16,16)
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #(8,8)
    #decoder
    model.add(UpSampling2D(size=(2, 2)))
    #(16,16)
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(UpSampling2D(size=(2, 2)))
    #(32,32)
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(UpSampling2D(size=(2, 2)))
    #(64,64)
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(UpSampling2D(size=(2, 2)))
    #(128,128)
    model.add(
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(UpSampling2D(size=(2, 2)))
    #(256,256)
    model.add(
        Conv2D(64, (3, 3),
               strides=(1, 1),
               input_shape=(img_w2, img_h2, 3),
               padding='same',
               activation='relu'))
    model.add(BatchNormalization())
    model.add(
        Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu'))
    model.add(BatchNormalization())
    model.add(Conv2D(n_label, (1, 1), strides=(1, 1), padding='same'))
    model.add(Reshape((n_label, img_w2 * img_h2)))
    model.add(Permute((2, 1)))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    model.summary()
    return model
Ejemplo n.º 19
0
def nn_with_past_outliers_multi_step_forecast(series,
                                              validation_series,
                                              input_length,
                                              horizon,
                                              del_outliers=False,
                                              normalize=False,
                                              plot=False):
    """
    Perform forecasting of a time series using a simple neural network with a single 128 neurons hidden layer.
    The network is trained using samples of shape input_length (corresponding to the last input_length days) to predict
    an array of horizon values (corresponding to horizon days). In this case, the network predicts horizon days at the
    time. Performance of the trained network is assessed on a validation series. The size of the validation series must
    be horizon.

    This function differs from nn_multi_step_forecast as in addition to the last input_length days, we also use horizon
    days at the same period the previous year as an input to the network. In addition, the horizon days from the
    previous year are normalized from the original series and contain the outliers. The hope is to gain information from
    the previous year.

    :param series:
    :param validation_series:
    :param input_length:
    :param horizon:
    :param del_outliers:
    :param normalize:
    :param plot:
    :return: SMAPE for the validation series, the forecast validation series
    """

    # whether to remove outliers in the training series
    if del_outliers:
        working_series = remove_outliers(series)

    else:
        working_series = series

    # whether to normalize the training series
    if normalize:
        scaler, working_series = normalize_series(working_series)
        scaler_bis, working_series_with_outliers = normalize_series(series)
    else:
        scaler = None
        working_series_with_outliers = series

    # input sequence is our data, np.log1p is applied to the data and mae error is used to approximate SMAPE error
    train_series = np.log1p(working_series)

    # we use the last n_steps_in days as input and predict n_steps_out
    n_steps_in, n_steps_out = input_length, horizon

    # split into samples
    train_samples, train_targets = split_sequence_nn_with_past_outliers_multi_step(
        train_series, working_series_with_outliers, n_steps_in, n_steps_out)

    # create the model
    model = Sequential()
    model.add(Dense(128, activation='relu', input_dim=n_steps_in + horizon))

    # we predict n_steps_out values
    model.add(Dense(n_steps_out))

    # we use 'mae' with data transformed with log1p and expm1 to approach SMAPE error
    model.compile(optimizer='adam', loss='mae')

    # fit model
    model.fit(train_samples, train_targets, epochs=200, verbose=0)

    # perform prediction

    # input is the last n_steps_in values of the train series (working_series is not log1p transformed)
    # in addition, we prepend the horizon values from the last year
    validation_in_sample = np.log1p(
        np.append(
            np.array(working_series_with_outliers.values[-365:-365 + horizon]),
            np.array(working_series.values[-n_steps_in:])))
    validation_in_sample = validation_in_sample.reshape(
        (1, n_steps_in + horizon))
    validation_forecast = model.predict(validation_in_sample, verbose=0)

    # dataframe which contains the result
    forecast_dataframe = pd.DataFrame(index=validation_series.index)

    # if data was normalized, we need to apply the reverse transform
    if normalize:

        # first reverse log1p using expm1
        validation_forecast = np.expm1(validation_forecast)

        # use scaler to reverse normalizing
        denormalized_forecast = scaler.inverse_transform(
            validation_forecast.reshape(-1, 1))
        denormalized_forecast = [val[0] for val in denormalized_forecast]

        # save the forecast in the dataframe
        forecast_dataframe['forecast'] = denormalized_forecast

    else:

        # save the forecast in the dataframe
        forecast_dataframe['forecast'] = np.expm1(validation_forecast)

    if plot:
        plt.figure(figsize=(10, 6))

        plt.plot(series[-100:], color="blue", linestyle="-")
        plt.plot(validation_series, color="green", linestyle="-")
        plt.plot(forecast_dataframe, color="red", linestyle="--")

        plt.legend(["Train series", "Validation series", "Predicted series"])

        plt.title(
            "Validation of simple multi step NN with past values and input size "
            + str(n_steps_in) + " output size " + str(n_steps_out))
        plt.show()

    return smape(
        validation_series,
        forecast_dataframe['forecast']), forecast_dataframe['forecast']
def build_model(hype_space):
    """Create model according to the hyperparameter space given."""
    print("Hyperspace:")
    print(hype_space)
    model = Sequential()

    # Define parameters to goverrn construction of model according to hype_space
    n_filters = int(round(hype_space['nb_conv_filters']))
    if hype_space['nb_conv_in_conv_pool_layers'] == 2:
        two_conv_layers = True
    else:
        two_conv_layers = False

    # first conv+pool layer
    model.add(first_convolution(n_filters, hype_space))
    if hype_space['conv_dropout'] is not None:
        model.add(conv_dropout(hype_space))
    if two_conv_layers:
        model.add(convolution(n_filters, hype_space))
        if hype_space['conv_dropout'] is not None:
            model.add(conv_dropout(hype_space))
    model.add(pooling(hype_space))

    # adds additional conv+pool layers based on hype_space
    for _ in range(hype_space['nb_conv_pool_layers'] - 1):
        model.add(convolution(n_filters, hype_space))
        if hype_space['conv_dropout'] is not None:
            model.add(conv_dropout(hype_space))
        if two_conv_layers:
            model.add(convolution(n_filters, hype_space))
            if hype_space['conv_dropout'] is not None:
                model.add(conv_dropout(hype_space))
        model.add(pooling(hype_space))

    # fully connected layers
    model.add(Flatten())

    model.add(keras.layers.core.Dense(
        units=int(round(hype_space['fc_nodes_1'])),
        activation=hype_space['activation'],
        kernel_regularizer=keras.regularizers.l2(hype_space['l2_weight_reg'])
    ))
    model.add(fc_dropout(hype_space))

    if hype_space['fc_second_layer'] is not None:
        model.add(keras.layers.core.Dense(
            units=int(round(hype_space['fc_second_layer'])),
            activation=hype_space['activation'],
            kernel_regularizer=keras.regularizers.l2(hype_space['l2_weight_reg'])))
        model.add(fc_dropout(hype_space))

    model.add(Dense(num_classes, activation='softmax'))

    # Finalize and compile model:
    model.compile(
        optimizer=OPTIMIZER_STR_TO_CLASS[hype_space['optimizer']](
            lr=hype_space['lr_rate']),
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )
    return model
def model_train_val_bow(X_train, X_val, y_train, y_val, vocab_size,
                        max_sent_len):
    BATCH_SIZE = 16
    EPOCHS = 300
    print(f"BATCH_SIZE: {BATCH_SIZE}")

    model = Sequential()

    # 128, 64, 64 or 32, 网络结构的调整. 效果没有提升,基本不变
    model.add(
        Embedding(
            input_dim=vocab_size + 1,
            output_dim=64,
            input_length=max_sent_len,
            mask_zero=True,
            name="embedding"))  # mask_zero=True 不比mask_zero=False好, 基本差不多
    # model.add(LSTM(units=64, return_sequences=True, dropout=0.25, name="lstm1"))
    model.add(LSTM(units=32, return_sequences=False, dropout=0.5,
                   name="lstm1"))

    # model.add(LSTM(units=128, return_sequences=False, dropout=0.25, name="lstm2"))

    model.add(Dense(units=5, activation="softmax", name="dense3"))
    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    early_stopping = EarlyStopping(monitor="val_loss", patience=10)
    lr_reduction = ReduceLROnPlateau(monitor="val_loss",
                                     patience=5,
                                     verbose=1,
                                     factor=0.2,
                                     min_lr=1e-5)
    # 检查最好模型: 只要有提升, 就保存一次
    model_path = "../data/output/models/best_model_{epoch:02d}_{val_loss:.2f}.hdf5"  # 保存到多个模型文件
    # model_path = "../data/output/models/best_model.hdf5"  # 保存到1个模型文件(因为文件名相同)
    checkpoint = ModelCheckpoint(filepath=model_path,
                                 monitor="val_loss",
                                 verbose=1,
                                 save_best_only=True,
                                 mode="min")
    # checkpoint = ModelCheckpoint(filepath=model_path, monitor="val_acc", verbose=1, save_best_only=True, mode="max")

    # hist_obj = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_split=0.1)
    hist_obj = model.fit(X_train,
                         y_train,
                         batch_size=BATCH_SIZE,
                         epochs=EPOCHS,
                         verbose=1,
                         validation_data=(X_val, y_val),
                         callbacks=[early_stopping, lr_reduction,
                                    checkpoint])  # shuffle默认值是True
    """
    # NOTE: 上一行中validation_data改成validation_split=0.3后准确率下降了5~6个百分点:
    当使用validation_split时"The validation data is selected from the last samples in the x and y data provided, before shuffling."
    准确率下降可能和shuffle是有关的, 因为是在shuffle之前进行的切割,可能训练集、验证集的数据均衡性分布有问题,导致训练效果变差
    "shuffle: whether to shuffle the training data before each epoch"

    此外, 本来使用validation_split是想使用交叉验证的,但似乎并不会起到交叉验证的作用(不一定,需要进一步确认),
    只是把这些数据剔除出来不进行训练, 和sklearn的train_test_split是一样的,但train_test_split提供了shuffle,
    所以还是train_test_split好些
    """

    with open(f"../data/output/history_{BATCH_SIZE}.pkl", "wb") as f:
        pickle.dump(hist_obj.history, f)
Ejemplo n.º 22
0
def model_train_and_fit(samples_num,n_in=10,epochs=25, batch_size=32):
    values = ndata.iloc[:samples_num,:].values
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled = scaler.fit_transform(values)
    scaled_y = scaled[:,0:1]
    scaled_exc_y = scaled[:,1:]
    scaled_columns = ['speed_wind_30s_avr','temp_de','speed_generator','temp_nde','speed_rotor', 'speed_high_shaft', 'temp_ambient', 'temp_main_bearing']

    # 将序列数据转化为监督学习数据
    """
        Frame a time series as a supervised learning dataset.
        Arguments:
            data: Sequence of observations as a list or NumPy array.
            n_in: Number of lag observations as input (X).
            n_out: Number of observations as output (y).
            dropnan: Boolean whether or not to drop rows with NaN values.
        Returns:
            Pandas DataFrame of series framed for supervised learning.
    """
    # n_in = 10
    #调整n_in 即可

    # 将序列数据转化为监督学习数据
    reframed = series_to_supervised(scaled_exc_y, scaled_columns, n_in, 0)

    #对齐powert 与  t-5的数据
    scaled_y = scaled[:-n_in,0:1]
    reframed['power(t)'] = scaled_y



    values = reframed.values

    #划分训练集和测试集
    train_size = round(len(values)*0.67)
    train = values[:train_size,:]
    test = values[train_size:,:]
    train_x, train_y = train[:, :-1], train[:, -1]
    test_x, test_y = test[:, :-1], test[:, -1]

    # 为了在LSTM中应用该数据,需要将其格式转化为3D format,即[Samples, timesteps, features]
    train_X = train_x.reshape((train_x.shape[0],1, train_x.shape[1]))
    test_X = test_x.reshape(( test_x.shape[0],1, test_x.shape[1]))


    model = Sequential()
    #model.add(LSTM(10, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(LSTM(10,input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    history = model.fit(train_X, train_y, epochs=25, batch_size=32)
    # , validation_data=(test_X, test_y)

    '''
        对数据绘图
    '''
    plt.plot(history.history['loss'], label='train')
    # plt.plot(history.history['val_loss'], label='test')
    plt.legend()
    plt.show()

    # make the prediction,为了在原始数据的维度上计算损失,需要将数据转化为原来的范围再计算损失
    yHat = model.predict(test_X)

    '''
        这里注意的是保持拼接后的数组  列数  需要与之前的保持一致
    '''
    inv_yHat = concatenate((yHat, test_x[:,:8]), axis=1)   # 数组拼接
    inv_yHat = scaler.inverse_transform(inv_yHat)
    inv_yHat = inv_yHat[:, 0]

    test_y = test_y.reshape((len(test_y), 1))
    inv_y = concatenate((test_y, test_x[:, :8]), axis=1)
    inv_y = scaler.inverse_transform(inv_y)    # 将标准化的数据转化为原来的范围
    inv_y = inv_y[:, 0]

    rmse = sqrt(mean_squared_error(inv_yHat, inv_y))
    print('Test RMSE: %.3f' % rmse)

    ahead_second = n_in * 30




    plt.figure(12)
    plt.suptitle("%s s ahead,Test RMSE:%s"%(ahead_second,rmse))
    plt.subplot(221),plt.plot(inv_yHat,label='predict')
    plt.legend()
    plt.subplot(223),plt.plot(inv_y,label='raw')
    plt.legend()
    plt.subplot(122),plt.plot(inv_y,label='raw'),plt.plot(inv_yHat,label='predict')
    plt.legend()
    plt.show()
Ejemplo n.º 23
0
    def model(self, country):
        self.country = country
        time = list(range(1, len(self.df) + 1))
        # 解决画图时中文乱码问题
        from pylab import mpl
        mpl.rcParams['font.sans-serif'] = ['SimHei']
        # 试测Arima模型
        # Arima模型忽略了确诊,死亡,治愈的关联关系,只是单纯依靠时间做的预测
        # 进行ADF检验
        '''
        pList = []
        for i in range(1, 10):
            series = self.df['China'].diff(2)
            series.fillna(0, inplace=True)
            p = adfuller(series)[1]
            pList.append({str(i): p})  # 计算p值
        '''
        # 发现1-10阶的差分的数据都通不过显著性检验,即p>0.05 故而舍弃Arima算法
        #
        # 试测logistics曲线模型
        # 设置参数范围
        if self.method == 'logistics':
            bounds = ([self.df[country][1], self.df[country][1], 0], [
                self.df[country][len(time)], self.df[country][len(time)], 1
            ])
            # 开始最优化参数
            self.popt, self.pcov = curve_fit(Model_Function.logistics,
                                             time,
                                             self.df[country],
                                             bounds=bounds)
            x = self.dayTime
            y = self.df[country]
            y_pre = pd.Series([
                ceil(
                    Model_Function.logistics(time[i], self.popt[0],
                                             self.popt[1], self.popt[2]))
                for i in range(len(time))
            ])

        # 采用LSTM算法
        elif self.method == 'LSTM':
            # 设置随机种子
            np.random.seed(1)
            # 设置滑动窗口
            self.windows = 2
            # 标准化
            self.scaler = MinMaxScaler(feature_range=(0, 1))
            self.dataSet = pd.DataFrame(self.scaler.fit_transform(
                pd.DataFrame(self.df[country])),
                                        index=time)
            #dataSet = pd.DataFrame(scaler.fit_transform(self.df), columns=self.df.columns, index=self.df.index)
            # 创建LSTM数据集
            trainX, trainY = Model_Function.createLSTMDataSet(
                self.dataSet[0], self.windows)
            # 对x进行扩维
            trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

            # 搭建LSTM网络
            self.model = Sequential()
            self.model.add(LSTM(4, input_shape=(1, self.windows)))
            self.model.add(Dense(1))
            self.model.compile(loss='mean_squared_error', optimizer='adam')
            # 衰减学习率
            learn_rate = ReduceLROnPlateau(monitor='val_loss',
                                           patience=10,
                                           mode='max')
            # with open(os.path.abspath(os.path.dirname(__file__)) + '/result/learn_rate.txt', 'a', encoding='UTF-8') as f:
            #     f.write(self.country + "的" + self.name + "LSTM模型的衰减学习率为" + str(learn_rate) + "\n")
            #     f.close()
            self.model.fit(trainX,
                           trainY,
                           epochs=100,
                           batch_size=5,
                           verbose=2,
                           callbacks=[learn_rate])
            trainPredict = self.model.predict(trainX)
            # 反标准化
            trainPredict = self.scaler.inverse_transform(trainPredict)

            # 计算RMSE
            trainY = self.scaler.inverse_transform([trainY])
            trainScore = np.sqrt(sum((trainY[0, :] - trainPredict[:, 0])**
                                     2)) / (len(trainY) * max(trainY[0, :]))
            with open(os.path.abspath(os.path.dirname(__file__)) +
                      '/result/rmse.txt',
                      'a',
                      encoding='UTF-8') as f:
                f.write(self.country + "的" + self.name + "预测与实际的RMSE为" +
                        str(trainScore) + "\n")
                f.close()
            # print('Train Score: %.2f RMSE' % (trainScore))
            x = self.dayTime[self.windows:]
            y = self.df[country][self.windows:]
            y_pre = pd.Series(trainPredict[:, 0]).astype(int)
        else:
            raise Exception('方法不存在!')

        x = Model_Function.timeMonthDay(x)
        plt.figure(figsize=[80, 60])
        plt.scatter(x, y, s=35, c='blue', marker='+', label=self.name + '人数')
        plt.plot(x, y_pre, 'r-s', marker='+', linewidth=1.5, label="验证曲线")
        plt.tick_params(labelsize=8)
        plt.xlabel('日期', fontsize=2)
        plt.ylabel(self.name + '人数')
        x_major_locator = MultipleLocator(5)
        # 把x轴的刻度间隔设置为5,并存在变量里
        ax = plt.gca()
        # ax为两条坐标轴的实例
        ax.xaxis.set_major_locator(x_major_locator)
        plt.legend(loc=0)
        plt.title(self.country + "的新冠病毒疫情的" + self.name + "发展及预测情况")
Ejemplo n.º 24
0
def deep2_cnn_arch(x_train, y_train, epochs, batch_size, validation_split):
    model = Sequential()
    model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               padding='same',
               activation='relu',
               input_shape=(48, 48, 1)))
    conv_arch = [(32, 3), (64, 3), (128, 3)]
    dense = [64, 2]
    if (conv_arch[0][1] - 1) != 0:
        for i in range(conv_arch[0][1] - 1):
            model.add(
                Conv2D(conv_arch[0][0],
                       kernel_size=(3, 3),
                       padding='same',
                       activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

    if conv_arch[1][1] != 0:
        for i in range(conv_arch[1][1]):
            model.add(
                Conv2D(conv_arch[1][0],
                       kernel_size=(3, 3),
                       padding='same',
                       activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

    if conv_arch[2][1] != 0:
        for i in range(conv_arch[2][1]):
            model.add(
                Conv2D(conv_arch[2][0],
                       kernel_size=(3, 3),
                       padding='same',
                       activation='relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())  # this converts 3D feature maps to 1D feature vectors
    if dense[1] != 0:
        for i in range(dense[1]):
            model.add(Dense(dense[0], activation='relu'))
            model.add(Dropout(0.5))
    model.add(Dense(y_train.shape[1], activation='softmax'))
    # 16 layers
    # optimizer:
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    print('Training....')
    hist = model.fit(x_train,
                     y_train,
                     epochs=epochs,
                     batch_size=batch_size,
                     validation_split=validation_split,
                     shuffle=True,
                     verbose=2)
    train_val_accuracy = hist.history
    train_acc = train_val_accuracy['acc']
    val_acc = train_val_accuracy['val_acc']
    print('          Done!')
    print('     Train acc: ', train_acc[-1])
    print('Validation acc: ', val_acc[-1])
    print(' Overfit ratio: ', val_acc[-1] / train_acc[-1])
    save_model(model, index="deep2")
Ejemplo n.º 25
0
def get_model5():
    model = Sequential()
    model.add(Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.01)))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[auc_roc])
    return model
categorical_featuresX = sc.fit_transform(categorical_features)

X = np.concatenate(
    (gaussian_featuresX, categorical_featuresX, disease_data.iloc[:, 10:13]),
    axis=1)  # Reassembling the data after nomalization
Y = disease_data.iloc[:, 13]
sc = StandardScaler()
X = sc.fit_transform(X)

# Splitting data for Training and Test sets

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30)

## CREATING NEURAL NETWORKS

classifier = Sequential()
# First Hidden Layer
classifier.add(
    Dense(7,
          activation='tanh',
          kernel_initializer='random_normal',
          input_dim=13))
# Second  Hidden Layer
classifier.add(Dense(7, activation='tanh', kernel_initializer='random_normal'))
# Output Layer
classifier.add(
    Dense(1, activation='sigmoid', kernel_initializer='random_normal'))

# Compiling the neural network

classifier.compile(optimizer='adam',
# Turn into an array
X_train = X_train.values
X_test = X_test.values
y_train = y_train.values
y_test = y_test.values

# Keras || OverSampling (SMOTE):
# Synthetic Minority Over-sampling TEchnique - It will create the new point between minority values within the minority range.
sm = SMOTE(random_state=42)
Xsm_train, ysm_train = sm.fit_sample(X_train, y_train)

n_inputs = Xsm_train.shape[1]
print('model input=', n_inputs)
model = Sequential([
    Dense(n_inputs, input_shape=(n_inputs, ), activation='relu'),
    Dense(32, activation='relu'),
    Dense(10, activation='relu'),
    Dense(2, activation='softmax')
])

model.compile(Adam(lr=0.002),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())

model.fit(Xsm_train,
          ysm_train,
          validation_split=0.2,
          batch_size=300,
          epochs=20,
          shuffle=True,
Ejemplo n.º 28
0

# Fill missing or null values with mean column values in the train set
import numpy as np




# Encoding categorical data
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing
labelEncoder = preprocessing.LabelEncoder()
labelEncoder.fit(dataset['diagnosis'])
dataset['diagnosis'] = labelEncoder.transform(dataset['diagnosis'])
dataset=dataset.values

X_train, X_test, Y_train, Y_test = train_test_split(dataset[:,2:32], dataset[:,1],
                                                    test_size=0.25, random_state=87)
np.random.seed(155)
my_first_nn = Sequential() # create model
my_first_nn.add(Dense(10, input_dim=30, activation='relu'))# hidden layer
my_first_nn.add(Dense(32, activation='softplus'))
#my_first_nn.add(Dense(156, activation='sigmoid')) #added one laye
#my_first_nn.add(Dense(7, activation='sigmoid')) #added another layer
my_first_nn.add(Dense(1, activation='sigmoid')) # output layer
my_first_nn.compile(loss='binary_crossentropy', optimizer='adam')
my_first_nn_fitted = my_first_nn.fit(X_train, Y_train, epochs=100, verbose=0,
                                     initial_epoch=0)
print(my_first_nn.summary())
print(my_first_nn.evaluate(X_test, Y_test, verbose=0))
Ejemplo n.º 29
0
                     tied_to=encoder1,
                     use_bias=False)

encoder2 = Dense(hidden_dim,
                 activation="relu",
                 input_shape=(encoding_dim, ),
                 use_bias=True,
                 kernel_regularizer=WeightsOrthogonalityConstraint(
                     encoding_dim, weightage=1., axis=0),
                 kernel_constraint=UnitNorm(axis=0))
decoder2 = DenseTied(encoding_dim,
                     activation="relu",
                     tied_to=encoder2,
                     use_bias=False)

autoencoder = Sequential()
autoencoder.add(encoder1)
autoencoder.add(encoder2)
autoencoder.add(decoder2)

autoencoder.add(decoder1)
autoencoder.summary()

autoencoder.compile(metrics=['accuracy'],
                    loss='mean_squared_error',
                    optimizer='adam')

#earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')
mcp_save = ModelCheckpoint('autoencoder_classifier.h5',
                           save_best_only=True,
                           monitor='val_loss',
Ejemplo n.º 30
0
print((cv_results["test-rmse-mean"]).tail(1))


## Neural Network with Keras
#based off https://www.freecodecamp.org/news/how-to-build-your-first-neural-network-to-predict-house-prices-with-keras-f8db83049159/


#I'm also messing with the hyperparms as I go. Here's some notes so not to try things many times
#sequential layer, relu, relu, sigmoid works best
#softmax->relu

# tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

# define the keras model
from tensorflow.keras import layers
model = Sequential()
model.add(Dense(12, input_shape=((900,)), activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# model = Sequential([ Dense(40, activation='relu', input_shape=(900,)),  Dense(32, activation='relu'), Dense(1, activation='sigmoid')])
# model.compile(optimizer='sgd',   loss='binary_crossentropy', metrics=['accuracy'])
#adam is stochastic gradient descent algorithm
# model.compile(optimizer='adam',   loss='binary_crossentropy', metrics=['accuracy'])
model.compile(loss='mean_squared_error',optimizer='sgd',metrics=[metrics.mae])
)
hist = model.fit(x_train, y_train, batch_size=64, epochs=100,validation_data=(x_train,y_train),verbose=0)
# model.evaluate(X_test, Y_test)[1]
Y_pred = model.predict(x_test)
loss_NN = model.evaluate(x_test, y_test, batch_size=128)
print(model.metrics_names)
print(loss_NN)