Example #1
0
    def create(cls, tokenizer: Tokenizer, hidden: int, dropout: float) -> "LanguageModel":
        from keras import Sequential
        from keras.layers import LSTM, Dropout, Dense

        if tokenizer.vocabulary_size == 0:
            logging.warning("Creating a model using a codec with an empty vocabulary.")
        model = Sequential()
        model.add(LSTM(hidden, input_shape=(tokenizer.context_size, 1)))
        model.add(Dropout(dropout))
        model.add(Dense(tokenizer.vocabulary_size, activation="softmax"))
        model.compile(loss="categorical_crossentropy", optimizer="adam")
        return cls(model, tokenizer)
Example #2
0
    def __init__(self, input_nodes, hidden_nodes, output_nodes, lr=None):
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes
        self.lr = lr
        self.scales_x = []
        self.scales_y = []

        input_kernel_range = np.sqrt(6) / (np.sqrt(input_nodes) + np.sqrt(hidden_nodes))
        input_kernel_initializer = RandomUniform(minval=-input_kernel_range, maxval=input_kernel_range)
        input_layer = Dense(input_nodes,
                            kernel_initializer=input_kernel_initializer,
                            name='input')

        hidden_kernel_range = np.sqrt(6) / (np.sqrt(hidden_nodes) + np.sqrt(output_nodes))
        hidden_kernel_initializer = RandomUniform(minval=-hidden_kernel_range, maxval=hidden_kernel_range)
        hidden_layer = Dense(hidden_nodes,
                             kernel_initializer=hidden_kernel_initializer,
                             name='hidden')

        output_layer = Dense(output_nodes,
                             name='output')

        self.model = Sequential()
        self.model.add(input_layer)
        self.model.add(hidden_layer)
        self.model.add(output_layer)
 def function_set(self):
     self.__net = Sequential()
     self.__net.add(GRU(units=5, input_length=5, input_dim=3))
     self.__net.add(Dense(units=1, activation="sigmoid"))
Example #4
0
max_features = 10000
maxlen = 500
batch_size = 32

(input_train, y_train), (input_test,
                         y_test) = imdb.load_data(num_words=max_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')

print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape ', input_train.shape)
print('input_test shape ', input_test.shape)

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(SimpleRNN(32))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train,
                    y_train,
                    epochs=10,
                    batch_size=128,
                    validation_split=0.2)

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
def build_lstm_model(X):
    # init sequential model
    model = Sequential()
    # vectorize input
    # input_dim (size of vocabulary) is 2500 because we have only 2500 most common words (line 53)
    # embed_dim (shape of output)
    # input_length (length of input sequences), just get shape of first value because lenghts of all values are same (line 59), also could be replaced with maxlen value
    model.add(Embedding(2500, embed_dim, input_length=X.shape[1]))
    # prevent from overfitting
    model.add(Dropout(0.2))
    # add LSTM layer with specified shape of output
    model.add(LSTM(lstm_out))
    # prevent from overfitting
    model.add(Dropout(0.2))
    # add Dense layer with specified shape of output, there are only 2 outputs, because review can be only positive or negative
    model.add(Dense(2, activation='softmax'))
    # compile model
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    return model
Example #6
0
def training(file_path, epochs, noise_std, do_gif=False):
    def create_gif(path, title, step=1):
        images = []
        for i, filename in enumerate(os.listdir(path)):
            if i % step == 0:
                images.append(imageio.imread(os.path.join(path, filename)))
        imageio.mimsave(os.path.join(ENC_PLOT_DATA_PATH, title),
                        images,
                        duration=0.1)

        shutil.rmtree(path)

    def get_data(file_path):
        # TODO: try using 2 proteins: train on the first one test on the other one
        data = read_data(file_path, normalize='std')
        x_train, x_test = train_test_split(data,
                                           test_size=0.1,
                                           random_state=42)
        return x_train, x_test

    def add_noise(x, std):
        return x + normal(0, std, size=x.shape)

    for file in os.listdir(file_path):
        if not (file.endswith(".csv")):
            continue
        name = file.replace(".csv", "")
        print("Analysing %s" % name)
        x_train, x_test = get_data(os.path.join(file_path, file))
        x_train_noisy, x_test_noisy = add_noise(
            x_train, std=noise_std), add_noise(x_test, std=noise_std)
        network_model = Sequential(input_size=x_train.shape[1], encoded_size=2)
        network_model.add(
            Bidirectional(LSTM(10, return_sequences=True),
                          input_shape=(5, 10)))
        network_model.add(Bidirectional(LSTM(10)))
        network_model.add(Dense(5))
        network_model.add(Activation('softmax'))
        network_model.compile(loss='categorical_crossentropy',
                              optimizer='rmsprop')

        data = read_data(os.path.join(file_path, file), normalize='std')

        # encoded = AutoEncoder(load_model("encoding/models/proteins-autoencoder_6EQE_Angles_And_RSA.h5")).encode(data)
        # df = pd.DataFrame(encoded)
        # df.to_csv("encoding/models/6EQE_Angles_And_RSA_result.csv", index=False, header=False)

        network_model.plot(data,
                           title=name,
                           file=os.path.join(ENC_PLOT_DATA_PATH, name + "_" +
                                             str(epochs) + ".jpg"))
Example #7
0
        def firstbuild():
            model = Sequential()
            model.add(
                Embedding(maxvocab + 1, embedding_size,
                          input_length=maxseqlen))
            model.add(
                LSTM(128,
                     dropout=0.4,
                     recurrent_dropout=0.4,
                     return_sequences=True))
            model.add(
                LSTM(64,
                     dropout=0.4,
                     recurrent_dropout=0.4,
                     return_sequences=True))
            model.add(
                LSTM(64,
                     dropout=0.4,
                     recurrent_dropout=0.4,
                     return_sequences=True))
            model.add(LSTM(32, dropout=0.4, recurrent_dropout=0.4))
            model.add(Dense(1, activation='sigmoid'))

            print(model.summary())
            return model
Example #8
0
'''
# for now we set max word length to 500
max_words = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_words)
X_test = sequence.pad_sequences(X_test, maxlen=max_words)
# Now run max_length and see both training and testing data have a max length of 500

n_classes = 2
print("Shape before one-hot encoding: ", y_train.shape)
y_train = np_utils.to_categorical(y_train, n_classes)
y_test = np_utils.to_categorical(y_test, n_classes)
print("Shape after one-hot encoding: ", y_train.shape)

# 100 and 500 are more common embedding_size
embedding_size=32
model=Sequential()
model.add(Embedding(vocabulary_size, embedding_size, input_length=max_words))
# the output of embedding layer is 500*32
model.add(CuDNNLSTM(100))
model.add(Dense(2, activation='softmax'))

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

model.fit(X_train , y_train , epochs=5  , validation_split=0.2)

test_loss , test_accuracy = model.evaluate(X_test , y_test)
print(test_loss , (test_accuracy)*100)

# saving the model
Example #9
0
def createWord2VecModel(texts, targets, epochs_model):
    # WORD2VEC
    W2V_SIZE = 300
    W2V_WINDOW = 4
    W2V_EPOCH = 40
    W2V_MIN_COUNT = 8

    # KERAS
    SEQUENCE_LENGTH = 32
    EPOCHS = epochs_model
    BATCH_SIZE = 1024

    w2v_model = gensim.models.word2vec.Word2Vec(size=W2V_SIZE,
                                                window=W2V_WINDOW,
                                                min_count=W2V_MIN_COUNT,
                                                workers=8)
    w2v_model.build_vocab(texts)
    words = w2v_model.wv.vocab.keys()
    vocab_size = len(words)
    print("Vocab size", vocab_size)

    train_x, test_x, train_y, test_y = train_test_split(texts,
                                                        targets,
                                                        test_size=0.20,
                                                        random_state=63)
    # train_x, valid_x, train_y, valid_y = train_test_split(train_x, train_y, test_size=0.20, random_state=63)

    w2v_model.train(texts, total_examples=len(texts), epochs=W2V_EPOCH)

    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(train_x)
    vocab_size = len(tokenizer.word_index) + 1

    train_x_seq = pad_sequences(tokenizer.texts_to_sequences(train_x),
                                maxlen=SEQUENCE_LENGTH)
    test_x_seq = pad_sequences(tokenizer.texts_to_sequences(test_x),
                               maxlen=SEQUENCE_LENGTH)

    embedding_matrix = np.zeros((vocab_size, W2V_SIZE))
    # %%
    for word, i in tokenizer.word_index.items():
        if word in w2v_model.wv:
            embedding_matrix[i] = w2v_model.wv[word]

    embedding_layer = Embedding(vocab_size,
                                W2V_SIZE,
                                weights=[embedding_matrix],
                                input_length=SEQUENCE_LENGTH,
                                trainable=False)

    model = Sequential()
    model.add(embedding_layer)
    model.add(Dropout(0.25))
    model.add(
        LSTM(128, dropout=0.1, recurrent_dropout=0.1, return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(64, dropout=0.1, recurrent_dropout=0.1))
    model.add(Dense(1, activation='sigmoid'))
    #ACCURACY: 0.8107593655586243
    model.summary()

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

    callbacks = [
        ReduceLROnPlateau(monitor='val_loss', patience=5, cooldown=0),
        EarlyStopping(monitor='val_accuracy', min_delta=1e-2, patience=5)
    ]

    history = model.fit(train_x_seq,
                        train_y,
                        batch_size=BATCH_SIZE,
                        epochs=EPOCHS,
                        validation_split=0.1,
                        verbose=1,
                        callbacks=callbacks)

    score = model.evaluate(test_x_seq, test_y, batch_size=BATCH_SIZE)
    print()
    print("ACCURACY:", score[1])
    print("LOSS:", score[0])

    return model
def get_network(network="nvidia", input_shape=PROJECT_SHAPE, dropout_rate=0.):
    """ Get the desired network.

    Args:
        network: up to now only the "nvidia" network is implemented
        input_shape: (height, width, channels) shape tuple of the input image
        dropout_rate: dropout probability for all implemented layers
    """

    if network.lower() == "nvidia":
        nvidia_model = Sequential()
        nvidia_model.add(
            Lambda(lambda x: x / 255. - 0.5, input_shape=input_shape))
        nvidia_model.add(
            Conv2D(filters=24,
                   kernel_size=5,
                   strides=2,
                   padding='valid',
                   activation='relu'))
        # nvidia_model.add(BatchNormalization())
        nvidia_model.add(
            Conv2D(filters=36,
                   kernel_size=5,
                   strides=2,
                   padding='valid',
                   activation='relu'))
        # nvidia_model.add(BatchNormalization())
        nvidia_model.add(
            Conv2D(filters=48,
                   kernel_size=5,
                   strides=2,
                   padding='valid',
                   activation='relu'))
        # nvidia_model.add(BatchNormalization())
        nvidia_model.add(
            Conv2D(filters=64,
                   kernel_size=3,
                   strides=1,
                   padding='valid',
                   activation='relu'))
        # nvidia_model.add(BatchNormalization())
        nvidia_model.add(
            Conv2D(filters=64,
                   kernel_size=3,
                   strides=1,
                   padding='valid',
                   activation='relu'))
        # nvidia_model.add(BatchNormalization())
        nvidia_model.add(Flatten())
        nvidia_model.add(Dense(units=100, activation='relu'))
        nvidia_model.add(Dropout(rate=dropout_rate))
        nvidia_model.add(Dense(units=50, activation='relu'))
        nvidia_model.add(Dropout(rate=dropout_rate))
        nvidia_model.add(Dense(units=10, activation='relu'))
        nvidia_model.add(Dense(units=1))

        return nvidia_model
Example #11
0
class MyModel:
    def build_model(self,lr=0.001,decay = 0.01):
        self.model = Sequential();
        init = initializers.RandomNormal(mean=0, stddev=1, seed=None);
        self.model.add(Dense(1024, input_shape=(2622,), activation='relu',kernel_initializer= init));
        self.model.add(Dense(512, activation="relu"));
        self.model.add(Dense(256,activation="relu")); 
        self.model.add(Dense(128,activation="relu"));
        self.model.add(Dense(64,activation="relu"));
        self.model.add(Dense(32,activation="relu"));
        self.model.add(Dense(16,activation="relu"));
        self.model.add(Dense(1,activation="sigmoid"));
        # learning rate 0.001
        optimizer_adam = optimizers.Adam(lr=lr,decay = decay)
        self.model.compile(loss='binary_crossentropy', optimizer= optimizer_adam,  metrics = ["accuracy"])         
        return self.model
Example #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["class"]))

            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(9, 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["class"]))
            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")
Example #13
0
    def model(cls):
        model = Sequential()

        model.add(cls._init_conv2D())
        model.add(Flatten())
        model.add(cls._dense(config.NUM_PAINTERS))
        model.add(Activation("softmax"))

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

        return model
Example #14
0
class AutoEncoder:
    def __init__(self,
                 date_range,
                 symbol="AAPL",
                 data_file="calibration_data"):
        self.data = None
        for day in date_range:
            path = "fundamental_{}_{}.bz2".format(symbol,
                                                  day.strftime("%Y%m%d"))
            path = os.path.join(data_file, path)
            if os.path.exists(path):
                prices = pd.read_pickle(path, compression="bz2")
                if self.data is None:
                    self.data = prices.values.T
                else:
                    self.data = np.vstack([self.data, prices.values.T])
        scaler = MinMaxScaler()
        self.data_scaled = np.array(
            [scaler.fit_transform(d.reshape(-1, 1)) for d in self.data])
        self.data_scaled = self.data_scaled[:, :, 0]
        print("The data shape is", self.data_scaled.shape)

    def build_model(self, encode_length=16, activation="relu"):
        n_in = self.data_scaled.shape[1]
        self.encode_length = encode_length

        self.model = Sequential()
        self.model.add(Dense(128, activation=activation, name="encoder_l1"))
        self.model.add(Dense(64, activation=activation, name="encoder_l2"))
        self.model.add(
            Dense(encode_length, name="encoder_output", activation=None))
        self.model.add(Dense(64, activation=activation))
        self.model.add(Dense(128, activation=activation))
        self.model.add(Dense(n_in, activation=None))

        self.model.compile(optimizer='adam', loss='mse')
        self.model.build()

        return self.model

    def _reshape_data(self, data):
        if len(data.shape) == 3:
            return data
        if len(data.shape) == 2:
            return data[:, :, np.newaxis]
        if len(data.shape) == 1:
            return data[np.newaxis, :, np.newaxis]

    def train_model(self,
                    test_size=0.1,
                    val_size=0.1,
                    batch_size=16,
                    epochs=200,
                    stop_patience=10,
                    plot_test=True,
                    plot_history=True):
        x = self.data_scaled
        if test_size != 0.:
            x_train, x_test, y_train, y_test = train_test_split(
                x, x, test_size=test_size, random_state=42)
            print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
        else:
            x_train, y_train = x, x

        early_stopping = EarlyStopping(monitor='val_loss',
                                       patience=stop_patience,
                                       mode="min",
                                       verbose=2,
                                       restore_best_weights=True)
        result = self.model.fit(x_train,
                                y_train,
                                batch_size=batch_size,
                                epochs=epochs,
                                validation_split=val_size / (1 - test_size),
                                callbacks=[early_stopping])
        if plot_test:
            y_test_predict = self.model.predict(x_test)
            print(
                "test loss:",
                np.sum((y_test_predict - y_test)**2) /
                (y_test.shape[0] * y_test.shape[1]))
            plt.plot(y_test[0])
            plt.plot(y_test_predict[0])
            plt.ylabel("Scaled Price")
            plt.xlabel("Minutes")
            plt.title("Encode length {}".format(self.encode_length))
            plt.legend(["Real", "Predict"])
            plot_name = "sample"
            plt.savefig('{}_{}.png'.format(plot_name, self.encode_length))
            plt.show()
        if plot_history:
            self.loss_plot(result.history)

        return result

    def loss_plot(self, history, plot_name='Loss'):
        loss = np.asarray(history['loss'])
        val_loss = np.asarray(history['val_loss'])
        plt.style.use('seaborn')
        plt.figure(figsize=(12, 9), dpi=100)
        plt.grid(True)
        plt.plot(loss)
        plt.plot(val_loss)
        plt.legend(['loss', 'val_loss'])
        plt.title("Encode length {}".format(self.encode_length))
        plt.xlabel("Epochs")
        plt.ylabel("MSE")
        plt.savefig('{}_{}.png'.format(plot_name, self.encode_length))
        plt.show()

    def save_feature(self, plot_feature=False):
        feature_name = "AutoEncoderFeature_{}.npy".format(self.encode_length)
        encoder = Model(inputs=self.model.input,
                        outputs=self.model.get_layer('encoder_output').output)
        feature = encoder.predict(self.data_scaled)
        np.save("feature/" + feature_name, feature)

        if plot_feature:
            if self.encode_length == 8:
                fig, ax = plt.subplots(ncols=4, nrows=2, figsize=(12, 9))
                axes = ax.flatten()
                for i in range(feature.shape[1]):
                    sns.distplot(feature[:, i], ax=axes[i])
                plt.show()
                return

            for i in range(feature.shape[1]):
                sns.distplot(feature[:, i])
                plt.show()
            return

    def save_model(self):
        self.model.save("model/AutoEncoder_{}.h5".format(self.encode_length))

    def save_encoder_ws(self):
        w1, b1 = self.model.get_layer('encoder_l1').get_weights()
        w2, b2 = self.model.get_layer('encoder_l2').get_weights()
        w3, b3 = self.model.get_layer('encoder_output').get_weights()
        with open("model/AutoEncoder_w_{}.h5".format(self.encode_length),
                  "wb") as f:
            pickle.dump([w1, b1, w2, b2, w3, b3], f)

    def encode(self, x):
        encoder = Model(inputs=self.model.input,
                        outputs=self.model.get_layer('encoder_output').output)
        return encoder.predict(x)
Example #15
0
def create_model():
    model = Sequential()
    model.add(Conv2D(LAYER1_SIZE, activation="relu", kernel_size=(3, 3),
                     input_shape=(2, BOARD_SIZE, BOARD_SIZE),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(Conv2D(LAYER1_SIZE, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(MaxPooling2D((2, 2), data_format="channels_first"))
    model.add(Conv2D(LAYER1_SIZE * 2, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(Conv2D(LAYER1_SIZE * 2, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(MaxPooling2D((2, 2), data_format="channels_first"))
    model.add(Flatten())
    model.add(Dense(LAYER2_SIZE, activation='relu', kernel_regularizer=l2(L2_REGULARISATION)))
    model.add(Dense(1, activation='tanh'))

    optimizer = Adam(decay=DECAY, lr=LR)
    model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy', 'mae'])
    model.summary()

    return model
Example #16
0
# In[]
'''

voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
               'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
               'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',
               'Sheep', 'Sofa', 'Train', 'Tvmonitor']
NUM_CLASSES = len(voc_classes) + 1

'''
# In[]

from keras import Sequential
from keras.layers import Conv2D, ZeroPadding2D, MaxPooling2D

model = Sequential()

# Block 1
model.add(Conv2D(64, (3, 3),
                 name='conv1_1',
                 padding='same',
                 activation='relu',
                 input_shape=(300,300,3)))
first_layer = model.layers[-1]
# this is a placeholder tensor that will contain our generated images
input_img = first_layer.input

# build the rest of the network
model.add(Conv2D(64, (3, 3),
                 name='conv1_2',
                 padding='same',
from keras import applications, optimizers
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from keras import Model, Sequential

base_model = applications.VGG16(include_top=False, weights='imagenet', 
                           input_shape=(150, 150, 3))


top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(512, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.2))
top_model.add(Dense(2, activation='softmax'))

model = Model(inputs = base_model.input, outputs = top_model(base_model.output))
def build_binary_classifier():
    #10000/10000 [==============================] - 18s 2ms/step
    #[0.17787481148242951, 0.92800000000000005]

    nn = Sequential()
    conv1 = Conv2D(32, (3, 3),
                   activation='relu',
                   padding="same",
                   input_shape=(32, 32, 3))
    conv2 = Conv2D(32, (3, 3), activation='relu', padding="same")
    nn.add(conv1)
    nn.add(conv2)

    pool1 = MaxPooling2D(pool_size=(2, 2))
    nn.add(pool1)

    drop1 = Dropout(0.25)
    nn.add(drop1)

    conv3 = Conv2D(32, (3, 3), activation='relu', padding="same")
    conv4 = Conv2D(32, (3, 3), activation='relu', padding="same")
    nn.add(conv3)
    nn.add(conv4)

    pool2 = MaxPooling2D(pool_size=(2, 2))
    nn.add(pool2)

    drop2 = Dropout(0.25)
    nn.add(drop2)

    nn.add(Flatten())

    hidden1 = Dense(units=250, activation="relu")
    nn.add(hidden1)

    hidden2 = Dense(units=600, activation="relu")
    nn.add(hidden2)

    output = Dense(units=1, activation="sigmoid")
    nn.add(output)
    return nn
Example #19
0
import numpy as np
from keras import Sequential
from keras.layers import Dense

data = np.random.random((1000, 32))
label = np.random.random((1000, 10))

model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(32, )))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile('adam', 'categorical_crossentropy')

model.fit(data, label, epochs=100)

model.save('my_model.h5')

def build_convolution_nn():
    #10000/10000 [==============================] - 18s 2ms/step
    #[0.7925042769432068, 0.72289999999999999]

    nn = Sequential()
    conv1 = Conv2D(32, (3, 3),
                   activation='relu',
                   padding="same",
                   input_shape=(32, 32, 3))
    conv2 = Conv2D(32, (3, 3), activation='relu', padding="same")
    nn.add(conv1)
    nn.add(conv2)

    pool1 = MaxPooling2D(pool_size=(2, 2))
    nn.add(pool1)

    drop1 = Dropout(0.25)
    nn.add(drop1)

    conv3 = Conv2D(32, (3, 3), activation='relu', padding="same")
    conv4 = Conv2D(32, (3, 3), activation='relu', padding="same")
    nn.add(conv3)
    nn.add(conv4)

    pool2 = MaxPooling2D(pool_size=(2, 2))
    nn.add(pool2)

    drop2 = Dropout(0.25)
    nn.add(drop2)

    nn.add(Flatten())

    hidden1 = Dense(units=250, activation="relu")
    nn.add(hidden1)

    hidden2 = Dense(units=600, activation="relu")
    nn.add(hidden2)

    output = Dense(units=10, activation="softmax")
    nn.add(output)
    return nn
Example #21
0
#%%
#estamos pegando todas as colunas, menos a útlima como o X train
X_train = np.expand_dims(dataset.values[:, :-1], axis=2)
#a última coluna é o label (ou o dev set)
y_train = dataset.values[:, -1:]
#o test set serão as últimas colunas
X_test = np.expand_dims(dataset.values[:, 1:], axis=2)
print(X_train.shape, y_train.shape, X_test.shape)

#%%
import keras
from keras import Sequential
from keras.layers import Dense
from keras.layers import Dense, LSTM, RepeatVector, TimeDistributed, Flatten, Dropout
#%%
model = Sequential()
model.add(LSTM(units=64, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(units=1))

model.compile(optimizer="adam", loss="mse", metrics=["mean_squared_error"])
model.summary()
#%%
model.fit(X_train, y_train, batch_size=4096, nb_epoch=5)

#%%
from sklearn.metrics import confusion_matrix, accuracy_score
precisão = accuracy_score(y_true, y_pred)
#%%
# creating submission file
predição = model.predict(X_test)
# we will keep every value between 0 and 20
Example #22
0
def mlp(sample_dim):
    model = Sequential()
    model.add(Dense(512, kernel_initializer='glorot_uniform', activation='relu', input_dim=sample_dim))
    model.add(Dense(128, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dense(64, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dense(32, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dense(1))
    model.compile(loss=losses.mae, optimizer='adam')
    return model
Example #23
0
    #Build input array, where a sounding and its neighbours are stacked
    dbdt_train = build_array_skytem(3, dbdt_train)
    dbdt_test = build_array_skytem(3, dbdt_test)
else:
    from sklearn.model_selection import train_test_split
    #normalize by sign and log
    dbdt_norm = np.sign(dbdt) * np.log(np.abs(dbdt))
    sc = StandardScaler()
    dbdt_norm = sc.fit_transform(dbdt_norm)
    dbdt_norm = build_array_skytem(3, dbdt_norm)
    X = range(dbdt_norm.shape[1])
    X_train_idx, X_test_idx, lbl_train, lbl_test = train_test_split(X, lbl[1:-1], test_size=0.2)
    dbdt_train = dbdt_norm[:, X_train_idx]
    dbdt_test = dbdt_norm[:, X_test_idx]

model = Sequential()
model.add(Dense(30, input_shape=(dbdt_train.shape[0],), activation='tanh'))
model.add(Dense(1, activation='tanh'))
model.compile(loss='binary_crossentropy',
              optimizer='Adam',#Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),
              metrics=['accuracy'])
print(model.summary())
history = model.fit(dbdt_train.T, lbl_train, epochs=30, batch_size = 100,
                    verbose = 2, validation_split=0.1)

# network = join(
#     Input(51),
#     Tanh(30),
#     Tanh(1),
# )
#
     ObjData = [ObjData] * 6
     PoseData = [PoseData] * 6
     p = np.append(RGBData, ObjData, 1)
     p = np.append(p, BGData, 1)
     p = np.append(p, PoseData, 1)
     # p = RGBData
     if train_data is not None:
         train_data = np.append(train_data, p, 0)
     else:
         train_data = p
     gt = pickle_filename.split('_')[1]
     ## coarse classify
     train_label = np.append(train_label, [catagories.index(gt)] * 6)
 train_label = np_utils.to_categorical(train_label, 15)
 print(train_data.shape)
 model = Sequential()
 model.add(
     Dense(128,
           input_shape=(train_data.shape[1], ),
           activation='relu',
           kernel_regularizer=regularizers.l2(0.001)))
 model.add(Dropout(0.6))
 model.add(Dense(15, activation='softmax'))
 model.compile(loss='categorical_crossentropy',
               optimizer='RMSprop',
               metrics=['accuracy'])
 ## model fit
 model.fit(train_data,
           train_label,
           shuffle=True,
           batch_size=128,
Example #25
0
def getLModel(dims):
    if K.image_data_format() == 'channels_first':
        input_shape = (3, dims[0], dims[1])
    else:
        input_shape = (dims[0], dims[1], 3)

    model = Sequential()
    model.add(Conv2D(32, (3, 3), strides=(2, 2), input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(64, (3, 3), strides=(2, 2)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(.2))
    model.add(Dense(4, activation='softmax'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model
Example #26
0
def classifier_model():
    model = Sequential()
    model.add(LSTM(512, input_dim=4096, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.01), activation='relu'))
    model.add(Dropout(0.6))
    model.add(Dense(64, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.01), activation='sigmoid'))
    model.add(Dropout(0.6))
    model.add(Dense(32, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.01)))
    model.add(Dropout(0.6))
    model.add(Dense(1, kernel_initializer='glorot_normal', kernel_regularizer=l2(0.01), activation='sigmoid'))
    return model
Example #27
0
print(dimData)
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. to_categorical is doing the same thing as LabelEncoder()
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='relu'))
model.add(Dense(512, activation='tanh'))  # Adding hidden layer
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=10,
                    verbose=1,
                    validation_data=(test_data, test_labels_one_hot))
Example #28
0
from sklearn import metrics

print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

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

# # 3.1. Predicting results using Neural Networks

import keras

from keras import Sequential
from keras.layers import Dense

# In[35]:

classifier = Sequential()

# In[36]:

# First Hidden Layer
classifier.add(
    Dense(7,
          activation='relu',
          kernel_initializer='random_normal',
          input_dim=13))

# In[37]:

# Second Hidden Layer
classifier.add(Dense(7, activation='relu', kernel_initializer='random_normal'))
class YApplyTimeSeries(object):
    def __init__(self):
        # data prepare
        self.__df = None
        self.__train_feature_label, self.__test_feature_label = None, None
        self.__train_feature, self.__train_label = None, None
        self.__test_feature, self.__test_label = None, None
        self.__mms = None

        # function set
        self.__net = None

        # optimizer function

        # pick the best function

    def data_prepare(self):
        self.__df = pd.read_csv("C:\\Users\\Dell\\Desktop\\time_series.csv",
                                encoding="utf-16")
        self.__df = self.__df.dropna()
        self.__train_feature_label = self.__df.loc[(
            self.__df["is_oot"] == 0), :]
        self.__test_feature_label = self.__df.loc[(
            self.__df["is_oot"] == 1), :]
        self.__train_feature_label = self.__train_feature_label.drop(
            ["id_no", "is_oot"], axis=1)
        self.__test_feature_label = self.__test_feature_label.drop(
            ["id_no", "is_oot"], axis=1)

        self.__train_feature = self.__train_feature_label[[
            i for i in self.__train_feature_label.columns if i != "is_overdue"
        ]].values
        self.__train_label = self.__train_feature_label["is_overdue"].values
        self.__test_feature = self.__test_feature_label[[
            i for i in self.__test_feature_label.columns if i != "is_overdue"
        ]].values
        self.__test_label = self.__test_feature_label["is_overdue"].values
        # 标准化
        self.__mms = MinMaxScaler()
        self.__mms.fit(self.__train_feature)
        self.__train_feature = self.__mms.transform(self.__train_feature)
        self.__test_feature = self.__mms.transform(self.__test_feature)
        # reshape samples × input_length × input_dim
        self.__train_feature = self.__train_feature.reshape((-1, 5, 3))
        self.__test_feature = self.__test_feature.reshape((-1, 5, 3))

    def function_set(self):
        self.__net = Sequential()
        self.__net.add(GRU(units=5, input_length=5, input_dim=3))
        self.__net.add(Dense(units=1, activation="sigmoid"))

    def optimizer_function(self):
        self.__net.summary()
        self.__net.compile(loss=keras.losses.binary_crossentropy,
                           optimizer=keras.optimizers.Adam(),
                           metrics=["accuracy"])

    def pick_the_best_function(self):
        self.__net.fit(self.__train_feature,
                       self.__train_label,
                       epochs=2,
                       batch_size=256)
        print(
            roc_auc_score(self.__test_label,
                          self.__net.predict_proba(self.__test_feature)))
class DNN(Model):
    """
    This class is parent class for all Deep neural network models. Any class
    inheriting this class should implement `make_default_model` method which
    creates a model with a set of hyper parameters.
    """
    def __init__(self, input_shape, num_classes, **params):
        """
        Constructor to initialize the deep neural network model. Takes the input
        shape and number of classes and other parameters required for the
        abstract class `Model` as parameters.

        Args:
            input_shape (tuple): shape of the input
            num_classes (int): number of different classes ( labels ) in the data.
            **params: Additional parameters required by the underlying abstract
                class `Model`.

        """
        super(DNN, self).__init__(**params)
        self.input_shape = input_shape
        self.model = Sequential()
        self.make_default_model()
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(loss='categorical_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])
        # print(self.model.summary(), file=sys.stderr)
        self.save_path = self.save_path or self.name + '_best_model.h5'

    def load_model(self, to_load):
        """
        Load the model weights from the given path.

        Args:
            to_load (str): path to the saved model file in h5 format.

        """
        try:
            self.model.load_weights(to_load)
        except:
            sys.stderr.write("Invalid saved file provided")
            sys.exit(-1)

    def save_model(self):
        """
        Save the model weights to `save_path` provided while creating the model.
        """
        self.model.save_weights(self.save_path)

    def train(self, x_train, y_train, x_val=None, y_val=None, n_epochs=50):
        """
        Train the model on the given training data.


        Args:
            x_train (numpy.ndarray): samples of training data.
            y_train (numpy.ndarray): labels for training data.
            x_val (numpy.ndarray): Optional, samples in the validation data.
            y_val (numpy.ndarray): Optional, labels of the validation data.
            n_epochs (int): Number of epochs to be trained.

        """
        best_acc = 0
        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        for i in range(n_epochs):
            # Shuffle the data for each epoch in unison inspired
            # from https://stackoverflow.com/a/4602224
            p = np.random.permutation(len(x_train))
            x_train = x_train[p]
            y_train = y_train[p]
            self.model.fit(x_train, y_train, batch_size=32, epochs=1)
            loss, acc = self.model.evaluate(x_val, y_val)
            if acc > best_acc:
                best_acc = acc
        self.trained = True

    def predict_one(self, sample):
        if not self.trained:
            sys.stderr.write(
                "Model should be trained or loaded before doing predict\n")
            sys.exit(-1)
        return self.model.predict(np.array([sample]))[0]

    def make_default_model(self):
        """
        Make the model with default hyper parameters
        """
        # This has to be implemented by child classes. The reason is that the
        # hyper parameters depends on the model.
        raise NotImplementedError()
def build_model(spec, X_train):
    model = Sequential()
    # create first layer
    layer = spec[0]
    num_posts, bow_dim = X_train[0].shape
    model.add(InputLayer(input_shape=(num_posts, bow_dim)))
    model.add(Flatten())
    if 'none' in layer:
        model.add(Dense(  # input_dim=1,
            units=int(layer.split('none')[1]),
            activation=None
        ))
    elif 'relu' in layer:
        model.add(Dense(  # input_shape=train_X[0].shape,
            units=int(layer.split('relu')[1]),
            activation='relu'
        ))
    elif 'sig' in layer:
        model.add(Dense(  # input_shape=train_X[0].shape,
            units=int(layer.split('sig')[1]),
            activation='sigmoid'
        ))
    else:
        return None

    for layer in spec[1:]:
        if 'none' in layer:
            model.add(Dense(int(layer.split('none')[1]), activation=None))
        elif 'relu' in layer:
            model.add(Dense(int(layer.split('relu')[1]), activation='relu'))
        elif 'sig' in layer:
            model.add(Dense(int(layer.split('sig')[1]), activation='sigmoid'))
        elif 'drop' in layer:
            model.add(Dropout(float(layer.split('drop')[1]), seed=None))
        elif 'l1' in layer:
            model.add(ActivityRegularization(l1=float(layer.split('l1')[1])))
        elif 'l2' in layer:
            model.add(ActivityRegularization(l2=float(layer.split('l2')[1])))
        else:
            return None

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

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

    return model
Example #32
0
def gener_model01(maxlen, len_chars, word_size, num_lstm, model_file_name):
    '''
    :param maxlen: 输入长度
    :param len_chars:  字典长度
    :param word_size: 字向量长度
    :param num_lstm: lstm节点数
    :param model_file_name: 模型的保持文件
    :return:
    '''
    model = Sequential()
    model.add(Embedding(len_chars + 1, word_size, input_length=maxlen))
    '''len_chars+1是输入维度,word_size是输出维度,input_length是节点数'''
    model.add(LSTM(num_lstm, return_sequences=True))
    model.add(LSTM(num_lstm, return_sequences=True))
    # model.add(Dropout(0.3))
    # model.add(Dense(5,activation='softmax'))
    model.add(TimeDistributed(Dense(5, activation='softmax')))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    # tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, write_images=1, histogram_freq=1)
    # 设置log的存储位置,将网络权值以图片格式保持在tensorboard中显示,设置每一个周期计算一次网络的权值,每层输出值的分布直方图
    model.save(model_file_name)
    model.summary()
    return model
Example #33
0
def build_model(in_dim=20, drate=0.5, out=64):
    mdl = Sequential()
    mdl.add(Dense(out, input_dim=in_dim, activation='relu'))
    if drate:
        mdl.add(Dropout(drate))
    mdl.add(Dense(out, activation='relu'))
    if drate:
        mdl.add(Dropout(drate))
    mdl.add(Dense(1, activation='sigmoid'))

    return mdl
Example #34
0
def gener_model03(maxlen, len_chars, word_size, num_lstm, model_file_name):
    model = Sequential()
    model.add(Embedding(len_chars + 1, word_size, input_length=maxlen))
    '''len_chars+1是输入维度,word_size是输出维度,input_length是节点数'''
    model.add(
        Bidirectional(LSTM(num_lstm, return_sequences=True), merge_mode='sum'))
    # model.add(Dropout(0.3))
    model.add(
        Bidirectional(LSTM(num_lstm, return_sequences=True), merge_mode='sum'))
    model.add(
        Bidirectional(LSTM(num_lstm, return_sequences=True), merge_mode='sum'))
    model.add(TimeDistributed(Dense(5, activation='softmax')))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.save(model_file_name)
    model.summary()
    return model
Example #35
0
def create_lstm_model(num_features):
    model = Sequential()
    # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
    # Note: In a situation where your input sequences have a variable length,
    # use input_shape=(None, num_feature).
    # By setting return_sequences to True, return not only the last output but
    # all the outputs so far in the form of (num_samples, timesteps,
    # output_dim). This is necessary as TimeDistributed in the below expects
    # the first dimension to be the timesteps.
    model.add(RNN(HIDDEN_SIZE, input_shape=(None, num_features), return_sequences=True))

    # Apply a dense layer to the every temporal slice of an input. For each of step
    # of the output sequence, decide which character should be chosen.
    model.add(layers.TimeDistributed(layers.Dense(len(LABEL_CLASS_MAPPING) + 1)))
    model.add(layers.Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.summary()
    return model
Example #36
0
 def model(cls, input_shape: (int, int)):
     model = Sequential()
     model.add(
         layers.Conv2D(filters=6,
                       kernel_size=(3, 3),
                       activation='relu',
                       input_shape=(input_shape[0], input_shape[1], 1)))
     model.add(layers.AveragePooling2D())
     model.add(
         layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
     model.add(layers.AveragePooling2D())
     model.add(layers.Flatten())
     model.add(layers.Dense(units=120, activation='relu'))
     model.add(layers.Dense(units=84, activation='relu'))
     model.add(layers.Dense(units=10, activation='softmax'))
     return model
import pandas as pd
from keras import Sequential
from keras.layers.core import Dense,Activation,Dropout
from sklearn.model_selection import train_test_split
# from matplotlib.pyplot import plt
train_data=pd.read_csv('D:\sufe\A\contest_basic_train.tsv',sep='\t')
train_data=train_data.drop(['REPORT_ID',"ID_CARD",'LOAN_DATE'],1)
train_data=train_data.dropna()
# print(train_data.info())
X=train_data.drop(['Y'],1).as_matrix()#7
y=train_data['Y'].as_matrix()#1
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

model=Sequential()
model.add(Dense(14,input_shape=(7,)))
model.add(Activation('relu'))
model.add(Dense(1))
model.add((Dropout(0.3)))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(X_train,y_train,epochs=10000,batch_size=16)
t=model.predict(X_test)

rate=0

for i in range(len(t)):
    if t[i]==y_test[i]:
        rate+=1
    else:
        pass
Example #38
0
    test_labels = labels[train_size:, :]
    test_labels = np.reshape(test_labels, (test_labels.shape[0], 1))

    train_data = train_data.reshape(train_data.shape[0], train_data.shape[1],
                                    1)
    test_data = test_data.reshape(test_data.shape[0], test_data.shape[1], 1)

    num_filters = 2
    kernel_size = (5)
    conv_strides = 2
    pool_kernel = (2)
    pool_strides = 2
    dense_units = 1
    learning_rate = 0.01
    # ONE CONVOLUTION, ONE POOLING, 2 FC LAYERS
    model = Sequential()

    model.add(
        Conv1D(num_filters,
               kernel_size,
               input_shape=(1000, 1),
               strides=conv_strides,
               padding='valid',
               activation='relu'))
    model.add(
        AveragePooling1D(pool_size=pool_kernel,
                         strides=pool_strides,
                         padding='same'))
    model.add(Flatten())
    model.add(Dense(1, activation='softmax'))
    model.compile(optimizer='adam',
Example #39
0
class NeuralNetwork(object):
    def __init__(self, input_nodes, hidden_nodes, output_nodes, lr=None):
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes
        self.lr = lr
        self.scales_x = []
        self.scales_y = []

        input_kernel_range = np.sqrt(6) / (np.sqrt(input_nodes) + np.sqrt(hidden_nodes))
        input_kernel_initializer = RandomUniform(minval=-input_kernel_range, maxval=input_kernel_range)
        input_layer = Dense(input_nodes,
                            kernel_initializer=input_kernel_initializer,
                            name='input')

        hidden_kernel_range = np.sqrt(6) / (np.sqrt(hidden_nodes) + np.sqrt(output_nodes))
        hidden_kernel_initializer = RandomUniform(minval=-hidden_kernel_range, maxval=hidden_kernel_range)
        hidden_layer = Dense(hidden_nodes,
                             kernel_initializer=hidden_kernel_initializer,
                             name='hidden')

        output_layer = Dense(output_nodes,
                             name='output')

        self.model = Sequential()
        self.model.add(input_layer)
        self.model.add(hidden_layer)
        self.model.add(output_layer)

    def train(self, x_train, y_train):
        self.set_normalize_scales(x_train, y_train)
        x_train = self.normalize(x_train, self.scales_x)
        y_train = self.normalize(y_train, self.scales_y)

        optimizer = SGD(lr=self.lr)
        self.model.compile(loss='mse', optimizer=optimizer)
        self.model.fit(x_train, y_train, batch_size=20, epochs=500)

    def evaluate(self, x_test, y_test):
        x_test = self.normalize(x_test, self.scales_x)
        y_test = self.normalize(y_test, self.scales_y)
        return self.model.evaluate(x_test, y_test)

    def predict(self, x):
        x = self.normalize(x, self.scales_x)
        y = self.model.predict(x)
        return self.unnormalize(y, self.scales_y)

    def set_normalize_scales(self, x, y):
        for i in range(x.shape[1]):
            mean, std = x[:, i].mean(), x[:, i].std()
            self.scales_x.append([mean, std])
        for i in range(y.shape[1]):
            mean, std = y[:, i].mean(), y[:, i].std()
            self.scales_y.append([mean, std])

    @staticmethod
    def normalize(data, scales):
        for i in range(0, len(scales)):
            mean, std = scales[i]
            data[:, i] = (data[:, i] - mean) / std
        return data

    @staticmethod
    def unnormalize(data, scales):
        for i in range(0, len(scales)):
            mean, std = scales[i]
            data[:, i] = data[:, i] * std + mean
        return data
Cells = Cells[s]
labels = labels[s]

X_train = Cells[(int)(0.2 * len(labels)):]
X_val = Cells[:(int)(0.2 * len(labels))]
X_train = X_train.astype('float32') / 255
X_val = X_val.astype('float32') / 255  # Normalization
y_train = labels[(int)(0.2 * len(labels)):]
y_val = labels[:(int)(0.2 * len(labels))]
# # # Up until here it's the same, regardless of the type of network to be used.

#Rec-NN
from keras import Sequential
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM  # long short term memory

model = Sequential()

model.add(
    LSTM(32,
         input_shape=X_train.shape[1:],
         activation='relu',
         return_sequences=True))
model.add(Dropout(0.25))

model.add(LSTM(64, activation='relu'))
model.add(Dropout(0.25))

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

model.add(Dense(43, activation='softmax'))
print(y_train[6])

print('Maximum review length: {}'.format(len(max((X_train + X_test), key=len))))

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


from keras.preprocessing import sequence
max_words = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_words)
X_test = sequence.pad_sequences(X_test, maxlen=max_words)

from keras import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout
embedding_size=32
model=Sequential()
model.add(Embedding(vocabulary_size, embedding_size, input_length=max_words))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
print(model.summary())


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

batch_size = 64
num_epochs = 3
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)
Example #42
0
def train_model(train_test_path):
    """
    Creates a model and performs training.
    """
    # Load train/test data
    train_test_data = np.load(train_test_path)
    x_train = train_test_data['X_train']
    y_train = train_test_data['y_train']

    print("x_train:", x_train.shape)
    print("y_train:", y_train.shape)

    del train_test_data

    x_train = np.expand_dims(x_train, axis=3)

    # Create network
    model = Sequential()
    model.add(Conv1D(128, 5, input_shape=x_train.shape[1:], padding='same', activation='relu'))
    model.add(MaxPooling1D(5))
    model.add(Conv1D(128, 5, padding='same', activation='relu'))
    model.add(MaxPooling1D(5))
    model.add(Dropout(0.5))

    model.add(Flatten())

    model.add(Dense(1024, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(512, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(256, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(128, kernel_initializer='glorot_uniform', activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(len(language_codes), kernel_initializer='glorot_uniform', activation='softmax'))

    model_optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    model.compile(loss='categorical_crossentropy', optimizer=model_optimizer, metrics=['accuracy'])

    # Train
    model.fit(x_train, y_train,
              epochs=10,
              validation_split=0.10,
              batch_size=64,
              verbose=2,
              shuffle=True)

    model.save(model_path)