2016-09-16 10 views
14

टेन्सफोर्लो में, मैं एक बहुआयामी सरणी को एक TFRecord में सहेजना चाहता हूं। उदाहरण के लिए:tf.SequenceExample बहुआयामी सरणी के साथ

[[1, 2, 3], [1, 2], [3, 2, 1]] 

के रूप में कार्य मैं हल करने के लिए कोशिश कर रहा हूँ अनुक्रमिक है, मैं Tensorflow के tf.train.SequenceExample() उपयोग करने के लिए कोशिश कर रहा हूँ और मैं एक TFRecord फाइल करने के लिए डेटा लेखन करने में सफल रहा हूँ जब डेटा लेखन।

W tensorflow/core/framework/op_kernel.cc:936] Invalid argument: Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: [] 
E tensorflow/core/client/tensor_c_api.cc:485] Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: [] 

समारोह मैं लोड करने के लिए अपने डेटा के नीचे है की कोशिश करने का उपयोग कर रहा हूँ: हालांकि, जब मैं tf.parse_single_sequence_example का उपयोग कर TFRecord फ़ाइल से डेटा लोड करने का प्रयास, मैं गुप्त त्रुटियों की एक बड़ी संख्या के साथ स्वागत कर रहा हूँ

def read_and_decode_single_example(filename): 

    filename_queue = tf.train.string_input_producer([filename], 
               num_epochs=None) 

    reader = tf.TFRecordReader() 
    _, serialized_example = reader.read(filename_queue) 

    context_features = { 
     "length": tf.FixedLenFeature([], dtype=tf.int64) 
    } 

    sequence_features = { 
     "input_characters": tf.FixedLenSequenceFeature([],   dtype=tf.int64), 
     "output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64) 
    } 

    context_parsed, sequence_parsed = tf.parse_single_sequence_example(
    serialized=serialized_example, 
    context_features=context_features, 
    sequence_features=sequence_features 
) 

context = tf.contrib.learn.run_n(context_parsed, n=1, feed_dict=None) 
print context 

समारोह है कि मैं डेटा को बचाने के लिए उपयोग कर रहा हूँ यहाँ है:

# http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/ 
def make_example(input_sequence, output_sequence): 
    """ 
    Makes a single example from Python lists that follows the 
    format of tf.train.SequenceExample. 
    """ 

    example_sequence = tf.train.SequenceExample() 

    # 3D length 
    sequence_length = sum([len(word) for word in input_sequence]) 
    example_sequence.context.feature["length"].int64_list.value.append(sequence_length) 

    input_characters = example_sequence.feature_lists.feature_list["input_characters"] 
    output_characters = example_sequence.feature_lists.feature_list["output_characters"] 

    for input_character, output_character in izip_longest(input_sequence, 
                  output_sequence): 

     # Extend seems to work, therefore it replaces append. 
     if input_sequence is not None: 
      input_characters.feature.add().int64_list.value.extend(input_character) 

     if output_characters is not None: 
      output_characters.feature.add().int64_list.value.extend(output_character) 

    return example_sequence 

किसी भी मदद का स्वागत किया किया जाएगा।

+0

हाय के साथ एम्बेडिंग की सूची में इंडेक्स की एक सूची बदल सकते हैं, क्या आप अधिक संदर्भ प्रदान कर सकते हैं? सबसे अच्छा आप एक न्यूनतम उदाहरण प्रदान करते हैं जिसे वास्तव में चलाया जा सकता है और परीक्षण किया जा सकता है, जिसमें आप अपना डेटा किसी फ़ाइल में कैसे सहेजते हैं। – jlarsch

+0

आपका उदाहरण अनुसरण करना बहुत कठिन है, और यदि आप प्रासंगिक संदर्भ को शामिल करने के लिए अपना उदाहरण संपादित करते हैं तो आपको और सहायता मिलेगी। उदाहरण के लिए - कोड में टिप्पणी में डाले गए लिंक को देखते हुए, यह स्पष्ट हो जाता है कि स्निपेट जहां आप अनुक्रम उदाहरण उत्पन्न करते हैं, उस कोड में शामिल नहीं है जो वास्तव में डेटा लिखता है। –

उत्तर

5

प्रदान किए गए कोड के साथ मैं आपकी त्रुटि को पुन: उत्पन्न करने में सक्षम नहीं था लेकिन कुछ शिक्षित अनुमानों को निम्नलिखित कार्य कोड दिया।

import tensorflow as tf 
import numpy as np 
import tempfile 

tmp_filename = 'tf.tmp' 

sequences = [[1, 2, 3], [1, 2], [3, 2, 1]] 
label_sequences = [[0, 1, 0], [1, 0], [1, 1, 1]] 

def make_example(input_sequence, output_sequence): 
    """ 
    Makes a single example from Python lists that follows the 
    format of tf.train.SequenceExample. 
    """ 

    example_sequence = tf.train.SequenceExample() 

    # 3D length 
    sequence_length = len(input_sequence) 

    example_sequence.context.feature["length"].int64_list.value.append(sequence_length) 

    input_characters = example_sequence.feature_lists.feature_list["input_characters"] 
    output_characters = example_sequence.feature_lists.feature_list["output_characters"] 

    for input_character, output_character in zip(input_sequence, 
                  output_sequence): 

     if input_sequence is not None: 
      input_characters.feature.add().int64_list.value.append(input_character) 

     if output_characters is not None: 
      output_characters.feature.add().int64_list.value.append(output_character) 

    return example_sequence 

# Write all examples into a TFRecords file 
def save_tf(filename): 
    with open(filename, 'w') as fp: 
     writer = tf.python_io.TFRecordWriter(fp.name) 
     for sequence, label_sequence in zip(sequences, label_sequences): 
      ex = make_example(sequence, label_sequence) 
      writer.write(ex.SerializeToString()) 
     writer.close() 

def read_and_decode_single_example(filename): 

    filename_queue = tf.train.string_input_producer([filename], 
               num_epochs=None) 

    reader = tf.TFRecordReader() 
    _, serialized_example = reader.read(filename_queue) 

    context_features = { 
     "length": tf.FixedLenFeature([], dtype=tf.int64) 
    } 

    sequence_features = { 
     "input_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64), 
     "output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64) 
    } 


    return serialized_example, context_features, sequence_features 

save_tf(tmp_filename) 
ex,context_features,sequence_features = read_and_decode_single_example(tmp_filename) 
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
    serialized=ex, 
    context_features=context_features, 
    sequence_features=sequence_features 
) 

sequence = tf.contrib.learn.run_n(sequence_parsed, n=1, feed_dict=None) 
#check if the saved data matches the input data 
print(sequences[0] in sequence[0]['input_characters']) 

आवश्यक परिवर्तन किए गए:

  1. sequence_length = sum([len(word) for word in input_sequence]) यह आपके उदाहरण डेटा के लिए काम नहीं करता है sequence_length = len(input_sequence) करने के लिए

अन्यथा

  1. extend को append
+0

इन परिवर्तनों को आजमाते समय मुझे त्रुटि मिलती है: 'TypeError: [37] में टाइप किया गया है, लेकिन इनमें से एक की उम्मीद है: (, ) '। – Torkoal

+0

मुझे लगता है कि मुझे समस्या दिखाई देती है, '[[1, 2, 3], [1, 2], [3, 2, 1]]' का अर्थ एक अनुक्रम नहीं है। – Torkoal

+0

क्या आपने उत्तर में स्निपेट का प्रयास किया था? इसे चलाने के दौरान मुझे कोई त्रुटि नहीं मिली (उबंटू, पायथन 3.4, जीपीयू के बिना टीएफ)। क्या आपका इनपुट डेटा बिल्कुल प्रश्न में दिखता है? –

3

मुझे एक ही समस्या थी। मुझे लगता है कि यह पूरी तरह हल करने योग्य है, लेकिन आपको आउटपुट प्रारूप पर फैसला करना होगा, और उसके बाद यह पता लगाना होगा कि आप इसका उपयोग कैसे करेंगे।

पहलेआपकी त्रुटि क्या है?

त्रुटि संदेश आपको बता रहा है कि आप जो पढ़ने की कोशिश कर रहे हैं वह आपके द्वारा निर्दिष्ट फीचर आकार में फिट नहीं है। तो आपने इसे कहां निर्दिष्ट किया? ठीक है यहाँ:

sequence_features = { 
    "input_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64), 
    "output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64) 
} 

यह कहते हैं, "मेरी input_characters एकल मूल्यों का एक क्रम है", लेकिन यह सच नहीं है, आपके पास एक ही मूल्य के अनुक्रमों का अनुक्रम है और इसलिए एक त्रुटि है।

दूसराआप क्या कर सकते हैं?

आप के बजाय का उपयोग करते हैं:

a = [[1,2,3], [2,3,1], [3,2,1]] 
sequence_features = { 
    "input_characters": tf.FixedLenSequenceFeature([3], dtype=tf.int64), 
    "output_characters": tf.FixedLenSequenceFeature([3], dtype=tf.int64) 
} 

आप क्योंकि आप निर्दिष्ट किया है कि शीर्ष स्तर अनुक्रम के प्रत्येक तत्व 3 तत्वों बशर्ते कि आपके कोड के साथ एक त्रुटि नहीं होगा।

वैकल्पिक रूप से, यदि आपके पास निश्चित लंबाई अनुक्रम नहीं हैं, तो आपको एक अलग प्रकार की सुविधा का उपयोग करना होगा।

sequence_features = { 
    "input_characters": tf.VarLenFeature(tf.int64), 
    "output_characters": tf.VarLenFeature(tf.int64) 
} 

VarLenFeature यह बताता है कि पढ़ने से पहले लंबाई अज्ञात है। दुर्भाग्य से इसका मतलब है कि आपके इनपुट_क्रैक्टर्स को अब एक चरण में घने वेक्टर के रूप में नहीं पढ़ा जा सकता है। इसके बजाए, यह डिफ़ॉल्ट रूप से SparseTensor होगा। आप tf.sparse_tensor_to_dense जैसे के साथ एक घने टेन्सर में इस बारी कर सकते हैं:

input_densified = tf.sparse_tensor_to_dense(sequence_parsed['input_characters']) 

the article कि आप देख किया गया है में उल्लेख किया है, यदि आपके डेटा हमेशा एक ही लंबाई नहीं है आप एक "not_really_a_word" होनी चाहिए आपके शब्दावली में शब्द, जिसे आप डिफ़ॉल्ट अनुक्रमणिका के रूप में उपयोग करते हैं। जैसे मान लीजिए कि आप है "not_really_a_word" शब्द के लिए सूचकांक 0 मानचित्रण करते हैं, तो आपके

a = [[1,2,3], [2,3], [3,2,1]] 

अजगर सूची का उपयोग कर एक

array((1,2,3), (2,3,0), (3,2,1)) 

टेन्सर जा रहा है खत्म हो जाएगा।

चेतावनी दी जानी चाहिए; मुझे यकीन नहीं है कि स्पैरसेन्सर्स के लिए बैक-प्रोपेगेशन "बस काम करता है", जैसे कि यह घने टेंसर के लिए करता है। wildml article "अनुक्रमांक_ए_वर्ड" शब्द के लिए हानि को मास्क करने के प्रति अनुक्रम 0s प्रति पैडिंग के बारे में वार्ता करता है (देखें: "साइड नोट: अपने लेख में अपने शब्दावली/वर्गों में 0 के साथ सावधान रहें)। ऐसा लगता है कि पहली विधि को कार्यान्वित करना आसान होगा।

ध्यान दें कि यह यहां वर्णित मामले से अलग है जहां प्रत्येक उदाहरण अनुक्रमों का अनुक्रम है। मेरी समझ के लिए, इस तरह की विधि का समर्थन अच्छी तरह से समर्थित नहीं है क्योंकि यह इस मामले का दुरुपयोग है कि इसका समर्थन करने के लिए है; निश्चित आकार एम्बेडिंग सीधे लोड हो रहा है।


मुझे लगता है कि आप जो भी अगली चीज करना चाहते हैं वह उन नंबरों को शब्द एम्बेडिंग में बदलना है। आप tf.nn.embedding_lookup

संबंधित मुद्दे