예제 #1
0
 def load_NN():
     """
     Loads the Keras NN model for a user-trained metric.
     """
     nn = KerasNN(name)
     nn.load('../data/nns/{}.h5'.format(name))
     return ('nn', nn)
예제 #2
0
def load_mp():
    """
    Loads the Keras NN model for melting point.
    """
    cnn_mp = KerasNN('mp')
    cnn_mp.load('../data/nns/mp.h5')
    return ('cnn', cnn_mp)
예제 #3
0
def load_bandgap():
    """
    Loads the Keras NN model for H**O-LUMO energy
    difference.
    """
    cnn_bandgap = KerasNN('bandgap')
    cnn_bandgap.load('../data/nns/bandgap.h5')
    return ('cnn', cnn_bandgap)
예제 #4
0
def load_PCE():
    """
    Loads the Keras NN model for Power Conversion
    Efficiency.
    """
    cnn_pce = KerasNN('pce')
    cnn_pce.load('../data/nns/pce.h5')
    return ('cnn', cnn_pce)
예제 #5
0
    def train_nn_as_metric(self, name, train_x, train_y, nepochs=1000):
        """Sets up a metric with a neural network trained on
        a dataset.

        Arguments.
        -----------

            - name. String used to identify the metric.

            - train_x. List of SMILES identificators.

            - train_y. List of property values.

            - nepochs. Number of epochs for training.

        Note.
        -----------

            A name.h5 file is generated in the data/nns directory,
            and this metric can be loaded in the future using the
            load_prev_user_metric() method through the name.pkl
            file generated in the data/ dir.

                load_prev_user_metric('name.pkl')

        """

        cnn = KerasNN(name)
        cnn.train(train_x,
                  train_y,
                  500,
                  nepochs,
                  earlystopping=True,
                  min_delta=0.001)
        K.clear_session()

        def batch_NN(smiles, train_smiles=None, nn=None):
            """
            User-trained neural network.
            """
            if nn == None:
                raise ValueError(
                    'The user-trained NN metric was not properly loaded.')
            fsmiles = []
            zeroindex = []
            for k, sm in enumerate(smiles):
                if mm.verify_sequence(sm):
                    fsmiles.append(sm)
                else:
                    fsmiles.append('c1ccccc1')
                    zeroindex.append(k)
            vals = np.asarray(nn.predict(fsmiles))
            for k in zeroindex:
                vals[k] = 0.0
            vals = np.squeeze(np.stack(vals, axis=1))
            return vals

        def load_NN():
            """
            Loads the Keras NN model for a user-trained metric.
            """
            nn = KerasNN(name)
            nn.load('../data/nns/{}.h5'.format(name))
            return ('nn', nn)

        self.AV_METRICS[name] = batch_NN
        self.LOADINGS[name] = load_NN

        if self.verbose:
            print('Defined metric {}'.format(name))

        metric = [batch_NN, load_NN]
        with open('../data/{}.pkl'.format(name), 'wb') as f:
            pickle.dump(metric, f)