コード例 #1
0
def engine(query=" ", senderid='', senderName='', x_test=x_test):

    query = query
    new_question = query.strip()
    new_question = data_helpers.clean_str(new_question)
    new_question = new_question.split(" ")

    num_padd = x_test.shape[1] - len(new_question)
    new_question = new_question + ["<PAD/>"] * num_padd
    #print new_question
    for word in new_question:
        if not vocabulary.has_key(word):
            new_question[new_question.index(word)] = "<PAD/>"

    x = np.array([vocabulary[word] for word in new_question])
    x_test = np.array([x])

    #print("\nEvaluating...\n")

    # Generate batches for one epoch
    batches = data_helpers.batch_iter(x_test, batch_size, 1, shuffle=False)

    # Collect the predictions here
    all_predictions = []

    for x_test_batch in batches:
        batch_predictions = sess.run(predictions, {
            input_x: x_test_batch,
            dropout_keep_prob: 1.0
        })
        all_predictions = np.concatenate([all_predictions, batch_predictions])

    # Print accuracy

    #print 'prediction---', int(all_predictions)
    if int(all_predictions[0]) == 1:
        print 'Positive'
        return {
            'Name':
            senderName,
            'Sentiment':
            'Positive',
            'Response':
            'Thank you for your valuable feedback! \n \nIt will help us to serve better in future.\n'
        }
    elif int(all_predictions[0]) == 0:
        print 'Negative'
        return {
            'Name':
            senderName,
            'Sentiment':
            'Negative',
            'Response':
            'We are really sorry for the bad experience with our product.\n   '
        }
    else:
        print 'Unidentified'
        return {'Name': senderName, 'Sentiment': 'Unidentified'}
コード例 #2
0
def do_predict(x_raw):

    # Map data into vocabulary
    x_raw2 = list()
    x_raw2.append(x_raw)

    vocab_path2 = os.path.join(FLAGS.checkpoint_dir3, "..", "vocab")
    vocab_processor2 = learn.preprocessing.VocabularyProcessor.restore(
        vocab_path2)
    x_test2 = np.array(list(vocab_processor2.transform(x_raw2)))

    graph2 = tf.Graph()
    with graph2.as_default():
        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement3,
            log_device_placement=FLAGS.log_device_placement3)
        sess2 = tf.Session(config=tf.ConfigProto(log_device_placement=True))
        with sess2.as_default():
            # Load the saved meta graph and restore variables
            saver2 = tf.train.import_meta_graph("model-345.meta")
            #saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
            saver2.restore(sess2, "model-345")

            # Get the placeholders from the graph by name
            input_x2 = graph2.get_operation_by_name("input_x").outputs[0]
            #    print("input_x : ",input_x2)
            #input_y = graph.get_operation_by_name("input_y").outputs[0]
            #print("input_y done")
            dropout_keep_prob = graph2.get_operation_by_name(
                "dropout_keep_prob").outputs[0]
            #   print("dropout_done")

            # Tensors we want to evaluate
            predictions2 = graph2.get_operation_by_name(
                "output/predictions").outputs[0]
            #    print("prediction_done",predictions2)
            # Generate batches for one epoch
            batches2 = data_helpers2.batch_iter(list(x_test2),
                                                FLAGS.batch_size3,
                                                1,
                                                shuffle=False)
            #   print("batches_done")
            # Collect the predictions here
            all_predictions2 = []

            for x_test_batch in batches2:
                batch_predictions2 = sess2.run(predictions2, {
                    input_x2: x_test_batch,
                    dropout_keep_prob: 1.0
                })
                print(x_test_batch, batch_predictions2)
                all_predictions2 = np.concatenate(
                    [all_predictions2, batch_predictions2])
#

        return all_predictions2
コード例 #3
0
ファイル: train2.py プロジェクト: mevanoff24/MachineLearning
        def run():

            # Generate batches
            batches = data_helpers2.batch_iter(zip(x_train, y_train), batch_size, num_epochs)
            # Training loop. 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=None)
                    print("")
コード例 #4
0
                cnn.input_y: y_batch,
                cnn.dropout_keep_prob: 1.0
            }
            step, summaries, loss, accuracy = sess.run(
                [cnn.global_step, dev_summary_op, cnn.loss, cnn.accuracy],
                feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print("{}: step {}, loss {:g}, acc {:g}".format(
                time_str, step, loss, accuracy))
            if writer:
                writer.add_summary(summaries, step)

        # just for epoch counting
        num_batches_per_epoch = int(len(x_train) / FLAGS.batch_size) + 1
        # Generate batches
        batches = data_helpers.batch_iter(list(zip(x_train, y_train)),
                                          FLAGS.batch_size, FLAGS.num_epochs)
        # Training loop. 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, cnn.global_step)
            if current_step % FLAGS.evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(x_dev, y_dev, writer=dev_summary_writer)
                print("Epoch: {}".format(
                    int(current_step / num_batches_per_epoch)))
                print("")
            if current_step % FLAGS.checkpoint_every == 0:
                path = saver.save(sess,
                                  checkpoint_prefix,
                                  global_step=current_step)
コード例 #5
0
ファイル: train.py プロジェクト: 57uff3r/tensorflow-workshop
            feed_dict = {cnn.input_x: x_batch,
                         cnn.input_y: y_batch,
                         cnn.dropout_keep_prob: 1.0}
            step, summaries, loss, accuracy = sess.run(
                [cnn.global_step, dev_summary_op, cnn.loss, cnn.accuracy],
                feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print("{}: step {}, loss {:g}, acc {:g}".format(
                time_str, step, loss, accuracy))
            if writer:
                writer.add_summary(summaries, step)

        # just for epoch counting
        num_batches_per_epoch = int(len(x_train)/FLAGS.batch_size) + 1
        # Generate batches
        batches = data_helpers.batch_iter(
            list(zip(x_train, y_train)), FLAGS.batch_size, FLAGS.num_epochs)
        # Training loop. 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, cnn.global_step)
            if current_step % FLAGS.evaluate_every == 0:
                print("\nEvaluation:")
                dev_step(x_dev, y_dev, writer=dev_summary_writer)
                print("Epoch: {}".format(
                    int(current_step / num_batches_per_epoch)))
                print("")
            if current_step % FLAGS.checkpoint_every == 0:
                path = saver.save(
                    sess, checkpoint_prefix, global_step=current_step)
                print("Saved model checkpoint to {}\n".format(path))
コード例 #6
0
            }
            step, loss, accuracy, precision, recall, prediction = sess.run([
                global_step, cnn.loss, cnn.accuracy, cnn.precision, cnn.recall,
                cnn.predictions
            ], feed_dict)
            f1 = 2.0 * precision * recall / (precision + recall)
            time_str = datetime.datetime.now().isoformat()
            print(
                "comparison {}:  precision {:g}, recall {:g}, F1 {:g}".format(
                    time_str, precision, recall, f1))

            return precision, recall, f1, prediction

        # Generate batches
        batches = data_helpers2.batch_iter(
            list(
                zip(x_train_arg1_shuffled, x_train_arg2_shuffled,
                    y_train_shuffled)), FLAGS.batch_size, FLAGS.num_epochs)

        max_f1 = 0.0
        for batch in batches:
            x_arg1_batch, x_arg2_batch, y_batch = zip(*batch)
            train_step(x_arg1_batch, x_arg2_batch, y_batch)
            current_step = tf.train.global_step(sess, global_step)
            #print(x_arg1_batch)
            if current_step % FLAGS.evaluate_every == 0:
                p, r, f1, prediction = test_step(x_test_arg1, x_test_arg2,
                                                 y_test)

                if f1 > max_f1:
                    out_path = outpath + "/" + FLAGS.label + '.txt'
                    f = open(out_path, 'w')
コード例 #7
0
ファイル: eval.py プロジェクト: 57uff3r/tensorflow-workshop
    with sess.as_default():
        # Load the saved meta graph and restore variables
        saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
        saver.restore(sess, checkpoint_file)

        # Get the placeholders from the graph by name
        input_x = graph.get_operation_by_name("input_x").outputs[0]
        dropout_keep_prob = graph.get_operation_by_name(
            "dropout_keep_prob").outputs[0]

        # Tensors we want to evaluate
        predictions = graph.get_operation_by_name(
            "output/predictions").outputs[0]

        # Generate batches for one epoch
        batches = data_helpers.batch_iter(
            x_test, FLAGS.batch_size, 1, shuffle=False)

        # Collect the predictions here
        all_predictions = []

        for x_test_batch in batches:
            batch_predictions = sess.run(
                predictions, {input_x: x_test_batch, dropout_keep_prob: 1.0})
            all_predictions = np.concatenate(
                [all_predictions, batch_predictions])

# Print accuracy
correct_predictions = float(sum(all_predictions == y_test))
print("Total number of test examples: {}".format(len(y_test)))
print("Accuracy: {:g}".format(correct_predictions/float(len(y_test))))
コード例 #8
0
ファイル: eval.py プロジェクト: feuerchop/source.ml
        # Load the saved meta graph and restore variables
        saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
        saver.restore(sess, checkpoint_file)

        # Get the placeholders from the graph by name
        input_x = graph.get_operation_by_name("input_x").outputs[0]
        dropout_keep_prob = graph.get_operation_by_name(
            "dropout_keep_prob").outputs[0]

        # Tensors we want to evaluate
        predictions = graph.get_operation_by_name(
            "output/predictions").outputs[0]

        # Generate batches for one epoch
        batches = data_helpers.batch_iter(x_test,
                                          FLAGS.batch_size,
                                          1,
                                          shuffle=False)

        # Collect the predictions here
        all_predictions = []

        for x_test_batch in batches:
            batch_predictions = sess.run(predictions, {
                input_x: x_test_batch,
                dropout_keep_prob: 1.0
            })
            all_predictions = np.concatenate(
                [all_predictions, batch_predictions])

# Print accuracy
correct_predictions = float(sum(all_predictions == y_test))