Ejemplo n.º 1
0
def train_model(train_data, train_labels, test_data, test_labels, epochs):
    input_shape = (28, 28, 1)

    my_model = Sequential()
    my_model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               activation="relu",
               input_shape=input_shape))
    my_model.add(Conv2D(filters=64, kernel_size=(3, 3), activation="relu"))
    my_model.add(MaxPooling2D())
    my_model.add(Dropout(0.25))
    my_model.add(Flatten())
    my_model.add(Dropout(0.5))
    my_model.add(Dense(128, activation='relu'))
    my_model.add(Dense(10, activation='softmax'))

    my_model.compile(loss=keras.losses.categorical_crossentropy,
                     optimizer=keras.optimizers.Adadelta(),
                     metrics=['accuracy'])

    my_model.fit(train_data.reshape((60000, 28, 28, 1)),
                 train_labels,
                 batch_size=128,
                 epochs=epochs,
                 verbose=1,
                 validation_data=(test_data.reshape(
                     (10000, 28, 28, 1)), test_labels))
    return my_model
def learn_model(x_train, y_train, x_test, y_test, take_components, save_path=None, do_pca=False):
    # pca select main features
    if do_pca:
        pca = PCA(n_components=take_components)
        print("Compute pca relevant features with " + str(take_components) + " percent of variance")
        previous_dims = len(x_train[0])
        x_train = pca.fit_transform(x_train)
        x_test = pca.transform(x_test)
        print(str(len(x_train[0])) + " dims are used from initially " + str(previous_dims))

    # expand dims
    x_train = np.expand_dims(x_train, axis=2)
    x_test = np.expand_dims(x_test, axis=2)

    # change label to categorical
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)

    # build model
    model = Sequential()
    model.add(Conv1D(256, 8, padding='same', input_shape=(x_train.shape[1], 1)))
    model.add(Activation('relu'))
    model.add(Conv1D(256, 8, padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.6))
    model.add(Conv1D(128, 8, padding='same'))
    model.add(Activation('relu'))
    model.add(Conv1D(128, 8, padding='same'))
    model.add(Activation('relu'))
    model.add(Conv1D(128, 8, padding='same'))
    model.add(Activation('relu'))
    model.add(Conv1D(128, 8, padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.6))
    model.add(Conv1D(64, 8, padding='same'))
    model.add(Activation('relu'))
    model.add(Conv1D(64, 8, padding='same'))
    model.add(Activation('relu'))
    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.6))
    model.add(Dense(2))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0),
                  metrics=['acc'])

    # fit network
    model.fit(x_train, y_train, batch_size=16, epochs=33)

    # evaluate model
    _, accuracy = model.evaluate(x_test, y_test)

    # save model
    if save_path is not None:
        model.save(save_path)

    return accuracy
Ejemplo n.º 3
0
def prueba_2():
	cantidad_twits=10
	# define class twits
	test = load_test()
	twits = preprocesing(test[:cantidad_twits, 0])
	print(f"\ntwiters:\n{twits}")
	# define class labels
	labels = test[:cantidad_twits, 1].astype('float32')
	print(f"\nlabels:\n{labels}")
	# prepare tokenizer
	t = Tokenizer()
	t.fit_on_texts(twits)
	vocab_size = len(t.word_index) + 1
	# integer encode the documents
	encoded_twits = t.texts_to_sequences(twits)
	print(f"\nencoded_twits:\n{encoded_twits}")
	# pad documents to a max length of 4 words
	# Calculo largo maximo
	mylen = np.vectorize(len)
	lens=mylen(encoded_twits)
	max_len=max(lens)
	#TODO: Contar el twtit mas largo
	max_length = max_len
	padded_twits = pad_sequences(encoded_twits, maxlen=max_length, padding='post')
	print(f"\npadded_twits:\n{padded_twits}")

	# load the whole embedding into memory
	embeddings_index = dict()
	f = open('fasttext.es.300.txt')
	for line in f:
		values = line.split()
		word = values[0]
		coefs = np.asarray(values[1:], dtype='float32')
		embeddings_index[word] = coefs
	f.close()
	print('Loaded %s word vectors.' % len(embeddings_index))

	# create a weight matrix for words in training docs
	embedding_matrix = np.zeros((vocab_size, 300))
	for word, i in t.word_index.items():
		embedding_vector = embeddings_index.get(word)
		if embedding_vector is not None:
			embedding_matrix[i] = embedding_vector

	# define model
	model = Sequential()
	e = Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_length, trainable=False)
	model.add(e)
	model.add(Flatten())
	model.add(Dense(1, activation='sigmoid'))
	# compile the model
	model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
	# summarize the model
	print(model.summary())
	# fit the model
	model.fit(padded_twits, labels, epochs=50, verbose=0)
	# evaluate the model
	loss, accuracy = model.evaluate(padded_twits, labels, verbose=0)
	print('Accuracy: %f' % (accuracy * 100))
Ejemplo n.º 4
0
def LSTM_model(n_steps, n_features, X, y):
  model = Sequential()
  model.add(LSTM(30, activation='sigmoid', input_shape=(n_steps, n_features)))
  model.add(Dropout(0.3))
  model.add(Dense(30))
  model.compile(optimizer='adam', loss='mse')
  model.fit(X, y, epochs=50, verbose=0)
  return model
def train_model_2BLSTM_variableSequenceLength(path, subjectID, modelType, MLtechnique, features, labels, dw, batch_size, patience, LSTMunits=30):
	"""
	FUNCTION NAME: train_model_2BLSTM_variableSequenceLength

	This function trains a bidirectional LSTM model with 2 hidden layers
	when input sequences have difference length from one sample to the other. 
	In the first step, input sequences are arranged into a tensor of the same 
	length using zero padding. When data is ready, the bidirectional LSTM is trained.

	INPUT:
	------
		-> path:			full path where to store the trained model
		-> subjectID:		integer indicating the ID of the subject being analyzed
		-> modelType:		type of model to train
		-> MLtechnique:		technique to use to train the model
		-> features:		matrix of features to train the model
		-> labels:			matrix of labels to train the model
		-> dw:				factor used when downsampling the available data
		-> batch_size:		value for batch_size parameter
		-> patience:		value for patience parameter
		-> LSTMunits:		number of units of the LSTM
		
	OUTPUT:
	------- 

	"""

	epochs = 200
	verbose = 1

	if (dw == 1):
		modelName = path + 'Model_Subject' + str(subjectID) + '_' + MLtechnique + '_LSTMunits' + str(LSTMunits) + '_BatchSize' + str(batch_size) + '_Patience' + str(patience) + '_' + modelType
	else:
		modelName = path + 'Model_Subject' + str(subjectID) + '_DW' + str(dw) + '_' + MLtechnique + '_LSTMunits' + str(LSTMunits) + '_BatchSize' + str(batch_size) + '_Patience' + str(patience) + '_' + modelType

	# Convert data matrices to tensors
	T_features, T_labels = DE.dataMatrices2tensors(features, labels, modelType)

	# Define the Bidirectional LSTM
	model = Sequential([
				Masking(mask_value = 0., input_shape=(None,DE.get_3DtensorDimensions(T_features)[2])),
				Bidirectional(LSTM(LSTMunits, activation='tanh', return_sequences=True)),
				Bidirectional(LSTM(int(LSTMunits/2), activation='tanh', return_sequences=True)),
				TimeDistributed(Dense(1, activation='linear'))
				])

	model.compile(optimizer=Adam(),loss=loss_CCC)

	earlyStop = EarlyStopping(monitor='loss', patience=patience)
	callbacks_list = [earlyStop]

	# Train the model
	model.fit(T_features, T_labels, batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks = callbacks_list, validation_split = 0)

	print '-> Saving model ..'
	# Save model
	model.save(modelName + '.h5')
	print '<- Model saved'	
Ejemplo n.º 6
0
class DNN(BaseTrainer):
    @Decorator.log(True)
    def load_data(self, **dict):
        print(dict)
        self.traindata = pd.read_csv(dict['train'], header=None)
        self.testdata = pd.read_csv(dict['test'], header=None)
        X = self.traindata.iloc[:, 1:42]
        Y = self.traindata.iloc[:, 0]
        C = self.testdata.iloc[:, 0]
        T = self.testdata.iloc[:, 1:42]
        trainX = np.array(X)
        testT = np.array(T)
        trainX.astype(float)
        testT.astype(float)
        scaler = Normalizer().fit(trainX)
        trainX = scaler.transform(trainX)
        scaler = Normalizer().fit(testT)
        testT = scaler.transform(testT)

        self.y_train = np.array(Y)
        self.y_test = np.array(C)

        self.X_train = np.array(trainX)
        self.X_test = np.array(testT)

    def train(self):
        batch_size = 64
        nb_epoch = 100
        if self.has_train:
            nb_epoch = nb_epoch - self.epoch
            print('new epoch', nb_epoch)
            self.model.fit(self.X_train,
                           self.y_train,
                           batch_size=batch_size,
                           epochs=nb_epoch,
                           callbacks=[self.checkpointer, self.csv_logger])
        else:
            # 1. define the network
            self.model = Sequential()
            self.model.add(Dense(1024, input_dim=41, activation='relu'))
            self.model.add(Dropout(0.01))
            self.model.add(Dense(1))
            self.model.add(Activation('sigmoid'))
            self.model.compile(loss='binary_crossentropy',
                               optimizer='adam',
                               metrics=['accuracy'])

            self.model.fit(self.X_train,
                           self.y_train,
                           batch_size=batch_size,
                           epochs=nb_epoch,
                           callbacks=[self.checkpointer, self.csv_logger])
            self.model.save("./dnn1layer_model.hdf5")
        score, acc = self.model.evaluate(self.X_test, self.y_test)
        print('Test score:', score)
        print('Test accuracy', acc)
class PointwiseNN(Ranker):
    def __init__(self):
        self.model = Sequential()
        self.model.add(Dense(128))
        self.model.add(Dense(1))
        self.model.compile(optimizer='adam', loss='mse')
        super().__init__()

    def fit(self, X_train, y_train):
        self.model.fit(x=X_train, y=y_train)

    def predict(self, X) -> ndarray:
        return self.model.predict(x=X)
Ejemplo n.º 8
0
def sequential():
    model = Sequential()

    model.add(Dense(28, input_dim=784, activation=relu))
    model.add(Dense(28, activation=relu))
    model.add(Dense(10, activation=tf.nn.softmax))
    model.compile(optimizer=Adam(0.001),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    model.fit(X, y, epochs=10, batch_size=10)
    y_pred = model.predict(test_X)
    print(y_pred)
Ejemplo n.º 9
0
def model(x_test, x_train, y_test, y_train):
    model = Sequential()
    
    model_choice = {{choice(['one', 'two'])}}
    if model_choice == 'one':
        model.add(Conv2D(64, kernel_size=3, activation='relu',padding='same', input_shape=(img_rows, img_cols, color_type)))
        model.add(Conv2D(128, kernel_size=3, activation='relu',padding='same'))
        model.add(MaxPooling2D(pool_size=2,strides=2))
        model.add(Dropout({{uniform(0, 1)}}))
        model.add(Conv2D(256, kernel_size=3, activation='relu'))
        model.add(Conv2D(512, kernel_size=3, activation='relu'))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=2,strides=2))
        model.add(Dropout({{uniform(0, 1)}}))
    elif model_choice == 'two':
        model.add(Conv2D(64, kernel_size=3, activation='relu',padding='same', input_shape=(img_rows, img_cols, color_type)))
        model.add(Conv2D(128, kernel_size=3, activation='relu',padding='same'))
        model.add(MaxPooling2D(pool_size=2,strides=2))
        model.add(Dropout({{uniform(0, 1)}}))
        model.add(Conv2D(256, kernel_size=3, activation='relu'))
        model.add(Conv2D(512, kernel_size=3, activation='relu'))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=2,strides=2))
        model.add(Dropout({{uniform(0, 1)}}))
    
    model.add(Flatten())
    model.add(Dense({{choice([256, 512,1024])}}, activation='relu'))
    model.add(BatchNormalization())
    model.add(Dropout({{uniform(0, 1)}}))
    choiceval = {{choice(['one', 'two'])}}
    if choiceval == 'two':
        model.add(Dense({{choice([256, 512,1024])}}, activation='relu'))
        model.add(BatchNormalization())
        model.add(Dropout({{uniform(0, 1)}}))
    
    model.add(Dense(10, activation='softmax'))
    
    adam = keras.optimizers.Adam(lr=0.001)
    
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
                  optimizer=adam)
    model.fit(x_train, y_train,
              batch_size=256,
              nb_epoch=15,
              verbose=2,
              validation_data=(x_test, y_test))
    score, acc = model.evaluate(x_test, y_test, verbose=0)
    print('Val accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}
Ejemplo n.º 10
0
def mlp_model(x_train, y_train, x_val, y_val, params):
    model = Sequential()
    model.add(
        Dense(params['layer_size'],
              activation=params['activation'],
              input_dim=x_train.shape[1],
              kernel_regularizer=l2(params['regularization'])))
    model.add(Dropout(params['dropout']))
    for i in range(params['layers'] - 1):
        model.add(
            Dense(params['layer_size'],
                  activation=params['activation'],
                  kernel_regularizer=l2(params['regularization'])))
        model.add(Dropout(params['dropout']))
    model.add(Dense(2, activation='softmax'))
    model.compile(
        optimizer=params['optimizer'](params['lr']),
        loss=params['loss_functions'],
        # loss=params['loss_functions']([params['weights1'], params['weights2']]),
        metrics=['accuracy', Recall(), Precision(), f1])
    history = model.fit(x_train,
                        y_train,
                        batch_size=params['batch_size'],
                        validation_data=(x_val, y_val),
                        epochs=100,
                        callbacks=[
                            EarlyStopping(monitor='val_acc',
                                          patience=5,
                                          min_delta=0.01)
                        ],
                        verbose=0)
    return history, model
Ejemplo n.º 11
0
class BiLSTM(NNBaseModel):
    def train(self):
        batch_size = 64
        units = 100
        embedding_matrix = np.zeros((self.vocab_size, 100))
        for word, index in self.tk.word_index.items():
            embedding_vector = self.word2vec.get(word)
            if embedding_vector is not None:
                embedding_matrix[index] = embedding_vector

        self.model = Sequential()
        self.model.add(
            Embedding(self.vocab_size,
                      units,
                      weights=[embedding_matrix],
                      trainable=False))
        self.model.add(
            Bidirectional(LSTM(units, return_sequences=True, dropout=0.2)))
        self.model.add(Bidirectional(LSTM(units, dropout=0.2)))
        self.model.add(Dense(self.output_size, activation='sigmoid'))
        print(self.model.summary())
        self.model.compile(optimizer='adam',
                           loss='sparse_categorical_crossentropy',
                           metrics=['acc'])
        history = self.model.fit(self.X_train,
                                 self.y_train,
                                 epochs=100,
                                 batch_size=batch_size,
                                 verbose=1)
        def train_model(lowdata, labels, results, p):

            # Print parameters
            print('\n' + ' '.join(map(str, p)))

            # Split data
            #X_train, X_test, y_train, y_test = train_test_split(lowdata, labels,  test_size=0.2, random_state=29, shuffle=True)

            # Cross validation
            n_split = 5
            scores = []
            for train_index, test_index in KFold(n_split).split(lowdata):

                X_train, X_test = lowdata[train_index], lowdata[test_index]
                y_train, y_test = labels[train_index], labels[test_index]

                # Define model (complexity is a function of input dimensionality)
                if p[5] > 8:
                    k = 32
                elif p[5] >= 3:
                    k = 8
                else:
                    k = 3

                model = Sequential()
                model.add(
                    Dense(2 * k, activation='relu',
                          input_dim=X_train.shape[1]))
                model.add(Dropout(0.5))
                model.add(Dense(k, activation='relu'))
                model.add(Dense(n_classes, activation='softmax'))

                loss = categorical_crossentropy
                #optimizer = Adadelta(lr=0.0005)
                optimizer = Adam(lr=0.0005)
                model.compile(loss=loss,
                              optimizer=optimizer,
                              metrics=['categorical_accuracy'])
                #print(model.summary())

                # Train model
                n_epochs = 30
                history = model.fit(X_train,
                                    y_train,
                                    batch_size=32,
                                    epochs=n_epochs,
                                    verbose=0,
                                    validation_data=(X_test, y_test))

                #plot(history)
                scores.append(eval_metrics(model, X_test, y_test, class_names))

            # Evaluate model
            results = {
                'params': p,
                'history': history.history,
                'score': scores
            }
            return results
def train_model(X, Y, X_test, Y_test, nr_steps):
    n_batch = 14000
    n_epoch = 1000
    n_neurons = 30

    # design network
    model = Sequential()
    model.add(
        LSTM(units=n_neurons,
             return_sequences=True,
             batch_input_shape=(n_batch, X.shape[1], X.shape[2]),
             stateful=True))

    model.add(Dropout(0.2))

    # Adding a second LSTM layer and Dropout layer
    model.add(LSTM(units=n_neurons, return_sequences=True))
    model.add(Dropout(0.2))

    model.add(Dense(1))
    optimizer = optimizers.Adam(clipvalue=0.5)
    model.compile(loss='mean_squared_error',
                  optimizer=optimizer,
                  metrics=['acc'])

    for z in range(0, len(X) // 4, n_batch):
        xChunk = X[z:z + n_batch]
        yChunk = Y[z:z + n_batch]

        model.fit(xChunk,
                  yChunk,
                  epochs=n_epoch,
                  batch_size=n_batch,
                  verbose=1,
                  shuffle=False)
        model.reset_states()

    yhat = model.predict(X_test[0:n_batch, :, :],
                         batch_size=n_batch,
                         steps=nr_steps)
    for i in range(len(Y_test[0:n_batch])):
        print("Extected : " + str(Y[i]) + " but actually: " + str(yhat[i][0]))
    error = 1
    for j in range(len(Y_test[0:n_batch])):
        error = error * (abs(Y_test[j] - yhat[j][0]) / yhat[j][0]) * 100
    print("error:   " + str(error) + "%")
Ejemplo n.º 14
0
class Agent:
    def __init__(self):
        #Here we'll create the brain of our agent, a deep neural network with Keras
        self.model = Sequential()
        self.q_targets = []
        self.model.add(kl.Dense(10, input_dim=27))
        self.model.add(kl.Activation('relu'))
        self.model.add(kl.Dense(4, input_dim=10))
        self.model.add(kl.Activation('tanh'))
        self.model.compile(optimizer="adam", loss='MSE', metrics=['accuracy'])

    def pick_action(
            self, state,
            eps):  #Epsilon is the probability that wa take a random action
        #we chose if we take a random action
        if np.random.rand() < eps:
            return np.random.randint(4)
        #Else we take the best action acording to our Neural network(NN)
        else:
            return int(
                np.argmax(self.model.predict(np.reshape(state, (-1, 27)))))

    def train_model(self, states0, states1, actions, rewards):
        length = np.min((len(actions), 1000))
        self.q_targets = []
        prediction = self.model.predict(np.reshape(states0, (-1, 27)))

        for n in range(length):

            action_mask = np.array([1, 1, 1, 1])
            action_mask = np.logical_xor(action_mask, actions[n])
            pred = self.model.predict(np.reshape(states1[n], (-1, 27)))
            #print(pred.shape)
            q_target = prediction[n] * action_mask + actions[n] * (
                rewards[n])  # + 0.99*pred[0][np.argmax(pred)] )
            self.q_targets.append(q_target)
            print(states0[n], actions[n], rewards[n], q_target)

        self.model.fit(
            np.reshape(states0, (length, 27)),
            np.array(self.q_targets),
            batch_size=1,
        )
Ejemplo n.º 15
0
def build_model(train_dataset, train_labels, local_testing):
    """
    Function for building the LSTM model and setting hyperparameters
    :param train_dataset: numpy array with all necessary variables, does not contain canteen values
    :param train_labels: the canteen values that is the solution/real values.
    :param local_testing: bool value, will print training data if set to True.
    :return: The model that is built and the training history
    """
    model = Sequential()
    model.add(
        LSTM(5, input_shape=(train_dataset.shape[1], train_dataset.shape[2])))
    model.add(Dense(1))

    optimizer = optimizers.Adam(lr=0.01)
    model.compile(
        loss="mean_squared_error",
        optimizer=optimizer,
        metrics=["mean_absolute_error", "mean_squared_error"],
    )

    if local_testing:
        history = model.fit(
            train_dataset,
            train_labels,
            epochs=200,
            batch_size=20,
            validation_split=0.2,
            verbose=2,
            shuffle=False,
        )

    else:
        history = model.fit(
            train_dataset,
            train_labels,
            epochs=200,
            batch_size=20,
            validation_split=0.2,
            verbose=0,
            shuffle=False,
        )

    return model, history
Ejemplo n.º 16
0
def ApplyCnn(X32_train, X32_test, Y32_train):

    model_CNN = Sequential()
    model_CNN.add(
        Conv2D(64,
               kernel_size=3,
               activation='relu',
               input_shape=(X3_train.shape[1], X3_train.shape[2], 1)))
    model_CNN.add(MaxPooling2D(pool_size=(2, 2)))
    model_CNN.add(Conv2D(32, kernel_size=3, activation='relu'))
    model_CNN.add(MaxPooling2D(pool_size=(2, 2)))
    model_CNN.add(Conv2D(16, kernel_size=3, activation='relu'))
    model_CNN.add(MaxPooling2D(pool_size=(2, 2)))
    model_CNN.add(Flatten())
    model_CNN.add(Dense(10, activation='softmax'))
    model_CNN.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
    model_CNN.fit(X32_train, Y32_train, epochs=10, verbose=0)
    return predict(X32_test, model_CNN)
Ejemplo n.º 17
0
def fit_mlp(x_train,
            y_train,
            conf,
            seed=3,
            epochs=1500,
            batch_size=300,
            lr=0.05):
    """
    :param x_train: training data
    :param y_train: training target
    :param conf: model structure
    :param seed: seed
    :param epochs: number of epochs (using early stopping)
    :param batch_size: batch size
    :param lr: learning rate
    :return: model and history
    """
    early_stop = keras.callbacks.EarlyStopping(monitor='val_loss',
                                               mode='min',
                                               verbose=0,
                                               patience=3,
                                               restore_best_weights=True)
    model = Sequential()
    for n, a in conf:
        model.add(
            Dense(
                n,
                activation=a,
                kernel_initializer=keras.initializers.glorot_normal(seed=seed),
                bias_initializer='zeros'))

    opt = keras.optimizers.Adam(lr=lr)
    if conf[-1][1] == 'linear':
        model.compile(loss=keras.losses.mse, optimizer=opt)
    elif conf[-1][1] == 'sigmoid':
        model.compile(loss=keras.losses.binary_crossentropy,
                      optimizer=opt,
                      metrics=['accuracy'])
    else:
        model.compile(loss=keras.losses.sparse_categorical_crossentropy,
                      optimizer=opt,
                      metrics=['accuracy'])

    history = model.fit(x_train,
                        y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        validation_split=0.2,
                        verbose=0,
                        shuffle=False,
                        callbacks=[early_stop])

    return model, history
Ejemplo n.º 18
0
def main():
    dataset = Tracking(dataset_dir, num_validation_y=0.2, seed=123)
    train_x, train_y, _ = dataset.read_train()
    print("Train set size: %d images, %d identities" %
          (len(train_x), len(np.unique(train_y))))
    valid_x, valid_y, camera_indices = dataset.read_validation()
    print("Validation set size: %d images, %d identities" %
          (len(valid_x), len(np.unique(valid_y))))
    train = extract_features(train_x)
    # print(list(zip(train,train_y[:20])))
    # data = zip(train,train_y[:20])
    # data = list(data)
    # # save to file
    # dump(train, open('train_x.pkl', 'wb'))
    # lookback = 3
    # pre = 1
    # inputs = np.zeros((len(data) - lookback, lookback, 3))
    # labels = np.zeros(len(data) - lookback)
    # for i in range(lookback, len(data) - (pre)):
    #     inputs[i - lookback] = data[0][i - lookback:i]
    #     labels[i - lookback] = data[1][i + pre]
    # inputs = inputs.reshape(-1, lookback, 1)
    # labels = labels.reshape(-1, 1)
    # print("input" ,inputs,"lavel",labels)
    train = np.asarray((train))
    train_y = np.asarray((train_y))
    print(train_y.shape)
    print(train.shape)

    model = Sequential()
    model.add(LSTM(units=64, activation='elu', input_shape=(None, 1)))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    opt = keras.optimizers.Adam(learning_rate=0.001)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])
    print(model.summary())
    train_data = tf.data.Dataset.from_tensor_slices((train, train_y[:20]))
    model.fit(train_data, epochs=10, batch_size=64)
Ejemplo n.º 19
0
def prueba_1():
	docs = ['Well done!',
			'Good work',
			'Great effort',
			'nice work',
			'Excellent!',
			'Weak',
			'Poor effort!',
			'not good',
			'poor work',
			'Could have done better.']
	# define class labels
	labels = np.array([1,1,1,1,1,0,0,0,0,0])

	# integer encode the documents
	vocab_size = 50
	encoded_docs = [one_hot(d, vocab_size) for d in docs]
	print(encoded_docs)

	# pad documents to a max length of 4 words
	max_length = 4
	padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
	print(padded_docs)

	# define the model
	model = Sequential()
	model.add(Embedding(vocab_size, 8, input_length=max_length))
	model.add(Flatten())
	model.add(Dense(1, activation='sigmoid'))
	# compile the model
	model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
	# summarize the model
	print(model.summary())

	# fit the model
	model.fit(padded_docs, labels, epochs=50, verbose=0)
	# evaluate the model
	loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
	print('Accuracy: %f' % (accuracy*100))
Ejemplo n.º 20
0
    def variant_default(cls, x_train, y_train, x_val, y_val, params):
        model = Sequential()

        model.add(
            TimeDistributed(
                Flatten(input_shape=(x_train.shape[1],
                                     x_train.shape[2] * x_train.shape[3]))))
        model.add(
            TimeDistributed(
                Flatten(input_shape=(x_train.shape[1],
                                     x_train.shape[2] * x_train.shape[3]))))

        for i in range(params["lstm_layers"] - 1):
            model.add(
                LSTM(
                    params["hidden_size"],
                    return_sequences=True,
                    activation=params["activation"],
                ))

        model.add(LSTM(params["hidden_size"], activation=params["activation"]))

        if params["dropout"] > 0:
            model.add(Dropout(params["dropout"]))

        model.add(Dense(10, activation=params["output_activation"]))

        model.compile(
            optimizer=tf.keras.optimizers.SGD(lr=params["lr"],
                                              momentum=params["momentum"]),
            loss="sparse_categorical_crossentropy",
            metrics=["accuracy"],
        )

        history = model.fit(
            x_train,
            y_train,
            validation_data=(x_val, y_val),
            epochs=params["epochs"],
            batch_size=params["batch_size"],
            verbose=1,
            callbacks=[
                tf.keras.callbacks.TensorBoard(
                    "./logs/" + "lstm_default/" +
                    "-".join("=".join((str(k), str(v)))
                             for k, v in params.items()) +
                    "-ts={}".format(str(time.time())))
            ],
        )

        return history, model
Ejemplo n.º 21
0
    def trainnet(self):
        classifier = Sequential([
            layers.Dense(4,
                         activation='relu',
                         kernel_initializer='random_normal',
                         input_dim=8),
            layers.Dense(4,
                         activation='relu',
                         kernel_initializer='random_normal'),
            layers.Dense(1,
                         activation='sigmoid',
                         kernel_initializer='random_normal')
        ])
        # Compiling the neural network
        classifier.compile(optimizer='adam',
                           loss='binary_crossentropy',
                           metrics=['accuracy'])
        # Fitting the data to the training dataset
        classifier.fit(self.X_train, self.y_train, batch_size=10, epochs=100)
        eval_model = classifier.evaluate(self.X_train, self.y_train)
        print(eval_model)

        nethandler().savenet(model=classifier)
Ejemplo n.º 22
0
def train_classifier():
    data = get_data()

    classifier = Sequential()
    classifier.add(
        Dense(100,
              activation=tf.nn.relu,
              input_shape=(FLAGS.sentence_embedding_size, )))
    for i in range(1 - 1):
        classifier.add(
            Dense(100,
                  activation='relu',
                  kernel_regularizer=tf.keras.regularizers.l2(0.3)))
        classifier.add(Dropout(0.5))
    classifier.add(Dense(2, activation='softmax'))
    classifier.compile(optimizer=Adagrad(0.01),
                       loss='categorical_crossentropy',
                       metrics=['accuracy',
                                Recall(),
                                Precision(), f1])

    classifier.summary()

    helper._print_header('Training classifier')

    classifier.fit(data['train'][0],
                   data['train'][1],
                   batch_size=FLAGS.classifier_batch_size,
                   validation_data=(data['val'][0], data['val'][1]),
                   epochs=200,
                   callbacks=[
                       EarlyStopping(monitor='val_accuracy',
                                     patience=25,
                                     min_delta=0.01),
                       SaveBestModelCallback()
                   ],
                   verbose=2)
def test_crf_viterbi_accuracy(get_random_data):
    nb_samples = 2
    timesteps = 10
    embedding_dim = 4
    output_dim = 5
    embedding_num = 12

    crf_loss_instance = ConditionalRandomFieldLoss()

    x, y = get_random_data(nb_samples,
                           timesteps,
                           x_high=embedding_num,
                           y_high=output_dim)
    # right padding; left padding is not supported due to the tf.contrib.crf
    x[0, -4:] = 0

    # test with masking, fix length
    model = Sequential()
    model.add(
        Embedding(embedding_num,
                  embedding_dim,
                  input_length=timesteps,
                  mask_zero=True))
    model.add(CRF(output_dim, name="crf_layer"))
    model.compile(optimizer='rmsprop',
                  loss={"crf_layer": crf_loss_instance},
                  metrics=[crf_viterbi_accuracy])

    model.fit(x, y, epochs=1, batch_size=10)

    # test viterbi_acc
    y_pred = model.predict(x)
    _, v_acc = model.evaluate(x, y)
    np_acc = (y_pred[x > 0] == y[x > 0]).astype('float32').mean()
    print(v_acc, np_acc)
    assert np.abs(v_acc - np_acc) < 1e-4
Ejemplo n.º 24
0
class SimpleDense(NNBaseModel):
    def train(self):
        self.model = Sequential()
        self.model.add(Embedding(self.vocab_size, 16))
        self.model.add(GlobalAveragePooling1D())
        self.model.add(Dense(16, activation=tf.nn.relu))
        self.model.add(Dense(self.output_size, activation=tf.nn.sigmoid))
        print(self.model.summary())
        self.model.compile(optimizer='adam',
                           loss='sparse_categorical_crossentropy',
                           metrics=['acc'])
        history = self.model.fit(self.X_train,
                                 self.y_train,
                                 epochs=100,
                                 batch_size=64,
                                 verbose=1)
Ejemplo n.º 25
0
    def test_with_keras(self):
        # Comentar para usar GPU
        tf.config.experimental.set_visible_devices([], 'GPU')
        dataset = DatasetUtils(path='./resources/train_data.csv')
        train, test = dataset.split(80)

        y_train = train[:, -1]
        x_train = train[:, :-1]

        y_test = test[:, -1][:, None]
        x_test = test[:, :-1]

        model = Sequential()

        model.add(Dense(128, input_dim=x_train.shape[1]))
        model.add(Dense(128, activation='relu'))
        model.add(Dense(16, activation='relu'))
        model.add(Dense(1, activation="sigmoid"))

        metrics = [
            keras.metrics.Precision(name="precision"),
            keras.metrics.Recall(name="recall"),
            keras.metrics.BinaryAccuracy(name="Binary Accuracy")
        ]

        model.compile(optimizer=keras.optimizers.Adam(1e-4),
                      loss="binary_crossentropy",
                      metrics=metrics)
        history = model.fit(x_train,
                            y_train,
                            batch_size=16,
                            epochs=200,
                            verbose=1,
                            validation_data=(x_test, y_test))

        PlotUtils().plotHistory(history=history, metrics=metrics, plt=plt)

        plt.figure()
        index_1 = (y_test == 1).reshape(len(y_test))
        index_0 = (y_test == 0).reshape(len(y_test))
        plt.scatter(x_test[index_1, 0], x_test[index_1, 1], c='red')
        plt.scatter(x_test[index_0, 0], x_test[index_0, 1], c='blue')

        PlotUtils().plotDecisionBoundry(x_test, model, plt)

        plt.show()
Ejemplo n.º 26
0
    def train(self):
        model = Sequential()
        model.add(
            DenseNet201(weights="imagenet",
                        include_top=False,
                        input_shape=self.input_shape))
        model.add(Flatten())
        model.add(Dense(1024, activation="relu"))
        model.add(Dense(1, activation="sigmoid"))
        plot_model(model)
        model.summary()
        model.compile(optimizer=Adam(learning_rate=1e-3),
                      loss="binary_crossentropy",
                      metrics=['accuracy'])
        history = model.fit(self.train_data,
                            epochs=100,
                            verbose=1,
                            validation_data=self.valid_data)

        return model, history
def test_masking_fixed_length(get_random_data):
    nb_samples = 2
    timesteps = 10
    embedding_dim = 4
    output_dim = 5
    embedding_num = 12

    crf_loss_instance = ConditionalRandomFieldLoss()

    x, y = get_random_data(nb_samples,
                           timesteps,
                           x_high=embedding_num,
                           y_high=output_dim)
    # right padding; left padding is not supported due to the tf.contrib.crf
    x[0, -4:] = 0

    # test with masking, fix length
    model = Sequential()
    model.add(
        Embedding(embedding_num,
                  embedding_dim,
                  input_length=timesteps,
                  mask_zero=True))
    model.add(CRF(output_dim, name="crf_layer"))
    model.compile(optimizer='adam', loss={"crf_layer": crf_loss_instance})

    model.fit(x, y, epochs=1, batch_size=1)
    model.fit(x, y, epochs=1, batch_size=2)
    model.fit(x, y, epochs=1, batch_size=3)
    model.fit(x, y, epochs=1)

    # check mask
    y_pred = model.predict(x)
    assert (y_pred[0, -4:] == 0).all()  # right padding
    # left padding not working currently due to the tf.contrib.crf.*
    # assert (y_pred[1, :5] == 0).all()

    # test saving and loading model
    MODEL_PERSISTENCE_PATH = './test_saving_crf_model.h5'
    model.save(MODEL_PERSISTENCE_PATH)
    load_model(MODEL_PERSISTENCE_PATH, custom_objects={'CRF': CRF})

    try:
        os.remove(MODEL_PERSISTENCE_PATH)
    except OSError:
        pass
Ejemplo n.º 28
0
class AudioFeaturesModel:
    def __init__(self, model_name, le, layers):
        self.le = le
        self.model = Sequential(name=model_name)
        # Builds layers based on the structure in model_structures
        for layer in layers:
            self.model.add(layer)

    def compile(self):
        """Compile the model and print the structure"""
        self.model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
        self.model.summary()

    def test_model(self, x_data, y_data):
        """Calculate the model's accuracy on the input dataset"""
        score = self.model.evaluate(x_data, y_data, verbose=0)
        accuracy = 100 * score[1]
        return accuracy

    def train_model(self, x_train, y_train, x_val, y_val):
        """Train and save the model"""
        early_stopping = EarlyStopping(monitor='val_loss', patience=sounds_config.patience, mode='min')
        checkpointer = ModelCheckpoint(filepath=f'{sounds_config.sounds_model_dir}/{self.model.name}.hdf5', verbose=1,
                                       save_best_only=True)
        history = self.model.fit(x_train, y_train, batch_size=sounds_config.num_batch_size,
                                 epochs=sounds_config.num_epochs, validation_data=(x_val, y_val),
                                 callbacks=[checkpointer, early_stopping], verbose=1)
        self.le.save(self.model.name)
        return history

    def calculate_confusion_matrix(self, x_test, y_test):
        """Calculate the probabilities required for the confusion matrix and create a dataframe"""
        y_pred = self.model.predict_classes(x_test)
        y_test = argmax(y_test, axis=1)
        con_mat = confusion_matrix(labels=y_test, predictions=y_pred).numpy()
        con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2)
        classes = self.le.inverse_transform(list(range(0, self.le.encoded_labels.shape[1])))
        return pd.DataFrame(con_mat_norm, index=classes, columns=classes)
Ejemplo n.º 29
0
class ImageFeaturesModel:
    def __init__(self, model_name, le, layers):
        self.le = le
        self.model = Sequential(name=model_name)

        for layer in layers:
            self.model.add(layer)

    def compile(self):
        self.model.compile(loss='categorical_crossentropy',
                           metrics=['accuracy'],
                           optimizer='adam')
        self.model.summary()

    def test_model(self, x_data, y_data):
        score = self.model.evaluate(x_data, y_data, verbose=0)
        accuracy = 100 * score[1]
        return accuracy

    def train_model(self, x_train, y_train, x_val, y_val):
        early_stop = EarlyStopping(monitor='val_loss',
                                   mode='min',
                                   verbose=1,
                                   patience=5)
        checkpointer = ModelCheckpoint(filepath=f'{self.model.name}.hdf5',
                                       verbose=1,
                                       save_best_only=True)
        history = self.model.fit(x_train,
                                 y_train,
                                 batch_size=config.num_batch_size,
                                 epochs=config.num_epochs,
                                 validation_data=(x_val, y_val),
                                 callbacks=[early_stop, checkpointer],
                                 verbose=1)
        self.le.save(self.model.name)
        return history
def test_masking_fixed_length(get_random_data):
    nb_samples = 2
    timesteps = 10
    embedding_dim = 4
    output_dim = 5
    embedding_num = 12

    crf_loss_instance = ConditionalRandomFieldLoss()

    x, y = get_random_data(nb_samples,
                           timesteps,
                           x_high=embedding_num,
                           y_high=output_dim)

    # test with no masking, fix length
    model = Sequential()
    model.add(Embedding(embedding_num, embedding_dim, input_length=timesteps))
    model.add(CRF(output_dim, name="crf_layer"))
    model.compile(optimizer='adam', loss={"crf_layer": crf_loss_instance})

    model.fit(x, y, epochs=1, batch_size=1)
    model.fit(x, y, epochs=1, batch_size=2)
    model.fit(x, y, epochs=1, batch_size=3)
    model.fit(x, y, epochs=1)

    # test saving and loading model
    MODEL_PERSISTENCE_PATH = './test_saving_crf_model.h5'
    model.save(MODEL_PERSISTENCE_PATH)
    load_model(MODEL_PERSISTENCE_PATH,
               custom_objects={
                   'CRF': CRF,
                   'crf_loss': crf_loss
               })

    try:
        os.remove(MODEL_PERSISTENCE_PATH)
    except OSError:
        pass