2017-09-10 20 views
6

Tôi đang cố gắng đọc một số dữ liệu trong chuỗi lưu lượng và sau đó kết hợp dữ liệu đó với các nhãn của nó. Thiết lập của tôi là như sau:Tại sao các ví dụ và nhãn của tôi theo thứ tự sai?

  • Tôi có một mảng của các chữ cái tiếng Anh, "a", "b", "c", "d", "e", ...
  • Tôi có một mảng của các chữ cái "Cyrillic", "a", "b", "w, "g", "d", ...,
  • tôi có một loạt các con số, 0, 1, 2, 3, 4, ...

Tôi muốn tạo một chuỗi các ví dụ chứa các cặp giữa hai mảng đầu tiên, như ["b", "b"], ["d", "g"], ["c", "w"], .... Tôi cũng muốn một hàng đợi các số tương ứng cho các cặp đó, trong trường hợp này là 1, 3, 2, ...

Tuy nhiên, khi tôi tạo các hàng đợi này, ví dụ và số của tôi không khớp - ví dụ, hàng đợi ["b", "b"], ["d", "g"], ["c", "w"], ... đi kèm với một hàng đợi nhãn là 5, 0, 2, ....

Điều gì có thể gây ra điều này? Để thử nghiệm, tôi đã vô hiệu hóa tất cả sự xáo trộn trong việc tạo hàng đợi/hàng loạt, nhưng vấn đề vẫn tồn tại.



Đây là mã của tôi:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import tensorflow as tf 

from constants import FLAGS 


letters_data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] 
cyrillic_letters_data = ["a", "b", "w", "g", "d", "e", "j", "v", "z", "i"] 
numbers_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 




def inputs(batch_size): 
    # Get the letters and the labels 
    (letters, labels) = _inputs(batch_size=batch_size) 

    # Return the letters and the labels. 
    return letters, labels 


def read_letter(pairs_and_overlap_queue): 
    # Read the letters, cyrillics, and numbers. 
    letter = pairs_and_overlap_queue[0] 
    cyrillic = pairs_and_overlap_queue[1] 
    number = pairs_and_overlap_queue[2] 

    # Do something with them 
    # (doesn't matter what) 
    letter = tf.substr(letter, 0, 1) 
    cyrillic = tf.substr(cyrillic, 0, 1) 
    number = tf.add(number, tf.constant(0)) 

    # Return them 
    return letter, cyrillic, number 


def _inputs(batch_size): 
    # Get the input data 
    letters = letters_data 
    cyrillics = cyrillic_letters_data 
    numbers = numbers_data 


    # Create a queue containing the letters, 
    # the cyrillics, and the numbers 
    pairs_and_overlap_queue = tf.train.slice_input_producer([letters, cyrillics, numbers], 
                  capacity=100000, 
                  shuffle=False) 

    # Perform some operations on each of those 
    letter, cyrillic, number = read_letter(pairs_and_overlap_queue) 

    # Combine the letters and cyrillics into one example 
    combined_example = tf.stack([letter, cyrillic]) 


    # Ensure that the random shuffling has good mixing properties. 
    min_fraction_of_examples_in_queue = 0.4 
    min_queue_examples = int(FLAGS.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN * 
          min_fraction_of_examples_in_queue) 

    # Generate an example and label batch, and return it. 
    return _generate_image_and_label_batch(example=combined_example, label=number, 
              min_queue_examples=min_queue_examples, 
              batch_size=batch_size, 
              shuffle=False) 


def _generate_image_and_label_batch(example, label, min_queue_examples, 
            batch_size, shuffle): 

    # Create a queue that shuffles the examples, and then 
    # read 'batch_size' examples + labels from the example queue. 
    num_preprocess_threads = FLAGS.NUM_THREADS 
    if shuffle: 
     examples, label_batch = tf.train.shuffle_batch(
      [example, label], 
      batch_size=batch_size, 
      num_threads=num_preprocess_threads, 
      capacity=min_queue_examples + 6 * batch_size, 
      min_after_dequeue=min_queue_examples) 
    else: 
     print("Not shuffling!") 
     examples, label_batch = tf.train.batch(
      [example, label], 
      batch_size=batch_size, 
      num_threads=num_preprocess_threads, 
      capacity=min_queue_examples + 6 * batch_size) 

    # Return the examples and the labels batches. 
    return examples, tf.reshape(label_batch, [batch_size]) 



lcs, nums = inputs(batch_size=3) 



with tf.Session() as sess: 

    # Start populating the filename queue. 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord, sess=sess) 


    for i in xrange(0, 5): 
     my_lcs = lcs.eval() 
     my_nums = nums.eval() 

     print(str(my_lcs) + " --> " + str(my_nums)) 

Cảm ơn rất nhiều sự giúp đỡ của bạn!

Trả lời

4

Khi bạn chạy tv.eval() hai lần, bạn thực sự chạy biểu đồ hai lần, vì vậy bạn đang trộn lcs và num từ hai lô khác nhau, nếu bạn thay đổi vòng lặp của bạn thành sau, bạn sẽ kéo cả hai tensors trong cùng một lần chạy đồ thị:

my_lcs, my_nums = sess.run([lcs, nums]) 

    print(str(my_lcs) + " --> " + str(my_nums)) 

Điều này cho phép ở bên cạnh tôi:

[[b'g' b'j'] 
[b'h' b'v'] 
[b'i' b'z']] --> [6 7 8] 
[[b'f' b'e'] 
[b'g' b'j'] 
[b'h' b'v']] --> [5 6 7] 
[[b'e' b'd'] 
[b'f' b'e'] 
[b'g' b'j']] --> [4 5 6] 
[[b'd' b'g'] 
[b'e' b'd'] 
[b'f' b'e']] --> [3 4 5] 
[[b'c' b'w'] 
[b'd' b'g'] 
[b'e' b'd']] --> [2 3 4] 

Xem thêm các bài sau: Does Tensorflow rerun for each eval() call?

+0

Ugh, cảm ơn rất nhiều. Tôi thực sự nên đã có thể tìm thấy điều này, nhưng tôi đã chắc chắn lỗi là trong thế hệ hàng loạt của tôi. Cảm ơn bạn! – 416E64726577

+1

điều tra tốt đẹp! –

Các vấn đề liên quan