Exemple #1
0
def train():
    # extracting file saved by data_prep.py
    data = np.load('face_data.npz')
    x , y  =  data['x'], data['y']
    #categorical conversion of data label
    y = keras.utils.to_categorical(y, 6)
    # using transfer learning to reduce the time required to train the algo
    resnet = VGGFace(model='resnet50',input_shape=(224, 224, 3))
    
    layer_name = resnet.layers[-2].name
    #adding our own custom layers to make the model work on our datatset
    out = resnet.get_layer(layer_name).output
    out = Dense(6,activation='softmax')(out)
    resnet_4 = Model(resnet.input, out)
    # removing last layer of the model and adding my own layer to it 
    for layer in resnet_4.layers[:-1]:
        layer.trainable = False
        
        resnet_4.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
        #checking the final created dataset 
        print (resnet_4.summary())
        # training the model we have created with our own dataset
        resnet_4.fit(x, y,batch_size=10,epochs=10,shuffle=True)
        #saving the trained model so that it can be used afterwards
        resnet_4.save("C:\\Users\\hseth\\Desktop\\face recogination\\model_save_face.h5")
        # checking the accuracy of the model on training data only as i used a very small dataset
        scores = resnet_4.evaluate(x, y, verbose=1)
        print('Test accuracy:', scores[1])
Exemple #2
0
def test_works():
    a = Input(shape=(None, 4), name="input")
    ls = Sort(initial_beta=np.array([1, 0, 0, 0]), name='sort')
    s = ls(a)
    p_ls = Perturbed_Sort(ls)
    p_s = p_ls(a)
    model = Model(input=[a], output=[s, p_s], name='test')

    # print(Sort(nb_out=5).get_output_shape_for((1,2,3)))

    inp = np.random.random((1000, 300, 4))
    indicies = np.argsort(inp[:, :, 0])
    # print(indicies)
    target = np.array(
        [np.take(inp[i], indicies[i], axis=-2) for i in range(inp.shape[0])])
    # print("Input")
    # print(inp)
    # print("Target")
    # print(target)
    model.compile(optimizer=Finite_Differences(model, ls, p_ls),
                  loss={
                      'sort': 'mse',
                      'pert_sort': 'mse'
                  })
    # model.fit(inp, [target, target], nb_epoch=200,batch_size=1000)
    print(model.evaluate(inp, [target, target], batch_size=1000))
Exemple #3
0
def embedding_binary_classification():
    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 = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
    vocab_size = 50
    encoded_docs = [one_hot(d, vocab_size)
                    for d in docs]  # one_hot编码到[1,n],不包括0
    print(encoded_docs)

    max_length = 4
    padded_docs = pad_sequences(encoded_docs,
                                maxlen=max_length,
                                padding='post')
    print(padded_docs)

    input = Input(shape=(4, ))
    x = Embedding(vocab_size, 8,
                  input_length=max_length)(input)  # 这一步对应的参数量为50*8
    x = Flatten()(x)
    x = Dense(1, activation='sigmoid')(x)
    model = Model(inputs=input, outputs=x)

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

    model.fit(padded_docs, labels, epochs=100, verbose=0)
    loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
    print('loss: {0},accuracy:{1}'.format(loss, accuracy))

    loss_test, accuracy_test = model.evaluate(padded_docs, labels, verbose=0)
    print('loss_test: {0},accuracy_test:{1}'.format(loss_test, accuracy_test))

    test = one_hot('Weak', 50)
    padded_test = pad_sequences([test], maxlen=max_length, padding='post')
    print(model.predict(padded_test))
Exemple #4
0
def train(model: Model,
          x_train,
          y_train,
          x_test,
          y_test,
          batch_size=128,
          epochs=20):
    time = int(datetime.now().timestamp())
    name = "{}_{}".format("conv", time)
    chkp_path = "./checkpoints/{}".format(name)
    os.makedirs(chkp_path, exist_ok=True)

    model.compile(loss=K.categorical_crossentropy,
                  optimizer=Adam(),
                  metrics=[metrics.categorical_accuracy])

    model.fit(
        x_train,
        y_train,
        batch_size=batch_size,
        epochs=epochs,
        validation_data=(x_test, y_test),
        callbacks=[
            TensorBoard(log_dir='/tmp/tensorflow/{}'.format(name)),
            ModelCheckpoint(os.path.join(
                chkp_path,
                "weights-improvement-{epoch:02d}-{val_categorical_accuracy:.2f}.hdf5"
            ),
                            monitor='val_categorical_accuracy',
                            verbose=1,
                            save_best_only=True,
                            mode='auto')
        ])

    export_model(tf.train.Saver(), ['conv2d_1_input'], 'dense_2/Softmax', name)

    score = model.evaluate(x_test, y_test)

    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
Exemple #5
0
    def test_lkrelu(self):
        batch_size = 32
        num_classes = 10

        (x_train, y_train), (x_test, y_test) = load_cifar10()

        inputs = Input(shape=x_train.shape[1:])
        x = Conv2D(self.width, (3, 3))(inputs)
        x = LKReLU()(x)
        x = Flatten()(x)
        x = Dense(num_classes, activation='softmax', name='fc1000')(x)
        model = Model(inputs=inputs, outputs=x)
        print(model.summary())

        opt = keras.optimizers.sgd()

        model.compile(loss='categorical_crossentropy',
                      optimizer=opt,
                      metrics=self.metrics)

        log_dir = 'summaries/width-lkrelu-{}-cifar10-{}-{}'.format(
            self.width, self.seed, datetime.datetime.now())
        model.fit(
            x_train,
            y_train,
            batch_size=batch_size,
            epochs=self.epochs,
            validation_data=(x_test, y_test),
            shuffle=False,
            callbacks=[
                TensorBoard(log_dir=log_dir),
                # ModelCheckpoint(
                # 'checkpoints/width-lkrelu-cifar10-{epoch:02d}-{val_loss:.2f}.hdf5')
            ])
        score = model.evaluate(x_test, y_test, verbose=0)
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])
Exemple #6
0
def main(_):

    # The data, shuffled and split between train and test sets:
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    inputs = Input(shape=(32, 32, 3))

    out1, out2 = simple_cnn(inputs)

    model = Model(inputs=inputs, outputs=[out1, out2])
    # model.summary()

    opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
    model.compile(
        optimizer=opt,
        loss=['categorical_crossentropy', 'categorical_crossentropy'],
        metrics=['accuracy'])

    if not data_augmentation:
        print('Not using data augmentation.')
        model.fit(x_train, [y_train, y_train],
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(x_test, [y_test, y_test]),
                  shuffle=True)
    else:
        print('Using real-time data augmentation.')

        def train_generator(x, y, batch_size):
            train_datagen = ImageDataGenerator(
                width_shift_range=
                0.1,  # randomly shift images horizontally (fraction of total width)
                height_shift_range=
                0.1,  # randomly shift images vertically (fraction of total height)
                horizontal_flip=True)  # randomly flip images
            generator = train_datagen.flow(x, y, batch_size=batch_size)
            while 1:
                x_batch, y_batch = generator.next()
                yield (x_batch, [y_batch, y_batch])

        # Fit the model on the batches generated by datagen.flow().
        model.fit_generator(generator=train_generator(x_train, y_train,
                                                      batch_size),
                            steps_per_epoch=int(y_train.shape[0] / batch_size),
                            epochs=epochs,
                            validation_data=(x_test, [y_test, y_test]),
                            callbacks=[])

    # Save model and weights
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    model_path = os.path.join(save_dir, model_name)
    model.save(model_path)
    print('Saved trained model at %s ' % model_path)

    # Score trained model.
    scores = model.evaluate(x_test, [y_test, y_test], verbose=1)
    print('Test loss:', scores[0])
    print('Test accuracy:', scores[1])
Exemple #7
0
m = Dense(32, activation='relu')(m)
op = Dense(3, activation='softmax')(m)

model = Model(input=ip, output=op)

model.summary()

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

history4 = model.fit(train_X_ex3,
                     train_y,
                     epochs=100,
                     batch_size=32,
                     verbose=0,
                     validation_data=(test_X_ex3, test_y))

plt.plot(history4.history['accuracy'], label='Train Accuracy')
plt.plot(history4.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

corrects, wrongs = model.evaluate(train_X_ex3, train_y)
print("accuracy train: ", corrects / (corrects + wrongs))
corrects, wrongs = model.evaluate(test_X_ex3, test_y)
print("accuracy: test", corrects / (corrects + wrongs))

cm = model.confusion_matrix(train_X_ex3, train_y)
Exemple #8
0
class RankNet(ObjectRanker, Tunable):

    def __init__(self, n_features, n_hidden=2, n_units=8,
                 loss_function=binary_crossentropy, batch_normalization=True,
                 kernel_regularizer=l2(l=1e-4), non_linearities='relu',
                 optimizer="adam", metrics=[top_k_categorical_accuracy, binary_accuracy],
                 batch_size=256, random_state=None, **kwargs):
        """Create an instance of the RankNet architecture.

        RankNet breaks the rankings into pairwise comparisons and learns a
        latent utility model for the objects.

        Parameters
        ----------
        n_features : int
            Number of features of the object space
        n_hidden : int
            Number of hidden layers used in the scoring network
        n_units : int
            Number of hidden units in each layer of the scoring network
        loss_function : function or string
            Loss function to be used for the binary decision task of the
            pairwise comparisons
        batch_normalization : bool
            Whether to use batch normalization in each hidden layer
        kernel_regularizer : function
            Regularizer function applied to all the hidden weight matrices.
        non_linearities : function or string
            Type of activation function to use in each hidden layer
        optimizer : function or string
            Optimizer to use during stochastic gradient descent
        metrics : list
            List of metrics to evaluate during training (can be
            non-differentiable)
        batch_size : int
            Batch size to use during training
        random_state : int, RandomState instance or None
            Seed of the pseudorandom generator or a RandomState instance
        **kwargs
            Keyword arguments for the algorithms

        References
        ----------
        .. [1] Burges, C. et al. (2005, August).
               "Learning to rank using gradient descent.",
               In Proceedings of the 22nd international conference on Machine
               learning (pp. 89-96). ACM.
        .. [2] Burges, C. J. (2010).
               "From ranknet to lambdarank to lambdamart: An overview.",
               Learning, 11(23-581), 81.
        """
        self.logger = logging.getLogger(RankNet.__name__)
        self.n_features = n_features
        self.batch_normalization = batch_normalization
        self.non_linearities = non_linearities
        self.metrics = metrics
        self.kernel_regularizer = kernel_regularizer
        self.loss_function = loss_function
        self.optimizer = optimizers.get(optimizer)
        self.n_hidden = n_hidden
        self.n_units = n_units
        self._construct_layers()
        self.threshold_instances = THRESHOLD
        self.batch_size = batch_size
        self.random_state = check_random_state(random_state)

    def _construct_layers(self, **kwargs):
        self.x1 = Input(shape=(self.n_features,))
        self.x2 = Input(shape=(self.n_features,))
        self.output_node = Dense(1, activation='sigmoid',
                                 kernel_regularizer=self.kernel_regularizer)
        self.output_layer_score = Dense(1, activation='linear')
        if self.batch_normalization:
            self.hidden_layers = [NormalizedDense(self.n_units, name="hidden_{}".format(x),
                                                  kernel_regularizer=self.kernel_regularizer,
                                                  activation=self.non_linearities
                                                  ) for x in range(self.n_hidden)]
        else:
            self.hidden_layers = [Dense(self.n_units, name="hidden_{}".format(x),
                                        kernel_regularizer=self.kernel_regularizer,
                                        activation=self.non_linearities)
                                  for x in range(self.n_hidden)]
        assert len(self.hidden_layers) == self.n_hidden

    def fit(self, X, Y, epochs=10, callbacks=None,
            validation_split=0.1, verbose=0, **kwd):

        self.logger.debug('Creating the Dataset')

        garbage, X1, X2, garbage, Y_single = generate_complete_pairwise_dataset(X, Y)
        del garbage
        if (X1.shape[0] > self.threshold_instances):
            indicies = self.random_state.choice(X1.shape[0], self.threshold_instances, replace=False)
            X1 = X1[indicies, :]
            X2 = X2[indicies, :]
            Y_single = Y_single[indicies]

        self.logger.debug('Finished the Dataset')

        self.logger.debug('Creating the model')

        output = self.construct_model()

        # Model with input as two objects and output as probability of x1>x2
        self.model = Model(inputs=[self.x1, self.x2], outputs=output)

        self.model.compile(loss=self.loss_function, optimizer=self.optimizer, metrics=self.metrics)
        self.logger.debug('Finished Creating the model, now fitting started')

        self.model.fit([X1, X2], Y_single, batch_size=self.batch_size, epochs=epochs,
                       callbacks=callbacks, validation_split=validation_split,
                       verbose=verbose, **kwd)
        self.scoring_model = self._create_scoring_model()

        self.logger.debug('Fitting Complete')

    def construct_model(self):
        # weight sharing using same hidden layer for two objects
        enc_x1 = self.hidden_layers[0](self.x1)
        enc_x2 = self.hidden_layers[0](self.x2)
        neg_x2 = Lambda(lambda x: -x)(enc_x2)
        for hidden_layer in self.hidden_layers[1:]:
            enc_x1 = hidden_layer(enc_x1)
            neg_x2 = hidden_layer(neg_x2)
        merged_inputs = add([enc_x1, neg_x2])
        output = self.output_node(merged_inputs)
        return output

    def _create_scoring_model(self):
        inp = Input(shape=(self.n_features,))
        x = inp
        for hidden_layer in self.hidden_layers:
            x = hidden_layer(x)
        output_score = self.output_node(x)
        model = Model(inputs=[inp], outputs=output_score)
        return model

    def predict(self, X, **kwargs):
        return super().predict(X, **kwargs)

    def _predict_scores_fixed(self, X, **kwargs):
        # assert X1.shape[1] == self.n_features
        n_instances, n_objects, n_features = X.shape
        self.logger.info("For Test instances {} objects {} features {}".format(n_instances, n_objects, n_features))
        scores = []
        for X1 in X:
            score = self.scoring_model.predict(X1, **kwargs)
            score = score.flatten()
            scores.append(score)
        scores = np.array(scores)
        self.logger.info("Done predicting scores")
        return scores

    def predict_scores(self, X, **kwargs):
        return super().predict_scores(X, **kwargs)

    def predict_pair(self, X1, X2, **kwargs):
        pairwise = np.empty(2)
        pairwise[0] = self.model.predict([X1, X2], **kwargs)
        pairwise[1] = self.model.predict([X2, X1], **kwargs)
        return pairwise

    def evaluate(self, X1_test, X2_test, Y_test, **kwargs):
        return self.model.evaluate([X1_test, X2_test], Y_test, **kwargs)

    def set_tunable_parameters(self, n_hidden=32,
                               n_units=2,
                               reg_strength=1e-4,
                               learning_rate=1e-3,
                               batch_size=128, **point):
        self.n_hidden = n_hidden
        self.n_units = n_units
        self.kernel_regularizer = l2(reg_strength)
        self.batch_size = batch_size
        K.set_value(self.optimizer.lr, learning_rate)
        self._construct_layers()
        if len(point) > 0:
            self.logger.warning('This ranking algorithm does not support'
                                ' tunable parameters'
                                ' called: {}'.format(print_dictionary(point)))
                include_top=False,
                input_shape=(224, 224, 3))
print(model.layers)

last_layer = model.get_layer('avg_pool').output

x = Flatten()(last_layer)
x = Dense(target_class, name='fc1')(x)
x = Activation('softmax', name='fc1/softmax')(x)

custom_model = Model(model.input, x)

print(custom_model.layers)

for layer in custom_model.layers:
    layer.trainable = True

if not RetrainAllLayers:
    for layer in custom_model.layers[:-2 * (newLayers)]:
        layer.trainable = False

custom_model.compile(optimizer=RMSprop(lr=1e-4),
                     loss='categorical_crossentropy',
                     metrics=['accuracy'])
custom_model.fit(x=x_train, y=y_train, epochs=20, batch_size=32)
result = custom_model.evaluate(x_test, y_test)

print("&&&")
print(result)

custom_model.save('resnet-retrained.h5')
Exemple #10
0
class FETANetwork(ObjectRanker, Tunable):
    def __init__(self,
                 n_objects,
                 n_features,
                 n_hidden=2,
                 n_units=8,
                 add_zeroth_order_model=False,
                 max_number_of_objects=5,
                 num_subsample=5,
                 loss_function=hinged_rank_loss,
                 batch_normalization=False,
                 kernel_regularizer=l2(l=1e-4),
                 non_linearities='selu',
                 optimizer="adam",
                 metrics=None,
                 use_early_stopping=False,
                 es_patience=300,
                 batch_size=256,
                 random_state=None,
                 **kwargs):
        """
        Create a FETA-network architecture for object ranking.
        Training and prediction complexity is quadratic in the number of objects.

        Parameters
        ----------
        n_objects : int
            Number of objects to be ranked
        n_features : int
            Dimensionality of the feature space of each object
        n_hidden : int
            Number of hidden layers
        n_units : int
            Number of hidden units in each layer
        add_zeroth_order_model : bool
            True if the model should include a latent utility function
        max_number_of_objects : int
            The maximum number of objects to train from
        num_subsample : int
            Number of objects to subsample to
        loss_function : function
            Differentiable loss function for the score vector
        batch_normalization : bool
            Whether to use batch normalization in the hidden layers
        kernel_regularizer : function
            Regularizer to use in the hidden units
        non_linearities : string or function
            Activation function to use in the hidden units
        optimizer : string or function
            Stochastic gradient optimizer
        metrics : list
            List of evaluation metrics (can be non-differentiable)
        batch_size : int
            Batch size to use for training
        random_state : int or object
            Numpy random state
        **kwargs
            Keyword arguments for the hidden units
        """
        self.logger = logging.getLogger(FETANetwork.__name__)

        self.random_state = check_random_state(random_state)

        self.kernel_regularizer = kernel_regularizer
        self.batch_normalization = batch_normalization
        self.non_linearities = non_linearities
        self.loss_function = loss_function
        self.metrics = metrics
        self._n_objects = n_objects
        self.max_number_of_objects = max_number_of_objects
        self.num_subsample = num_subsample
        self.n_features = n_features
        self.batch_size = batch_size

        self.optimizer = optimizers.get(optimizer)
        self._use_zeroth_model = add_zeroth_order_model
        self.n_hidden = n_hidden
        self.n_units = n_units
        self._construct_layers()

    @property
    def n_objects(self):
        if self._n_objects > self.max_number_of_objects:
            return self.max_number_of_objects
        return self._n_objects

    def _construct_layers(self, **kwargs):
        self.input_layer = Input(shape=(self.n_objects, self.n_features))
        # Todo: Variable sized input
        # X = Input(shape=(None, n_features))
        if self.batch_normalization:
            if self._use_zeroth_model:
                self.hidden_layers_zeroth = [
                    NormalizedDense(self.n_units,
                                    name="hidden_zeroth_{}".format(x),
                                    kernel_regularizer=self.kernel_regularizer,
                                    activation=self.non_linearities,
                                    **kwargs) for x in range(self.n_hidden)
                ]
            self.hidden_layers = [
                NormalizedDense(self.n_units,
                                name="hidden_{}".format(x),
                                kernel_regularizer=self.kernel_regularizer,
                                activation=self.non_linearities,
                                **kwargs) for x in range(self.n_hidden)
            ]
        else:
            if self._use_zeroth_model:
                self.hidden_layers_zeroth = [
                    Dense(self.n_units,
                          name="hidden_zeroth_{}".format(x),
                          kernel_regularizer=self.kernel_regularizer,
                          activation=self.non_linearities)
                    for x in range(self.n_hidden)
                ]
            self.hidden_layers = [
                Dense(self.n_units,
                      name="hidden_{}".format(x),
                      kernel_regularizer=self.kernel_regularizer,
                      activation=self.non_linearities)
                for x in range(self.n_hidden)
            ]
        assert len(self.hidden_layers) == self.n_hidden
        self.output_node = Dense(1,
                                 activation='sigmoid',
                                 kernel_regularizer=self.kernel_regularizer)
        if self._use_zeroth_model:
            self.output_node_zeroth = Dense(
                1,
                activation='sigmoid',
                kernel_regularizer=self.kernel_regularizer)

    def _create_zeroth_order_model(self):
        inp = Input(shape=(self.n_features, ))

        x = inp
        for hidden in self.hidden_layers_zeroth:
            x = hidden(x)
        zeroth_output = self.output_node_zeroth(x)

        return Model(inputs=[inp], outputs=zeroth_output)

    def _create_pairwise_model(self):
        x1 = Input(shape=(self.n_features, ))
        x2 = Input(shape=(self.n_features, ))

        x1x2 = concatenate([x1, x2])
        x2x1 = concatenate([x2, x1])

        for hidden in self.hidden_layers:
            x1x2 = hidden(x1x2)
            x2x1 = hidden(x2x1)

        merged_left = concatenate([x1x2, x2x1])
        merged_right = concatenate([x2x1, x1x2])

        N_g = self.output_node(merged_left)
        N_l = self.output_node(merged_right)

        merged_output = concatenate([N_g, N_l])

        return Model(inputs=[x1, x2], outputs=merged_output)

    def _predict_pair(self, a, b, only_pairwise=False, **kwargs):
        # TODO: Is this working correctly?
        pairwise = self.pairwise_model.predict([a, b], **kwargs)
        if not only_pairwise and self._use_zeroth_model:
            utility_a = self.zero_order_model.predict([a])
            utility_b = self.zero_order_model.predict([b])
            return pairwise + (utility_a, utility_b)
        return pairwise

    def _predict_scores_using_pairs(self, X, **kwd):
        n_instances, n_objects, n_features = X.shape
        n2 = n_objects * (n_objects - 1)
        pairs = np.empty((n2, 2, n_features))
        scores = np.zeros((n_instances, n_objects))
        for n in range(n_instances):
            if self._use_zeroth_model:
                scores[n] = self.zero_order_model.predict(X[n]).ravel()
            for k, (i, j) in enumerate(permutations(range(n_objects), 2)):
                pairs[k] = (X[n, i], X[n, j])
            result = self._predict_pair(pairs[:, 0],
                                        pairs[:, 1],
                                        only_pairwise=True,
                                        **kwd)[:, 0]
            scores[n] += result.reshape(n_objects, n_objects - 1).mean(axis=1)
            del result
        del pairs
        return scores

    def fit(self,
            X,
            Y,
            epochs=10,
            callbacks=None,
            validation_split=0.1,
            verbose=0,
            **kwd):
        self.logger.debug('Enter fit function...')

        X, Y = self.sub_sampling(X, Y)
        scores = self.construct_model()
        self.model = Model(inputs=self.input_layer, outputs=scores)

        self.pairwise_model = self._create_pairwise_model()
        if self._use_zeroth_model:
            self.zero_order_model = self._create_zeroth_order_model()

        self.logger.debug('Compiling complete model...')
        self.model.compile(loss=self.loss_function,
                           optimizer=self.optimizer,
                           metrics=self.metrics)
        self.logger.debug('Starting gradient descent...')

        self.model.fit(x=X,
                       y=Y,
                       batch_size=self.batch_size,
                       epochs=epochs,
                       callbacks=callbacks,
                       validation_split=validation_split,
                       verbose=verbose,
                       **kwd)

    def construct_model(self):
        def create_input_lambda(i):
            return Lambda(lambda x: x[:, i])

        if self._use_zeroth_model:
            self.logger.debug('Create 0th order model')
            zeroth_order_outputs = []
            inputs = []
            for i in range(self.n_objects):
                x = create_input_lambda(i)(self.input_layer)
                inputs.append(x)
                for hidden in self.hidden_layers_zeroth:
                    x = hidden(x)
                zeroth_order_outputs.append(self.output_node_zeroth(x))
            zeroth_order_scores = concatenate(zeroth_order_outputs)
            self.logger.debug('0th order model finished')
        self.logger.debug('Create 1st order model')
        outputs = [list() for _ in range(self.n_objects)]
        for i, j in combinations(range(self.n_objects), 2):
            if self._use_zeroth_model:
                x1 = inputs[i]
                x2 = inputs[j]
            else:
                x1 = create_input_lambda(i)(self.input_layer)
                x2 = create_input_lambda(j)(self.input_layer)
            x1x2 = concatenate([x1, x2])
            x2x1 = concatenate([x2, x1])

            for hidden in self.hidden_layers:
                x1x2 = hidden(x1x2)
                x2x1 = hidden(x2x1)

            merged_left = concatenate([x1x2, x2x1])
            merged_right = concatenate([x2x1, x1x2])

            N_g = self.output_node(merged_left)
            N_l = self.output_node(merged_right)

            outputs[i].append(N_g)
            outputs[j].append(N_l)
        # convert rows of pairwise matrix to keras layers:
        outputs = [concatenate(x) for x in outputs]
        # compute utility scores:
        sum_fun = lambda s: K.mean(s, axis=1, keepdims=True)
        scores = [Lambda(sum_fun)(x) for x in outputs]
        scores = concatenate(scores)
        self.logger.debug('1st order model finished')
        if self._use_zeroth_model:
            scores = add([scores, zeroth_order_scores])
        return scores

    def sub_sampling(self, X, Y):
        if self._n_objects > self.max_number_of_objects:
            bucket_size = int(self._n_objects / self.max_number_of_objects)
            idx = self.random_state.randint(bucket_size,
                                            size=(len(X), self.n_objects))
            # TODO: subsampling multiple rankings
            idx += np.arange(start=0, stop=self._n_objects,
                             step=bucket_size)[:self.n_objects]
            X = X[np.arange(X.shape[0])[:, None], idx]
            Y = Y[np.arange(X.shape[0])[:, None], idx]
            tmp_sort = Y.argsort(axis=-1)
            Y = np.empty_like(Y)
            Y[np.arange(len(X))[:, None], tmp_sort] = np.arange(self.n_objects)
        return X, Y

    def _predict_scores_fixed(self, X, **kwargs):
        n_instances, n_objects, n_features = tensorify(X).get_shape().as_list()
        self.logger.info("For Test instances {} objects {} features {}".format(
            n_instances, n_objects, n_features))
        if self.max_number_of_objects < self._n_objects or self.n_objects != n_objects:
            scores = self._predict_scores_using_pairs(X, **kwargs)
        else:
            scores = self.model.predict(X, **kwargs)
        self.logger.info("Done predicting scores")
        return scores

    def evaluate(self, X, Y, **kwargs):
        scores = self.model.evaluate(X, Y, **kwargs)
        return scores

    def predict_scores(self, X, **kwargs):
        return super().predict_scores(X, **kwargs)

    def predict(self, X, **kwargs):
        return ObjectRanker.predict(self, X, **kwargs)

    def set_tunable_parameters(self,
                               n_hidden=32,
                               n_units=2,
                               reg_strength=1e-4,
                               learning_rate=1e-3,
                               batch_size=128,
                               **point):
        self.n_hidden = n_hidden
        self.n_units = n_units
        self.kernel_regularizer = l2(reg_strength)
        self.batch_size = batch_size
        K.set_value(self.optimizer.lr, learning_rate)
        self._construct_layers()
        if len(point) > 0:
            self.logger.warning('This ranking algorithm does not support'
                                ' tunable parameters'
                                ' called: {}'.format(print_dictionary(point)))
Exemple #11
0
from keras.layers import Dense
# extracting file saved by data_prep.py
data = np.load('face_data.npz')
x, y = data['x'], data['y']
#categorical conversion of data label
y = keras.utils.to_categorical(y, 6)
# using transfer learning to reduce the time required to train the algo
resnet = VGGFace(model='resnet50', input_shape=(224, 224, 3))

layer_name = resnet.layers[-2].name
#adding our own custom layers to make the model work on our datatset
out = resnet.get_layer(layer_name).output
out = Dense(6, activation='softmax')(out)
resnet_4 = Model(resnet.input, out)
# removing last layer of the model and adding my own layer to it
for layer in resnet_4.layers[:-1]:
    layer.trainable = False

resnet_4.compile(loss='categorical_crossentropy',
                 optimizer='adam',
                 metrics=['accuracy'])
#checking the final created dataset
print(resnet_4.summary())
# training the model we have created with our own dataset
resnet_4.fit(x, y, batch_size=10, epochs=10, shuffle=True)
#saving the trained model so that it can be used afterwards
resnet_4.save("/home/hardik/Desktop/model_save_face.h5")
# checking the accuracy of the model on training data only as i used a very small dataset
scores = resnet_4.evaluate(x, y, verbose=1)
print('Test accuracy:', scores[1])
def main(i, RNN_TYPE):
    # TODO: change to GRU, Recurrent, and SimpleRNN
    # print(i, RNN_TYPE)
    RNN = recurrent.LSTM
    if RNN_TYPE == "gru":
        # print("Starting a GRU: ")
        RNN = recurrent.GRU
        file_name = "history_gru_" + str(i) + ".csv"

    elif RNN_TYPE == "recurrent":
        # print("Starting a Recurrent Unit: ")
        RNN = recurrent.Recurrent
        file_name = "history_recurrent_" + str(i) + ".csv"

    elif RNN_TYPE == "simplernn":
        # print("Starting a SimpleRNN: ")
        RNN = recurrent.SimpleRNN
        file_name = "history_simple_" + str(i) + ".csv"
    else:
        # print("Starting an LSTM")
        file_name = "history_lstm_" + str(i) + ".csv"

    EMBED_HIDDEN_SIZE = 100
    SENT_HIDDEN_SIZE = 100
    QUERY_HIDDEN_SIZE = 100
    BATCH_SIZE = 50
    EPOCHS = 100
    # print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN,
    #                                                            EMBED_HIDDEN_SIZE,
    #                                                            SENT_HIDDEN_SIZE,
    #                                                            QUERY_HIDDEN_SIZE))
    # print(os.getcwd())
    file_list = (os.getcwd())
    base_file = os.getcwd() + "/tasks_1-20_v1-2/en/"
    file_list = (os.listdir(base_file))
    # print(file_list)
    test_file = ""
    train_file = ""
    for file in file_list:
        if file.startswith("qa" + str(i) + "_"):
            if file.endswith("_test.txt"):
                test_file = file
                # print(test_file)
            elif file.endswith("_train.txt"):
                train_file = file
                # print(train_file)
    print(train_file)
    print(test_file)

    f_train = open(base_file + train_file)
    f_test = open(base_file + test_file)

    # try:
    #     path = get_file('babi-tasks-v1-2.tar.gz',
    #                     origin='https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz')
    # except:
    #     print('Error downloading dataset, please download it manually:\n'
    #           '$ wget http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz\n'
    #           '$ mv tasks_1-20_v1-2.tar.gz ~/.keras/datasets/babi-tasks-v1-2.tar.gz')
    #     raise
    # tar = tarfile.open(path)
    # # Default QA1 with 1000 samples
    # # challenge = 'tasks_1-20_v1-2/en/qa1_single-supporting-fact_{}.txt'
    # # QA1 with 10,000 samples
    # # challenge = 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt'
    # # QA2 with 1000 samples
    # challenge = 'tasks_1-20_v1-2/en/qa2_two-supporting-facts_{}.txt'
    # # QA2 with 10,000 samples
    # # challenge = 'tasks_1-20_v1-2/en-10k/qa2_two-supporting-facts_{}.txt'

    # train = get_stories(tar.extractfile(challenge.format('train')))
    # test = get_stories(tar.extractfile(challenge.format('test')))
    # print("training stories:")
    train = get_stories(f_train)
    # print(len(train))
    # print("testing stories:")
    test = get_stories(f_test)
    # print(len(test))
    vocab = set()
    for story, q, answer in train + test:
        vocab |= set(story + q + [answer])
    vocab = sorted(vocab)
    # check_existence(vocab)
    # get_word_vectors_from_pretr_embeddings(train, test, vocab)

    # Reserve 0 for masking via pad_sequences
    vocab_size = len(vocab) + 1
    print("Vocabulary size: ", vocab_size)
    word_idx = dict((c, i + 1) for i, c in enumerate(vocab))
    story_maxlen = max(map(len, (x for x, _, _ in train + test)))
    query_maxlen = max(map(len, (x for _, x, _ in train + test)))

    x, xq, y = vectorize_stories(train, word_idx, story_maxlen, query_maxlen)
    tx, txq, ty = vectorize_stories(test, word_idx,story_maxlen, query_maxlen)
    print('vocab = {}'.format(vocab))
    print('x.shape = {}'.format(x.shape))
    print('xq.shape = {}'.format(xq.shape))
    print('y.shape = {}'.format(y.shape))
    print('story_maxlen, query_maxlen = {}, {}'.format(story_maxlen, query_maxlen))
    print('Build model...')

    pre_trained_emb_weights = get_pre_trained_emb(vocab)
    sentence = layers.Input(shape=(story_maxlen,), dtype='float32')
    encoded_sentence = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(sentence)
    encoded_sentence = layers.Dropout(0.3)(encoded_sentence)
    question = layers.Input(shape=(query_maxlen,), dtype='float32')
    encoded_question = layers.Embedding(vocab_size, EMBED_HIDDEN_SIZE)(question)
    encoded_question = layers.Dropout(0.3)(encoded_question)

    encoded_question = RNN(EMBED_HIDDEN_SIZE)(encoded_question)
    encoded_question = layers.RepeatVector(story_maxlen)(encoded_question)
    merged = layers.add([encoded_sentence, encoded_question])
    merged = RNN(EMBED_HIDDEN_SIZE)(merged)
    merged = layers.Dropout(0.3)(merged)
    preds = layers.Dense(vocab_size, activation='softmax')(merged)
    model = Model([sentence, question], preds)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print('Training')
    history = model.fit([x, xq], y, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_split=0.05)
    pandas.DataFrame(history.history).to_csv("__pre_"+file_name)

    loss, acc = model.evaluate([tx, txq], ty,
                               batch_size=BATCH_SIZE)

    pandas.DataFrame([str(loss)+"_"+ str(acc)]).to_csv("__test_"+RNN_TYPE+"_"+str(i)+".csv")
    print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc))
Exemple #13
0
# Freeze Layers of VGG16
for layer in model.layers[:20]:
    layer.trainable = False

# Was having array issue's so this should fix
y_train = keras.utils.to_categorical(y_train - 1, num_classes=10)
y_test = keras.utils.to_categorical(y_test - 1, num_classes=10)

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

# Fit my training data to the model
model.fit(x_train, y_train, epochs=5, batch_size=32)
# model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data = (x_train, y_train))

# Percent lost and percent correct
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=32)
print("X-Test and Y-Test: ", loss_and_metrics)

# Predict the y values of the unlabeled data set
# classes = model.predict(unlabeled, batch_size=32)
# print(classes)

# Write the data to a file, to loop at later.
# f = open( 'classes.txt', 'w' )
# f.write( 'dict = ' + repr(classes) + '\n' )
# f.close()

# loss_and_metrics = model.evaluate(unlabeled, classes, batch_size = 32)
# print("Unlabeled: " , loss_and_metrics)
last = base_model.get_layer('top_activation').output
x = Flatten()(last)
x = Dense(1024, activation='relu', kernel_initializer="he_uniform")(x)
x = Dense(512, activation='relu', kernel_initializer="he_uniform")(x)
x = Dense(256, activation='relu', kernel_initializer="he_uniform")(x)
x = Dropout(0.5)(x)
output = Dense(10, activation='softmax')(x)

model = Model(base_model.input, output)

model.compile(optimizer=Adam(lr=0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(X_train,
                    y_train,
                    validation_data=(X_test, y_test),
                    epochs=nb_epoch,
                    batch_size=128,
                    verbose=1)
scores = model.evaluate(X_test, y_test, verbose=0)
print("loss: %.2f" % scores[0])
print("acc: %.2f" % scores[1])
# test acc : 0.88

plt.title('model accuracy')
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()
     0: 1.,
     1: 4.,  # 1: 20.,
     2: 1.
 }
 # prepare callback
 histories = my_callbacks.Histories()
 #
 history = model.fit(X_train,
                     y_train,
                     epochs=n_epochs,
                     batch_size=n_batch_size,
                     validation_data=([X_val, y_val]),
                     class_weight=class_weight,
                     callbacks=[histories])
 #
 scores = model.evaluate(X_val, y_val, verbose=0)
 print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
 cvscores.append(scores[1] * 100)
 #
 auc_values = histories.aucs
 aucs.append(auc_values)
 #
 loss_values = histories.losses
 losses.append(loss_values)
 #
 y_pred_kfoldValidationSet_asNumber = model.predict([X_val])
 y_preds = y_pred_kfoldValidationSet_asNumber[:, 0]
 val_output = np.column_stack((X_val_IDs, y_preds))
 np.savetxt("./pythonOutput/y_pred_asNumber_2param_1param_fold_" +
            str(index) + "_.csv",
            val_output,
Exemple #16
0
def train_label_none_label_classification(label_folder,
                                          non_label_folder,
                                          model_file=None):

    c = Config()

    #  Build or load model
    if model_file is None:
        # create model
        img_input = Input(shape=(28, 28, 3))
        # prediction = model_cnn_2_layer.nn_classify_label_non_label(img_input)
        # prediction = model_cnn_3_layer.nn_classify_label_non_label(img_input)
        prediction = nn_cnn_3_layer.nn_classify_label_non_label(img_input)
        model = Model(inputs=img_input, outputs=prediction)
        model.compile(loss='categorical_crossentropy',
                      optimizer=RMSprop(),
                      metrics=['accuracy'])
    else:
        model = load_model(model_file)

    model.summary()

    # Load and normalize data
    x_train, y_train, x_test, y_test = load_train_validation_data(
        label_folder, non_label_folder)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    x_train[:, :, :, 0] -= c.img_channel_mean[0]
    x_train[:, :, :, 1] -= c.img_channel_mean[1]
    x_train[:, :, :, 2] -= c.img_channel_mean[2]
    x_test[:, :, :, 0] -= c.img_channel_mean[0]
    x_test[:, :, :, 1] -= c.img_channel_mean[1]
    x_test[:, :, :, 2] -= c.img_channel_mean[2]

    x_train /= 255
    x_test /= 255
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # x_train.reshape(x_train.shape[0], 28, 28, 3)
    # x_test.reshape(x_test.shape[0], 28, 28, 3)

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, 2)
    y_test = keras.utils.to_categorical(y_test, 2)

    # Checkpointing is to save the network weights only when there is an improvement in classification accuracy
    # on the validation dataset (monitor=’val_acc’ and mode=’max’).
    file_path = "weights-improvement-{epoch:04d}-{val_acc:.4f}.hdf5"
    checkpoint = ModelCheckpoint(file_path,
                                 monitor='val_acc',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='max')
    callbacks_list = [checkpoint]

    model.fit(x_train,
              y_train,
              batch_size=128,
              epochs=100,
              verbose=1,
              callbacks=callbacks_list,
              validation_data=(x_test, y_test))
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

    model.save('final_model.h5')
Exemple #17
0
def train(train_xy,
          test_xy=None,
          save_path=None,
          input_shape=(224, 224, 3),
          batch_size=128,
          num_epochs=25,
          lr=0.001,
          fixed_low_level=False,
          gender=False):

    train_data, train_label = train_xy
    print 'Training data histogram:', np.sum(train_label, axis=0)
    if test_xy:
        test_data, test_label = test_xy
        print 'Test data histogram:', np.sum(test_label, axis=0)

    num_classes = train_label.shape[1]
    vgg_notop = VGGFace(include_top=False, input_shape=input_shape)

    # We take the output of the last MaxPooling layer.
    last_layer = vgg_notop.get_layer('pool5').output
    x = Flatten(name='flatten')(last_layer)

    # Put two fully-connected layers after it.
    x = Dense(VGG_HIDDEN_DIM, activation='relu', name='fc6')(x)
    x = Dense(VGG_HIDDEN_DIM, activation='relu', name='fc7')(x)

    # Finally, a Dense layer for the output of classes.
    face_probs = Dense(num_classes, name='fc8')(x)

    model = Model(vgg_notop.input, face_probs)

    if fixed_low_level:
        # We make low-level layers untrainable.
        for i in range(len(model.layers) - 3):
            model.layers[i].trainable = False

    # A softmax loss is used for training of this network.
    def softmax(correct, predicted):
        """
        The softmax loss function. For more than 2 one-hot encoded classes.
        """
        return tf.nn.softmax_cross_entropy_with_logits(
            labels=correct, logits=predicted)

    print 'Learning rate: %f' % lr

    model.compile(loss=softmax,
                  optimizer=SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True),
                  metrics=['accuracy'])

    # Training the model.
    if test_xy:
        # Training with validation data.
        model.fit(train_data, train_label,
                  batch_size=batch_size,
                  validation_data=(test_data, test_label),
                  epochs=num_epochs,
                  shuffle=True)
    else:
        # We train one final time to save the model.
        model.fit(train_data, train_label,
                  batch_size=batch_size,
                  epochs=num_epochs,
                  shuffle=True)

    if save_path:
        model.save(save_path)
    else:
        # We only return the validation accuracy.
        return model.evaluate(test_data,
                              test_label,
                              batch_size=batch_size)
Exemple #18
0
def train_label_none_label_classification(label_folder, non_label_folder, model_file=None):

    c = Config()

    #  Build or load model
    if model_file is None:
        # create model
        img_input = Input(shape=(28, 28, 3))
        # prediction = model_cnn_2_layer.nn_classify_label_non_label(img_input)
        # prediction = model_cnn_3_layer.nn_classify_label_non_label(img_input)
        prediction = nn_cnn_3_layer.nn_classify_label_non_label(img_input)
        model = Model(inputs=img_input, outputs=prediction)
        model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])
    else:
        model = load_model(model_file)

    model.summary()

    # Load and normalize data
    x_train, y_train, x_test, y_test = load_train_validation_data(label_folder, non_label_folder)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    x_train[:, :, :, 0] -= c.img_channel_mean[0]
    x_train[:, :, :, 1] -= c.img_channel_mean[1]
    x_train[:, :, :, 2] -= c.img_channel_mean[2]
    x_test[:, :, :, 0] -= c.img_channel_mean[0]
    x_test[:, :, :, 1] -= c.img_channel_mean[1]
    x_test[:, :, :, 2] -= c.img_channel_mean[2]

    x_train /= 255
    x_test /= 255
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # x_train.reshape(x_train.shape[0], 28, 28, 3)
    # x_test.reshape(x_test.shape[0], 28, 28, 3)

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, 2)
    y_test = keras.utils.to_categorical(y_test, 2)

    # Checkpointing is to save the network weights only when there is an improvement in classification accuracy
    # on the validation dataset (monitor=’val_acc’ and mode=’max’).
    file_path = "weights-improvement-{epoch:04d}-{val_acc:.4f}.hdf5"
    checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
    callbacks_list = [checkpoint]

    model.fit(x_train, y_train,
              batch_size=128,
              epochs=100,
              verbose=1,
              callbacks=callbacks_list,
              validation_data=(x_test, y_test)
              )
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

    model.save('final_model.h5')
    monitor='val_acc',
    verbose=1,
    save_best_only=True,
    mode='max')

t = time.time()
hist = custom_resnet_model.fit(X_train,
                               y_train,
                               batch_size=batch_trainsize,
                               epochs=nb_epoch,
                               verbose=1,
                               validation_data=(X_test, y_test),
                               callbacks=[tb, checkpoint])
print('Training time: %s' % (time.time() - t))
(loss, accuracy) = custom_resnet_model.evaluate(X_test,
                                                y_test,
                                                batch_size=batch_testsize,
                                                verbose=1)

print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))

# serialize model to JSON
model_json = custom_resnet_model.to_json()
with open("./outputs/custom_resnet_model1.json", "w") as json_file:
    json_file.write(model_json)

#Save model
custom_resnet_model.save('./trained_models/resnet50model1.h5')
print('model1 resaved.')
del custom_resnet_model  #prevent memory leak
###########################################################################################################################
Exemple #20
0
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=0)

y_train = keras.utils.to_categorical(y_train, 4)
y_test = keras.utils.to_categorical(y_test, 4)

resnet = VGGFace(model='resnet50',input_shape=(224, 224, 3))

layer_name = resnet.layers[-2].name

out = resnet.get_layer(layer_name).output
out = Dense(4,activation='softmax')(out)
resnet_4 = Model(resnet.input, out)

for layer in resnet_4.layers[:-1]:
	layer.trainable = False

resnet_4.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print (resnet_4.summary())

resnet_4.fit(x_train, y_train,batch_size=16,epochs=5,validation_data=(x_test, y_test),shuffle=True)

scores = resnet_4.evaluate(x_test, y_test, verbose=1)
print('Test accuracy:', scores[1])




Exemple #21
0
class RankNet(ObjectRanker, Tunable):
    _tunable = None
    _use_early_stopping = None

    def __init__(self,
                 n_features,
                 n_hidden=2,
                 n_units=8,
                 loss_function=binary_crossentropy,
                 batch_normalization=True,
                 kernel_regularizer=l2(l=0.01),
                 non_linearities='relu',
                 optimizer="adam",
                 metrics=[top_k_categorical_accuracy, binary_accuracy],
                 use_early_stopping=False,
                 es_patience=300,
                 batch_size=256,
                 random_state=None,
                 **kwargs):
        """Create an instance of the RankNet architecture.

        RankNet breaks the rankings into pairwise comparisons and learns a
        latent utility model for the objects.

        Parameters
        ----------
        n_features : int
            Number of features of the object space
        n_hidden : int
            Number of hidden layers used in the scoring network
        n_units : int
            Number of hidden units in each layer of the scoring network
        loss_function : function or string
            Loss function to be used for the binary decision task of the
            pairwise comparisons
        batch_normalization : bool
            Whether to use batch normalization in each hidden layer
        kernel_regularizer : function
            Regularizer function applied to all the hidden weight matrices.
        non_linearities : function or string
            Type of activation function to use in each hidden layer
        optimizer : function or string
            Optimizer to use during stochastic gradient descent
        metrics : list
            List of metrics to evaluate during training (can be
            non-differentiable)
        use_early_stopping : bool
            If True, stop the training early, if no progress has been made for
            es_patience many iterations
        es_patience : int
            If early stopping is enabled, wait for this many iterations without
            progress until stopping the training
        batch_size : int
            Batch size to use during training
        random_state : int, RandomState instance or None
            Seed of the pseudorandom generator or a RandomState instance
        **kwargs
            Keyword arguments for the algorithms

        References
        ----------
        .. [1] Burges, C. et al. (2005, August).
               "Learning to rank using gradient descent.",
               In Proceedings of the 22nd international conference on Machine
               learning (pp. 89-96). ACM.
        .. [2] Burges, C. J. (2010).
               "From ranknet to lambdarank to lambdamart: An overview.",
               Learning, 11(23-581), 81.
        """
        self.logger = logging.getLogger("RankNet")
        self.n_features = n_features
        self.batch_normalization = batch_normalization
        self.non_linearities = non_linearities
        self.early_stopping = EarlyStoppingWithWeights(patience=es_patience)
        self._use_early_stopping = use_early_stopping
        self.metrics = metrics
        self.kernel_regularizer = kernel_regularizer
        self.loss_function = loss_function
        self.optimizer = optimizers.get(optimizer)
        self._construct_layers(n_hidden, n_units)
        self.threshold_instances = THRESHOLD
        self.batch_size = batch_size
        self.random_state = check_random_state(random_state)

    def _construct_layers(self, n_hidden=2, n_units=8, **kwargs):
        self.x1 = Input(shape=(self.n_features, ))
        self.x2 = Input(shape=(self.n_features, ))
        self.output_node = Dense(1,
                                 activation='sigmoid',
                                 kernel_regularizer=self.kernel_regularizer)
        self.output_layer_score = Dense(1, activation='linear')
        if self.batch_normalization:
            self.hidden_layers = [
                NormalizedDense(n_units,
                                name="hidden_{}".format(x),
                                kernel_regularizer=self.kernel_regularizer,
                                activation=self.non_linearities)
                for x in range(n_hidden)
            ]
        else:
            self.hidden_layers = [
                Dense(n_units,
                      name="hidden_{}".format(x),
                      kernel_regularizer=self.kernel_regularizer,
                      activation=self.non_linearities) for x in range(n_hidden)
            ]
        assert len(self.hidden_layers) == n_hidden

    def fit(self,
            X,
            Y,
            epochs=10,
            log_callbacks=None,
            validation_split=0.1,
            verbose=0,
            **kwd):

        self.logger.debug('Creating the Dataset')

        garbage, X1, X2, garbage, Y_single = generate_complete_pairwise_dataset(
            X, Y)
        del garbage
        if (X1.shape[0] > self.threshold_instances):
            indicies = self.random_state.choice(X1.shape[0],
                                                self.threshold_instances,
                                                replace=False)
            X1 = X1[indicies, :]
            X2 = X2[indicies, :]
            Y_single = Y_single[indicies]

        self.logger.debug('Finished the Dataset')

        self.logger.debug('Creating the model')

        output = self.construct_model()

        callbacks = []
        if log_callbacks is None:
            log_callbacks = []
        callbacks.extend(log_callbacks)
        callbacks = self.set_init_lr_callback(callbacks)

        if self._use_early_stopping:
            callbacks.append(self.early_stopping)

        self.logger.info("Callbacks {}".format(', '.join(
            [c.__name__ for c in callbacks])))
        # Model with input as two objects and output as probability of x1>x2
        self.model = Model(inputs=[self.x1, self.x2], outputs=output)

        self.model.compile(loss=self.loss_function,
                           optimizer=self.optimizer,
                           metrics=self.metrics)
        self.logger.debug('Finished Creating the model, now fitting started')

        self.model.fit([X1, X2],
                       Y_single,
                       batch_size=self.batch_size,
                       epochs=epochs,
                       callbacks=callbacks,
                       validation_split=validation_split,
                       verbose=verbose,
                       **kwd)
        self.scoring_model = self._create_scoring_model()

        self.logger.debug('Fitting Complete')

    def construct_model(self):
        # weight sharing using same hidden layer for two objects
        enc_x1 = self.hidden_layers[0](self.x1)
        enc_x2 = self.hidden_layers[0](self.x2)
        neg_x2 = Lambda(lambda x: -x)(enc_x2)
        for hidden_layer in self.hidden_layers[1:]:
            enc_x1 = hidden_layer(enc_x1)
            neg_x2 = hidden_layer(neg_x2)
        merged_inputs = add([enc_x1, neg_x2])
        output = self.output_node(merged_inputs)
        return output

    def _create_scoring_model(self):
        inp = Input(shape=(self.n_features, ))
        x = inp
        for hidden_layer in self.hidden_layers:
            x = hidden_layer(x)
        output_score = self.output_node(x)
        model = Model(inputs=[inp], outputs=output_score)
        return model

    def predict(self, X, **kwargs):
        return super().predict(X, **kwargs)

    def _predict_scores_fixed(self, X, **kwargs):
        # assert X1.shape[1] == self.n_features
        n_instances, n_objects, n_features = X.shape
        self.logger.info("For Test instances {} objects {} features {}".format(
            n_instances, n_objects, n_features))
        scores = []
        for X1 in X:
            score = self.scoring_model.predict(X1, **kwargs)
            score = score.flatten()
            scores.append(score)
        scores = np.array(scores)
        self.logger.info("Done predicting scores")
        return scores

    def predict_scores(self, X, **kwargs):
        return super().predict_scores(X, **kwargs)

    def predict_pair(self, X1, X2, **kwargs):
        pairwise = np.empty(2)
        pairwise[0] = self.model.predict([X1, X2], **kwargs)
        pairwise[1] = self.model.predict([X2, X1], **kwargs)
        return pairwise

    def evaluate(self, X1_test, X2_test, Y_test, **kwargs):
        return self.model.evaluate([X1_test, X2_test], Y_test, **kwargs)

    @classmethod
    def set_tunable_parameter_ranges(cls, param_ranges_dict):
        logger = logging.getLogger('RankNet')
        return tunable_parameters_ranges(cls, logger, param_ranges_dict)

    def set_tunable_parameters(self, point):
        named = Tunable.set_tunable_parameters(self, point)
        hidden_layers_created = False
        for name, param in named.items():
            if name in [
                    N_HIDDEN_LAYERS, N_HIDDEN_UNITS, REGULARIZATION_FACTOR
            ]:
                self.kernel_regularizer = l2(l=named[REGULARIZATION_FACTOR])
                if not hidden_layers_created:
                    self._construct_layers(**named)
                hidden_layers_created = True
            elif name == LEARNING_RATE:
                K.set_value(self.optimizer.lr, param)
            elif name == EARLY_STOPPING_PATIENCE:
                self.early_stopping.patience = param
            elif name == BATCH_SIZE:
                self.batch_size = param
            else:
                self.logger.warning(
                    'This ranking algorithm does not support a tunable parameter called {}'
                    .format(name))

    @classmethod
    def tunable_parameters(cls):
        if cls._tunable is None:
            cls._tunable = OrderedDict([
                (N_HIDDEN_LAYERS, N_HIDDEN_LAYERS_DEFAULT_RANGES),
                (N_HIDDEN_UNITS, N_UNITS_DEFAULT_RANGES),
                (LEARNING_RATE, LR_DEFAULT_RANGE),
                (REGULARIZATION_FACTOR, REGULARIZATION_FACTOR_DEFAULT_RANGE),
                (BATCH_SIZE, BATCH_SIZE_DEFAULT_RANGE),
            ])
            if cls._use_early_stopping:
                cls._tunable[
                    EARLY_STOPPING_PATIENCE] = EARLY_STOPPING_PATIENCE_DEFAULT_RANGE