Esempio n. 1
0
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
# embed()

# Training
# ==================================================
with tf.Graph().as_default():
    session_conf = tf.ConfigProto(allow_soft_placement=True,
                                  log_device_placement=False)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        # embed()
        if (MODEL_TO_RUN == 0):
            model = CNN_LSTM(seq_legth, num_classes, embedding_dim,
                             filter_sizes, num_filters, num_hidden)
        elif (MODEL_TO_RUN == 1):
            model = LSTM_CNN(seq_legth, num_classes, embedding_dim,
                             filter_sizes, num_filters, num_hidden)
        #        elif (MODEL_TO_RUN == 2):
        #            model = CNN(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),
        #                        embedding_dim,filter_sizes,num_filters,l2_reg_lambda)
        #        elif (MODEL_TO_RUN == 3):
        #            model = LSTM(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim)
        else:
            print(
                "PLEASE CHOOSE A VALID MODEL!\n0 = CNN_LSTM\n1 = LSTM_CNN\n2 = CNN\n3 = LSTM\n"
            )
            exit()

        # Define Training procedure
Esempio n. 2
0
print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_dev)))

# embed()

# Training
# ==================================================

with tf.Graph().as_default():
    session_conf = tf.ConfigProto(allow_soft_placement=True,
                                  log_device_placement=False)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        # embed()
        if (MODEL_TO_RUN == 0):
            model = CNN_LSTM(x_train.shape[1], y_train.shape[1],
                             len(vocab_processor.vocabulary_), embedding_dim,
                             filter_sizes, num_filters, l2_reg_lambda)
        elif (MODEL_TO_RUN == 1):
            model = LSTM_CNN(x_train.shape[1], y_train.shape[1],
                             len(vocab_processor.vocabulary_), embedding_dim,
                             filter_sizes, num_filters, l2_reg_lambda)
        elif (MODEL_TO_RUN == 2):
            model = CNN(x_train.shape[1], y_train.shape[1],
                        len(vocab_processor.vocabulary_), embedding_dim,
                        filter_sizes, num_filters, l2_reg_lambda)
        elif (MODEL_TO_RUN == 3):
            model = LSTM(x_train.shape[1], y_train.shape[1],
                         len(vocab_processor.vocabulary_), embedding_dim)
        else:
            print
            "PLEASE CHOOSE A VALID MODEL!\n0 = CNN_LSTM\n1 = LSTM_CNN\n2 = CNN\n3 = LSTM\n"
Esempio n. 3
0
                         batch_size=args.batch_size,
                         shuffle=True,
                         num_workers=16)
validloader = DataLoader(valid,
                         batch_size=args.batch_size,
                         shuffle=False,
                         num_workers=16)
testloader = DataLoader(test,
                        batch_size=args.batch_size,
                        shuffle=False,
                        num_workers=16)

if args.model == "CNN_MLP":
    model = CNN_MLP(args)
elif args.model == "CNN_LSTM":
    model = CNN_LSTM(args)
elif args.model == "Resnet18_MLP":
    model = Resnet18_MLP(args)

if args.resume:
    model.load_model(args.save, 0)
    print('Loaded model')
if args.cuda:
    model = model.cuda()


def train(epoch):
    model.train()
    train_loss = 0
    accuracy = 0
def main(embdFilePath, embdDim, outFolderName, num_epochs):

    # Load data
    print("Loading data...")
    x_text, y = batchgen.get_dataset(goodfile, badfile,
                                     5000)  #TODO: MAX LENGTH

    max_document_length = max([len(x.split(" ")) for x in x_text])

    x, y, vocab_processor = generateData(embdFilePath, embdDim, x_text, y,
                                         max_document_length)
    # Randomly shuffle data
    np.random.seed(42)
    shuffle_indices = np.random.permutation(np.arange(len(y)))
    x_shuffled = x[shuffle_indices]
    y_shuffled = y[shuffle_indices]

    # Split train/test set
    # TODO: This is very crude, should use cross-validation
    dev_sample_index = -1 * int(dev_size * float(len(y)))
    x_train, x_dev = x_shuffled[:dev_sample_index], x_shuffled[
        dev_sample_index:]
    y_train, y_dev = y_shuffled[:dev_sample_index], y_shuffled[
        dev_sample_index:]
    print("Vocabulary Size: {:d}".format(len(vocab_processor.vocabulary_)))
    print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_dev)))

    # Training
    # ==================================================

    with tf.Graph().as_default():
        session_conf = tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=False)
        sess = tf.Session(config=session_conf)
        with sess.as_default():
            model = CNN_LSTM(x_train.shape[1], y_train.shape[1],
                             len(vocab_processor.vocabulary_), embdDim,
                             filter_sizes, num_filters, l2_reg_lambda)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(1e-3)
            grads_and_vars = optimizer.compute_gradients(model.loss)
            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)

            # Keep track of gradient values and sparsity (optional)
            grad_summaries = []
            for g, v in grads_and_vars:
                if g is not None:
                    grad_hist_summary = tf.summary.histogram(
                        "{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar(
                        "{}/grad/sparsity".format(v.name),
                        tf.nn.zero_fraction(g))
                    grad_summaries.append(grad_hist_summary)
                    grad_summaries.append(sparsity_summary)
            grad_summaries_merged = tf.summary.merge(grad_summaries)

            # Output directory for models and summaries
            timestamp = str(int(time.time()))
            out_dir = os.path.abspath(
                os.path.join(os.path.curdir, outFolderName, timestamp))
            print("Writing to {}\n".format(out_dir))

            # Summaries for loss and accuracy
            loss_summary = tf.summary.scalar("loss", model.loss)
            acc_summary = tf.summary.scalar("accuracy", model.accuracy)

            # Train Summaries
            train_summary_op = tf.summary.merge(
                [loss_summary, acc_summary, grad_summaries_merged])
            train_summary_dir = os.path.join(out_dir, "summaries", "train")
            train_summary_writer = tf.summary.FileWriter(
                train_summary_dir, sess.graph)

            # Dev summaries
            dev_summary_op = tf.summary.merge([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
            dev_summary_writer = tf.summary.FileWriter(dev_summary_dir,
                                                       sess.graph)

            # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
            checkpoint_dir = os.path.abspath(
                os.path.join(out_dir, "checkpoints"))
            checkpoint_prefix = os.path.join(checkpoint_dir, "model")
            if not os.path.exists(checkpoint_dir):
                os.makedirs(checkpoint_dir)
            saver = tf.train.Saver(tf.global_variables(),
                                   max_to_keep=num_checkpoints)

            # Write vocabulary
            vocab_processor.save(os.path.join(out_dir, "vocab"))

            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            #TRAINING STEP
            def train_step(x_batch, y_batch, save=False):
                feed_dict = {
                    model.input_x: x_batch,
                    model.input_y: y_batch,
                    model.dropout_keep_prob: dropout_prob
                }
                _, step, summaries, loss, accuracy = sess.run([
                    train_op, global_step, train_summary_op, model.loss,
                    model.accuracy
                ], feed_dict)
                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, loss {:g}, acc {:g}".format(
                    time_str, step, loss, accuracy))
                if save:
                    train_summary_writer.add_summary(summaries, step)

            #EVALUATE MODEL
            def dev_step(x_batch, y_batch, writer=None, save=False):
                feed_dict = {
                    model.input_x: x_batch,
                    model.input_y: y_batch,
                    model.dropout_keep_prob: 0.5
                }
                step, summaries, loss, accuracy = sess.run(
                    [global_step, dev_summary_op, model.loss, model.accuracy],
                    feed_dict)
                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, loss {:g}, acc {:g}".format(
                    time_str, step, loss, accuracy))
                if save:
                    if writer:
                        writer.add_summary(summaries, step)

            #CREATE THE BATCHES GENERATOR
            batches = batchgen.gen_batch(list(zip(x_train, y_train)),
                                         batch_size, num_epochs)

            #TRAIN FOR EACH BATCH
            for batch in batches:
                x_batch, y_batch = zip(*batch)
                train_step(x_batch, y_batch)
                current_step = tf.train.global_step(sess, global_step)
                if current_step % evaluate_every == 0:
                    print("\nEvaluation:")
                    dev_step(x_dev, y_dev, writer=dev_summary_writer)
                    print("")
                if current_step % checkpoint_every == 0:
                    path = saver.save(sess,
                                      checkpoint_prefix,
                                      global_step=current_step)
                    print("Saved model checkpoint to {}\n".format(path))

            dev_step(x_dev, y_dev, writer=dev_summary_writer)

            path = saver.save(sess,
                              checkpoint_prefix,
                              global_step=current_step)
            print("Saved model checkpoint to {}\n".format(path))
Esempio n. 5
0
x_train = x[train_index, :, :]
y_test = y[test_index, :]
y_train = y[train_index, :]
print(x_train.shape)
print(y_train.shape)
# Training
# ==================================================

with tf.Graph().as_default():
    session_conf = tf.ConfigProto(allow_soft_placement=True,
                                  log_device_placement=False)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        # embed()
        if (MODEL_TO_RUN == 0):
            model = CNN_LSTM(x_train.shape[1], y_train.shape[1], embedding_dim,
                             filter_sizes, num_filters, num_hidden)
        elif (MODEL_TO_RUN == 1):
            model = LSTM_CNN(x_train.shape[1], y_train.shape[1], 21,
                             embedding_dim, filter_sizes, num_filters,
                             l2_reg_lambda)
        elif (MODEL_TO_RUN == 2):
            model = CNN(x_train.shape[1], y_train.shape[1], 26, embedding_dim,
                        filter_sizes, num_filters, l2_reg_lambda)
        elif (MODEL_TO_RUN == 3):
            model = LSTM(x_train.shape[1], y_train.shape[1], 26, embedding_dim)
        else:
            print
            "PLEASE CHOOSE A VALID MODEL!\n0 = CNN_LSTM\n1 = LSTM_CNN\n2 = CNN\n3 = LSTM\n"
            exit()

        # Define Training procedure
Esempio n. 6
0
def pre(seq, value):
    global pres0, pres
    X = []
    for sequence in seq:
        sequence = sequence.tolist()
        for i, t in enumerate(sequence):
            t = id2word.get((int(t)))
            if t is None:
                sequence[i] = '0'
            else:
                sequence[i] = t
        sequences = []
        sequence = ''.join(sequence)
        sequence = sequence.replace('X', '0')
        sequence = sequence.replace('U', '0')
        sequence = sequence.replace('O', '0')
        sequence = sequence.replace('B', 'N')
        sequence = sequence.replace('Z', 'Q')
        sequence = sequence.replace('J', 'L')
        sequence = list(sequence)

        if len(sequence) >= max_sequence_size:
            a = int((len(sequence) - max_sequence_size))
            for i in list(range(a)):
                sequence.pop(51)
            # sequences.append(list((ord(t)-64) for t in sequence))
            sequences.append(sequence)
        else:
            b = int((max_sequence_size - len(sequence)))
            for i in list(range(b)):
                sequence.insert(int((len(sequence))), '0')
            sequences.append(sequence)
        X.append(sequences[0])
    acid_letters = [
        '0', 'A', 'C', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'M', 'L', 'N', 'Q',
        'P', 'S', 'R', 'T', 'W', 'V', 'Y'
    ]
    le = LabelEncoder()
    datas = np_utils.to_categorical(le.fit_transform(acid_letters))

    def two2three(x):
        xx = []
        for _, m in enumerate(x):
            k = []
            for j, t in enumerate(m):
                if t not in acid_letters:
                    t = '0'
                n = acid_letters.index(t)
                k.append(datas[n])
            xx.append(k)
        return np.array(xx)

    x_train = np.array(two2three(X))
    print(x_train.shape)
    # Training
    # ==================================================
    batches = batchgen.gen_batch(x_train, batch_size, num_epochs)
    with tf.Graph().as_default():
        session_conf = tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=False)
        sess = tf.Session(config=session_conf)
        with sess.as_default():
            model = CNN_LSTM(x_train.shape[1], embedding_dim, filter_sizes,
                             num_filters, num_hidden)

            # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
            checkpoint_dir = os.path.abspath(
                os.path.join("runs_deeploc", "checkpoints"))
            saver = tf.train.Saver(tf.global_variables(),
                                   max_to_keep=num_checkpoints)
            sess.run(tf.global_variables_initializer())

            # EVALUATE MODEL
            def dev_step(x_batch, writer=None):
                feed_dict = {
                    model.input_x: x_batch,
                    model.dropout_keep_prob: 1
                }
                pre = sess.run(model.predictions, feed_dict)
                return pre

            ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
            saver.restore(sess, ckpt.model_checkpoint_path)
    new_posi = []
    new_nege = []
    '''
    for i,batch in enumerate(batches):
        if i == 0:
            pres0 = dev_step(batch)
        elif i == 1:
            pres1 = dev_step(batch)
            pres = np.concatenate((pres0, pres1))
        else:
            press = dev_step(batch)
            pres = np.concatenate((pres, press))
    '''
    pres = dev_step(x_train)
    # ['Cell.membrane', 'Cytoplasm', 'Endoplasmic.reticulum', 'Extracellular', 'Golgi apparatus', 'Lysosome/Vacuole', 'Mitochandrion', 'Nucleus', 'Peroxisome', 'Plastid']
    # ['Cell.membrane', 'Cytoplasm', 'Endoplasmic.reticulum', 'Golgi.apparatus', 'Lysosome/Vacuole', 'Mitochondrion', 'Nucleus', 'Peroxisome', 'Plastid', 'Extracellular']     [0, 1, 2, 4, 5, 6, 7, 8, 9, 3]
    print(len(pres))
    for i, t in enumerate(pres):
        #print(np.argmax(t))
        if t[7] >= value:
            new_posi.append(seq[i])
        elif t[3] >= value:
            new_nege.append(seq[i])
    print(len(new_posi))
    accuracy = len(new_posi) / len(pres)
    print(accuracy)
    with open("acc_0.65.txt", "a") as f:
        f.write(str(accuracy))
        f.write("\n")
    return new_posi, new_nege
Esempio n. 7
0
    print('test data:', len(x_test), len(y_test))
    print("Vocabulary Size: {:d}".format(n_symbols))
    print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_test)))
    # Training
# ==================================================

with tf.Graph().as_default():
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    session_conf = tf.ConfigProto(allow_soft_placement=True,
                                  log_device_placement=False,
                                  gpu_options=gpu_options)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        #embed()
        if (MODEL_TO_RUN == 0):
            model = CNN_LSTM(max_seq_legth, 1, 5000, 300, filter_sizes,
                             num_filters, l2_reg_lambda)
        elif (MODEL_TO_RUN == 1):
            model = LSTM_CNN(max_seq_legth,
                             1,
                             n_symbols,
                             200,
                             filter_sizes,
                             num_filters,
                             l2_reg_lambda,
                             weight=embedding_weights)
        # elif (MODEL_TO_RUN == 2):
        #     model = CNN(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim,filter_sizes,num_filters,l2_reg_lambda)
        # elif (MODEL_TO_RUN == 3):
        #     model = LSTM(x_train.shape[1],y_train.shape[1],len(vocab_processor.vocabulary_),embedding_dim)
        else:
            print(