コード例 #1
0
    def __init__(self, weight_path, index_path, word_path):
        num_encoder_tokens = 5004
        num_decoder_tokens = 5004
        latent_dim = 256
        _, self.encoder_model, self.decoder_model = build_model(
            latent_dim,
            num_encoder_tokens,
            num_decoder_tokens,
            weight_path=weight_path)
        with open(index_path, 'rb') as handle:
            self.word_to_idx = pickle.load(handle)
            handle.close()

        with open(word_path, 'rb') as handle:
            self.idx_to_word = pickle.load(handle)
            handle.close()
コード例 #2
0
ファイル: build_chatbot.py プロジェクト: surender728/chatbot
def get_model():
    if len(models) == 0:
        model = cm.build_model()
        model.load_weights('chatBotModel')
        models.extend([model])
    return models[0]
コード例 #3
0

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Classify images')
    parser.add_argument('--n_estimators', dest='n_estimators', action='store',
                               default=100, help='This option is only used if a model is to be learned. It specifies the number of estimators in each random forest.\nIn the paper, 100 was used, which requires ~10 GB of RAM. A smaller number consumes less memory, but may result in less variable results. In our (unreported) testing, even a number as small as 10 was still quite good (same average performance, but higher variance). Thus, if memory usage is an issue, try a small number.')

    parser.add_argument('--model', dest='model', action='store',
                        default=MODEL_FILE, help='Model file to use. This an option for advanced users. Normally the default is fine.')

    parser.add_argument('--quiet', dest='quiet', action='store_true',
                                default=False, help='Turn of verbose output')

    parser.add_argument('DNA_file', nargs=1, action='store')
    parser.add_argument('Histone_file', nargs=1, action='store')

    args = parser.parse_args()

    if not path.exists(args.model):
        if args.verbose:
            print("Model has not previously been created.")
            print("Creating now (this will take a long time, but only needs to be run once)...")
        import create_model
        create_model.build_model(args.n_estimators, verbose=(not args.quiet), ofilename=args.model)

    prediction = predict_image(args.DNA_file[0], args.Histone_file[0], verbose=(not args.quiet))
    print("Prediction for image DNA={[0]} & HISTONE={[0]} is {:.1%}".format(args.DNA_file, args.Histone_file, prediction))

コード例 #4
0
ファイル: train.py プロジェクト: IEEE-VIT/HUSE
def main(argv):
    CSV_FILE_PATH = 'data.csv'
    num_epochs = 250000
    BATCH_SIZE = 1
    img_shape = (224, 224, 3)  # Reduce based on RAM
    GRAPH_THRESHOLD = 0.5
    LEARNING_RATE = 1.6192e-05

    # Give importance to classification, semantic and gap loss respectively.
    LOSS_WEIGHTS = [0.6, 0.2, 0.2]
    IMAGE_ENCODER = 'resnet50'
    TEXT_ENCODER = 'bert'

    try:
        opts, args = getopt.getopt(
            argv, "i:t:b:", ["image_encoder=", "text_encoder=", "batch_size="])
    except getopt.GetoptError:
        print('test -i <image_folder> -c <csv_filename>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('test.py -i <inputfile> -o <outputfile>')
            sys.exit()
        elif opt in ("-i", "--image_encoder"):
            IMAGE_ENCODER = arg
        elif opt in ("-c", "--text_encoder"):
            TEXT_ENCODER = arg
        elif opt in ("-b", "--batch_size"):
            BATCH_SIZE = int(arg)
            print("Set batch_size to %d" % BATCH_SIZE)

    df = pd.read_csv(CSV_FILE_PATH)
    num_samples = df.shape[0]

    class_names = df.classes.unique()

    # CONVERT TO CATEGORICAL
    temp = list(df.classes)
    training_class_intmap = temp.copy()

    # map each color to an integer
    mapping = {}

    for x in range(len(class_names)):
        mapping[class_names[x]] = x

    # integer representation
    for x in range(df.shape[0]):
        training_class_intmap[x] = mapping[training_class_intmap[x]]

    training_classes = tf.keras.utils.to_categorical(training_class_intmap)
    image_names = df.image
    text_list = df.text

    text_list = utils.clean_text(text_list)
    num_classes = len(class_names)

    adj_graph_classes = utils.get_adj_graph(class_names)

    if (IMAGE_ENCODER == 'resnet50'):
        image_embedding_extractor_model = encoder.get_resnet50(img_shape)
        image_encoder_size = 2048
    elif (IMAGE_ENCODER == 'resnet101'):
        image_embedding_extractor_model = encoder.get_resnet101(img_shape)
    if (TEXT_ENCODER == 'bert'):
        tokenizer, text_embedding_extractor_model = encoder.get_bert(512)
        text_encoder_size = 768

    complete_model = build_model(image_encoder_size, text_encoder_size,
                                 num_classes)

    train_loss_results = []
    # train_accuracy_results = []
    # Define the optimize and specify the learning rate
    optimizer = tf.keras.optimizers.RMSprop(learning_rate=LEARNING_RATE)
    epoch_time = []
    for epoch in range(num_epochs):
        epoch_start_time = time.time()
        epoch_loss_avg = tf.keras.metrics.Mean()
        # epoch_accuracy = tf.keras.metrics.CategoricalAccuracy() #Uncomment if you want to track
        # Training loop - using batches of 1024
        # encode_and_pack_batch(batch_size, image_encoder, text_encoder, image_names, text_list, training_classes, img_shape):
        xi1, xt1, xi2, xt2, y1, y2 = utils.encode_and_pack_batch(
            BATCH_SIZE, image_embedding_extractor_model,
            text_embedding_extractor_model, image_names, text_list,
            training_classes, img_shape, tokenizer)
        x1 = [xi1, xt1]
        x2 = [xi2, xt2]
        # Optimize the model
        loss_value, grads = grad(complete_model, x1, x2, y1, y2, LOSS_WEIGHTS,
                                 GRAPH_THRESHOLD, adj_graph_classes)
        optimizer.apply_gradients(
            zip(grads, complete_model.trainable_variables))

        # Track progress
        epoch_loss_avg.update_state(loss_value)  # Add current batch loss

        # End epoch
        train_loss_results.append(epoch_loss_avg.result())
        epoch_end_time = time.time()
        epoch_time.append((epoch_end_time - epoch_start_time))
        if epoch % 5 == 0:
            print("Average Epoch time: %s seconds." %
                  str(np.sum(epoch_time) / 5))
            epoch_time = []
            print("Epoch {:03d}: Loss: {:.3f}".format(epoch,
                                                      epoch_loss_avg.result()))
コード例 #5
0
from create_model import build_model
from keras.utils.np_utils import to_categorical
import numpy as np


def to_cat(data):
    return np.array([to_categorical(i, 5004) for i in data])


def seq_seq(model, epoch, X, Y):
    for _ in range(epoch):
        randomizer = np.random.randint(0, 5000000, 10000)
        x_train = to_cat(X[randomizer])
        y = to_cat(Y[randomizer])
        y_train = y[:, :-1, :]
        y_test = y[:, 1:, :]
        model.fit([x_train, y_train], y_test, epochs=1, validation_split=0.2)
    model.save_weights('my_model_weights.h5')


if __name__ == '__main__':
    num_encoder_tokens = 5004
    num_decoder_tokens = 5004
    latent_dim = 256

    model, _, _ = build_model(latent_dim, num_encoder_tokens,
                              num_decoder_tokens)
    X = np.load('X_train.npy')
    Y = np.load('X_train.npy')
    seq_seq(model, 200, X, Y)
コード例 #6
0
ファイル: main.py プロジェクト: zkywsg/automatic-scoring
def main():
    parser = argparse.ArgumentParser(description="sentence Hi_CNN model")
    parser.add_argument('--embedding',
                        type=str,
                        default='glove',
                        help='Word embedding type, word2vec, senna or glove')
    parser.add_argument('--embedding_dict',
                        type=str,
                        default='glove.6B.50d.txt',
                        help='Pretrained embedding path')
    parser.add_argument(
        '--embedding_dim',
        type=int,
        default=50,
        help='Only useful when embedding is randomly initialised')

    parser.add_argument('--num_epochs',
                        type=int,
                        default=50,
                        help='number of epochs for training')
    parser.add_argument('--batch_size',
                        type=int,
                        default=10,
                        help='Number of texts in each batch')
    parser.add_argument("-v",
                        "--vocab-size",
                        dest="vocab_size",
                        type=int,
                        metavar='<int>',
                        default=4000,
                        help="Vocab size (default=4000)")

    parser.add_argument(
        '--optimizer',
        choices=['sgd', 'momentum', 'nesterov', 'adagrad', 'rmsprop'],
        help='updating algorithm',
        default='rmsprop')
    parser.add_argument('--learning_rate',
                        type=float,
                        default=0.001,
                        help='Initial learning rate')
    parser.add_argument('--dropout',
                        type=float,
                        default=0.5,
                        help='Dropout rate for layers')
    parser.add_argument('--oov',
                        choices=['random', 'embedding'],
                        help="Embedding for oov word",
                        default='random',
                        required=False)
    parser.add_argument('--l2_value',
                        type=float,
                        help='l2 regularizer value',
                        default=0.001)
    parser.add_argument('--checkpoint_path',
                        type=str,
                        help='checkpoint1 directory',
                        default='./checkpoint')

    parser.add_argument(
        "--aggregation",
        dest="aggregation",
        type=str,
        metavar='<str>',
        default='attsum',
        help=
        "The aggregation method for regp and bregp types (mot|attsum|attmean) (default=mot)"
    )
    parser.add_argument("--seed",
                        dest="seed",
                        type=int,
                        metavar='<int>',
                        default=1234,
                        help="Random seed (default=1234)")
    parser.add_argument(
        "-r",
        "--rnndim",
        dest="rnn_dim",
        type=int,
        metavar='<int>',
        default=100,
        help="RNN dimension. '0' means no RNN layer (default=300)")
    parser.add_argument(
        "--maxlen",
        dest="maxlen",
        type=int,
        metavar='<int>',
        default=0,
        help=
        "Maximum allowed number of words during training. '0' means no limit (default=0)"
    )
    parser.add_argument(
        "--vocab-path",
        dest="vocab_path",
        type=str,
        metavar='<str>',
        help="(Optional) The path to the existing vocab file (*.pkl)")

    fold = '4'

    parser.add_argument('--train',
                        default='./data/fold_' + fold +
                        '/train.tsv')  # data_path"
    parser.add_argument('--dev', default='./data/fold_' + fold + '/dev.tsv')
    parser.add_argument('--test', default='./data/fold_' + fold + '/test.tsv')
    parser.add_argument('--prompt_id',
                        type=int,
                        default=3,
                        help='prompt id of essay set')
    parser.add_argument(
        '--init_bias',
        action='store_true',
        help='init the last layer bias with average score of training data')

    print('pp3, f' + fold)
    args = parser.parse_args()

    checkpoint_dir = args.checkpoint_path

    embedding_path = args.embedding_dict
    embedding = args.embedding
    embedd_dim = args.embedding_dim

    (train_x, train_y, train_pmt), (dev_x, dev_y, dev_pmt), (
        test_x, test_y, test_pmt
    ), vocab, vocab_size, overal_maxlen, num_outputs = dataset.get_data(
        (args.train, args.dev, args.test),
        args.prompt_id,
        args.vocab_size,
        args.maxlen,
        tokenize_text=True,
        to_lower=True,
        sort_by_len=False,
        vocab_path=None)

    embedd_dict, embedd_dim, _ = load_word_embedding_dict(
        embedding, embedding_path, vocab, logger, embedd_dim)
    embedd_matrix = build_embedd_table(vocab,
                                       embedd_dict,
                                       embedd_dim,
                                       logger,
                                       caseless=True)

    train_x = sequence.pad_sequences(train_x, maxlen=overal_maxlen)
    dev_x = sequence.pad_sequences(dev_x, maxlen=overal_maxlen)
    test_x = sequence.pad_sequences(test_x, maxlen=overal_maxlen)

    import keras.backend as K

    train_y = np.array(train_y, dtype=K.floatx())
    dev_y = np.array(dev_y, dtype=K.floatx())
    test_y = np.array(test_y, dtype=K.floatx())

    train_pmt = np.array(train_pmt, dtype='int32')
    dev_pmt = np.array(dev_pmt, dtype='int32')
    test_pmt = np.array(test_pmt, dtype='int32')

    train_mean = train_y.mean(axis=0)
    scaled_train_mean = dataset.get_model_friendly_scores(
        train_mean, args.prompt_id)

    train_y = dataset.get_model_friendly_scores(train_y, train_pmt)
    dev_y = dataset.get_model_friendly_scores(dev_y, dev_pmt)
    test_y = dataset.get_model_friendly_scores(test_y, test_pmt)
    logger.info('  train_x shape: ' + str(np.array(train_x).shape))
    logger.info('  dev_x shape:   ' + str(np.array(dev_x).shape))
    logger.info('  test_x shape:  ' + str(np.array(test_x).shape))

    logger.info('  train_y shape: ' + str(train_y.shape))
    logger.info('  dev_y shape:   ' + str(dev_y.shape))
    logger.info('  test_y shape:  ' + str(test_y.shape))

    model = build_model(args, overal_maxlen, vocab_size, embedd_dim,
                        embedd_matrix, True, scaled_train_mean)

    evl = Evaluator(args.prompt_id, checkpoint_dir, train_x, dev_x, test_x,
                    train_y, dev_y, test_y)

    # Initial evaluation
    if is_training:
        # logger.info("Initial evaluation: ")
        # evl.evaluate(model, -1, print_info=True)
        logger.info("Train model")
        for ii in xrange(args.num_epochs):

            logger.info('Epoch %s/%s' % (str(ii + 1), args.num_epochs))
            start_time = time.time()

            model.fit({'word_input': train_x},
                      train_y,
                      batch_size=args.batch_size,
                      nb_epoch=1,
                      verbose=0,
                      shuffle=True)
            tt_time = time.time() - start_time
            logger.info("Training one epoch in %.3f s" % tt_time)
            evl.evaluate(model, ii + 1)
            evl.print_info()

        evl.print_final_info()

    print('p' + str(args.prompt_id) + ',f' + fold)
コード例 #7
0

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Classify images')
    parser.add_argument('--n_estimators', dest='n_estimators', action='store',
                               default=100, help='This option is only used if a model is to be learned. It specifies the number of estimators in each random forest.\nIn the paper, 100 was used, which requires ~10 GB of RAM. A smaller number consumes less memory, but may result in less variable results. In our (unreported) testing, even a number as small as 10 was still quite good (same average performance, but higher variance). Thus, if memory usage is an issue, try a small number.')

    parser.add_argument('--model', dest='model', action='store',
                        default=MODEL_FILE, help='Model file to use. This an option for advanced users. Normally the default is fine.')

    parser.add_argument('--quiet', dest='quiet', action='store_true',
                                default=False, help='Turn of verbose output')

    parser.add_argument('DNA_file', nargs=1, action='store')
    parser.add_argument('Histone_file', nargs=1, action='store')

    args = parser.parse_args()

    if not path.exists(args.model):
        if args.verbose:
            print("Model has not previously been created.")
            print("Creating now (this will take a long time, but only needs to be run once)...")
        import create_model
        create_model.build_model(args.n_estimators, verbose=(not args.quiet), ofilename=args.model)

    prediction = predict_image(args.DNA_file[0], args.Histone_file[0], verbose=(not args.quiet))
    print("Prediction for image DNA={[0]} & HISTONE={[0]} is {:.1%}".format(args.DNA_file, args.Histone_file, prediction))

コード例 #8
0
slot_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'slot_vocab'))
intent_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'intent_vocab'))
intent_dim = arg.intent_dim

# Create training model
input_data = tf.placeholder(tf.int32, [None, None], name='inputs')  # word ids
sequence_length = tf.placeholder(tf.int32, [None], name="sequence_length")  # sequence length
global_step = tf.Variable(0, trainable=False, name='global_step')
slots = tf.placeholder(tf.int32, [None, None], name='slots')  # slot ids
slot_weights = tf.placeholder(tf.float32, [None, None], name='slot_weights')  # sequence mask
intent = tf.placeholder(tf.int32, [None], name='intent')  # intent label

with tf.variable_scope('model'):
    training_outputs = build_model(input_data, len(in_vocab['vocab']), sequence_length, len(slot_vocab['vocab']) - 2,
                                   len(intent_vocab['vocab']), intent_dim,
                                   layer_size=arg.layer_size, embed_dim=arg.embed_dim, num_rnn=arg.num_rnn,
                                   isTraining=True, iter_slot=arg.iter_slot, iter_intent=arg.iter_intent,
                                   re_routing=re_routing)

slots_shape = tf.shape(slots)
slots_reshape = tf.reshape(slots, [-1])
slot_outputs = training_outputs[0]
intent_outputs = training_outputs[1]
slot_routing_weight = training_outputs[2]
intent_routing_weight = training_outputs[3]
intent_outputs_norm = tf.norm(intent_outputs, axis=-1)

# Define slot loss
with tf.variable_scope('slot_loss'):
    slots_reshape_onehot = tf.one_hot(slots_reshape, len(slot_vocab['vocab']) - 2)  # [16*18, 74]
    crossent = tf.nn.softmax_cross_entropy_with_logits_v2(labels=slots_reshape_onehot, logits=slot_outputs)