示例#1
0
def handle_learning_server_request(req):
    global h_to_a_model
    from learning_ros_server.srv import LearningServerResponse
    prob_config = config.get_problem_config(rnn_model.PROBLEM_NAME)
    my_seq_length = prob_config['max_sequence_length'] + 1
    data_generator = rnn_model.DataGenerator(1, req.input, my_seq_length)
    x, y = data_generator.next_batch()
    ans_index = len(data_generator.seqs[0]) - 1
    probs, outputs, new_rnn_state, image_summary = h_to_a_model.predict_and_return_state(
        x, None, summary=False
    )  #probs = batch size*seq length * output length #output = seqlength*batch size* hiddenunits
    #summary_writer.add_summary(image_summary)
    probs_without_dummy_actions = [i[:-2] for i in probs[0]]
    prediction = np.argmax([probs_without_dummy_actions], axis=2)
    prediction_outputs = []
    ans = LearningServerResponse()
    ans.last_activation = []
    for i in xrange(len(outputs)):
        if (not rnn_model.is_stump(x[0][i])) and (not rnn_model.is_pad(
                x[0][i])):  #not stump nd pad
            prediction_outputs.append(outputs[i][0])
    if len(prediction_outputs) > 0:
        ans.last_activation = prediction_outputs[-1]

    ans.prediction = prediction[0]
    ans.ans_index = ans_index
    print data_generator.yseqs

    return ans
示例#2
0
def train(model_name, output_dir, model_input):

    data_generator = rnn_model.DataGenerator(1, model_input)
    num_val_batches = data_generator.num_batches
    print len(data_generator.xseqs)
    print data_generator.seq_length
    print 'data number of batches', data_generator.num_batches
    #num_val_batches = 1
    seq_length = data_generator.seq_length
    #generate training data for two svms
    with tf.Session(config=config.get_tf_config()) as sess:
        h_to_a_model = load_model(model_name, sess, seq_length)
        #summary_writer = tf.summary.FileWriter('output', sess.graph, seq_length)

        correct_prediction_outputs = []
        wrong_prediction_outputs = []
        for _ in xrange(num_val_batches):
            x, y = data_generator.next_batch(
            )  # x/y = batch size*seq length*input_length/output length
            target = np.argmax(y, axis=2)  #target  = batch size*seq length *1
            probs, outputs, rnn_state, image_summary = h_to_a_model.predict_and_return_state(
                x, None,
                summary=False)  #output = seqlength*batch size* hiddenunits
            #summary_writer.add_summary(image_summary)
            prediction = np.argmax(
                probs, axis=2)  # prediction = batch size*seq length * 1
            #print data_generator.xseqs
            #print y
            #print target[0]
            #print prediction[0]
            #print outputs[0:2]
            correct_prediction = target == prediction  #batch size *seq length * 1
            for i in xrange(len(outputs)):
                if (not rnn_model.is_stump(x[0][i])) and (not rnn_model.is_pad(
                        x[0][i])):  #not stump nd pad
                    if correct_prediction[0][i]:
                        correct_prediction_outputs.append(outputs[i][0])
                    else:
                        wrong_prediction_outputs.append(outputs[i][0])

        print 'num correct prediction traces', len(correct_prediction_outputs)
        print 'num wrong prediction traces', len(wrong_prediction_outputs)
        correct_prediction_svm = compute_svm(correct_prediction_outputs,
                                             output_dir, 'correct_prediction')
        #y_correct_predict = correct_prediction_svm.predict(correct_prediction_outputs)
        wrong_prediction_svm = compute_svm(wrong_prediction_outputs,
                                           output_dir, 'wrong_prediction')
示例#3
0
def test(model_name, svm_model_prefix, model_input, action='test'):
    #generate training data for two svms
    data_generator = rnn_model.DataGenerator(1, model_input)
    num_val_batches = data_generator.num_batches
    if action == 'testBatch':
        print len(data_generator.xseqs)
        print data_generator.seq_length
        print 'data number of batches', data_generator.num_batches
    #num_val_batches = 1
    seq_length = data_generator.seq_length
    with tf.Session(config=config.get_tf_config()) as sess:
        h_to_a_model = load_model(model_name, sess, seq_length)
        #summary_writer = tf.summary.FileWriter('output', sess.graph)

        prediction_outputs = []
        num_seen_predictions_correct_svm = 0
        num_unseen_prediction_correct_svm = 0
        num_seen_predictions_wrong_svm = 0
        num_unseen_prediction_wrong_svm = 0

        for _ in xrange(num_val_batches):
            x, y = data_generator.next_batch(
            )  # x/y = batch size*seq length*input_length/output length
            #target = np.argmax(y, axis=2) #target  = batch size*seq length *1
            probs, outputs, image_summary = h_to_a_model.predict_and_return_state(
                x, summary=False)  #output = seqlength*batch size* hiddenunits
            #summary_writer.add_summary(image_summary)
            prediction = np.argmax(
                probs, axis=2)  # prediction = batch size*seq length * 1
            #print data_generator.xseqs
            #print y
            #print target[0]
            #print prediction[0]
            #print outputs[0:2]

            for i in xrange(len(outputs)):
                if (not rnn_model.is_stump(x[0][i])) and (not rnn_model.is_pad(
                        x[0][i])):  #not stump nd pad
                    prediction_outputs.append(outputs[i][0])

        if len(prediction_outputs) == 0:  #Initial stump
            print 1
            print -1
        else:
            correct_prediction_svm = joblib.load(svm_model_prefix +
                                                 'correct_prediction_svm.pkl')
            wrong_prediction_svm = joblib.load(svm_model_prefix +
                                               'wrong_prediction_svm.pkl')
            if action == 'test':
                y_correct_predict = correct_prediction_svm.predict(
                    [prediction_outputs[-1]])
                y_wrong_predict = wrong_prediction_svm.predict(
                    [prediction_outputs[-1]])

                print y_correct_predict[-1]
                print y_wrong_predict[-1]
                #if(y_correct_predict[-1] == 1) and (y_wrong_predict[-1] == -1):
                #    print 1
                #else:
                #    print 0
                print y_correct_predict
                print y_wrong_predict
                #print data_generator.xseqs
            if action == 'testBatch':
                y_correct_predict = correct_prediction_svm.predict(
                    prediction_outputs)
                y_wrong_predict = wrong_prediction_svm.predict(
                    prediction_outputs)
                num_seen_predictions_correct_svm = num_seen_predictions_correct_svm + sum(
                    xx for xx in y_correct_predict if xx == 1)
                num_unseen_prediction_correct_svm = num_unseen_prediction_correct_svm + sum(
                    xx for xx in y_correct_predict if xx == -1)
                num_seen_predictions_wrong_svm = num_seen_predictions_wrong_svm + sum(
                    xx for xx in y_wrong_predict if xx == 1)
                num_unseen_prediction_wrong_svm = num_unseen_prediction_wrong_svm + sum(
                    xx for xx in y_wrong_predict if xx == -1)
        if action == 'test':
            print data_generator.yseqs
            print prediction[0]
        if action == 'testBatch':
            print "Num seen predictions (correct svm, wrong svm):" + repr(
                num_seen_predictions_correct_svm) + "," + repr(
                    num_seen_predictions_wrong_svm)
            print "Num unseen predictions (corect svm, wrong svm):" + repr(
                num_unseen_prediction_correct_svm) + "," + repr(
                    num_unseen_prediction_wrong_svm)
示例#4
0
def debug_with_pca_train(model_name,
                         model_input,
                         svm_model_prefix,
                         model_input_test=None):
    import matplotlib.pyplot as plt
    (probs, outputs, new_rnn_state, target,
     x) = get_learning_model_output(model_name, model_input, None, -1)

    prediction = np.argmax(probs, axis=2)
    correct_prediction = target == prediction
    batch_size = len(probs)
    correct_prediction_outputs = []
    wrong_prediction_outputs = []
    for j in xrange(batch_size):
        for i in xrange(len(outputs)):
            if (not rnn_model.is_stump(x[j][i])) and (not rnn_model.is_pad(
                    x[j][i])):  #not stump nd pad
                if correct_prediction[j][i]:
                    correct_prediction_outputs.append(outputs[i][j])
                else:
                    wrong_prediction_outputs.append(outputs[i][j])

    #print len(correct_prediction_outputs + wrong_prediction_outputs)

    (y_correct_predict, y_wrong_predict) = get_svm_model_output(
        svm_model_prefix,
        correct_prediction_outputs + wrong_prediction_outputs)
    from sklearn.decomposition import PCA
    pca = PCA(n_components=2)
    pca.fit(correct_prediction_outputs + wrong_prediction_outputs)
    joblib.dump(pca, "toy_pca_model.pkl")

    print pca.explained_variance_ratio_
    #print pca.explained_variance_
    print len(pca.components_)
    transformed_correct_prediction_outputs = pca.transform(
        correct_prediction_outputs)
    transformed_wrong_prediction_outputs = pca.transform(
        wrong_prediction_outputs)
    scatter_x = []
    scatter_y = []
    scatter_colors = []
    scatter_markers = []
    csv_file = open('toy_outputs.csv', 'w')
    csv_file.write("x,y,type,correct_predict,wrong_predict \n")

    for i in range(0,
                   len(correct_prediction_outputs + wrong_prediction_outputs)):
        data_type = 'correct'
        if i < len(correct_prediction_outputs):
            scatter_x.append(transformed_correct_prediction_outputs[i][0])
            scatter_y.append(transformed_correct_prediction_outputs[i][1])
            scatter_colors.append('green')
        else:
            data_type = 'wrong'
            scatter_x.append(transformed_wrong_prediction_outputs[
                i - len(transformed_correct_prediction_outputs)][0])
            scatter_y.append(transformed_wrong_prediction_outputs[
                i - len(transformed_correct_prediction_outputs)][1])
            scatter_colors.append('red')

        csv_file.write(repr(scatter_x[-1]) + "," + repr(scatter_y[-1]) + ",")
        csv_file.write(data_type + "," + repr(y_correct_predict[i]) + "," +
                       repr(y_wrong_predict[i]) + "\n")

        if (y_correct_predict[i] * y_wrong_predict[i] == 1):
            if (y_correct_predict[i] == 1):
                scatter_markers.append('^')
            else:
                scatter_markers.append('v')
        else:
            if (y_correct_predict[i] == 1):
                scatter_markers.append('+')
            else:
                scatter_markers.append('o')

    csv_file.close()
    area = np.pi * (15 * 1)**2
    m = np.array(scatter_markers)

    unique_markers = set(m)  # or yo can use: np.unique(m)

    for um in unique_markers:
        mask = m == um
        # mask is now an array of booleans that van be used for indexing
        plt.scatter(np.array(scatter_x)[mask],
                    np.array(scatter_y)[mask],
                    marker=um,
                    c=np.array(scatter_colors)[mask],
                    s=area)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Data')
    plt.show()