2017-06-06 7 views
0

TensorFlowを初めて使用し、複数のCSVファイルを入力として使用してSachin Joglekarのブログ(https://codesachin.wordpress.com/2015/11/28/self-organizing-maps-with-googles-tensorflow/)に基づいてSOMモデルをトレーニングしています。私はhttps://www.tensorflow.org/programmers_guide/reading_dataのチュートリアルに従い、ミニバッチでCSVファイルキューから読み込みました。私のコードは実行されていますが、リーダーからデコードされたCSV入力をプリントアウトして、入力パイプラインが正しく機能していることを確認します。 CSVファイルの入力はグラフの一部ではないので、Tensor.eval(self.sess)を使用して印刷することはできません。 self.label.eval(session = tf.Session(graph = self.label.graph))を使ってデコードされたレコードラベルを印刷しようとすると、スクリプトがハングアップし、出力が得られません。私の入力パイプラインが機能していることを確認する方法はありますか?ここに私のコードの関連するスニペットです:CSVファイルキュー用のTensorFlow入力パイプライン

主な機能

def main(argv): 
    som = SOM(somDim1, somDim2, windowSizes[win], iterations, learningRate, 
    neighborhood, fileNameList, batchSize) 
    som.train(batchSize, fileNameList, windowSizes[win]) 

グラフは

def __init__(self, m, n, dim, iterations, alpha, sigma, fileNameList, batchSize): 

    ##INITIALIZE GRAPH 
    self.graph = tf.Graph() 

    ##POPULATE GRAPH WITH NECESSARY COMPONENTS 
    with self.graph.as_default(): 

     ##PLACEHOLDERS FOR TRAINING INPUTS 
     #These should be placeholders according to the TensorFlow framework, 
     #but we are declaring them as variables so that we can assign them directly 
     #to values read in from the CSV files. 
     batchInputLg = np.zeros((dim, batchSize)) 
     labelFloat = np.zeros((3, batchSize)) 
     self.label = tf.cast(labelFloat, "string") 
     self.batchInput = tf.cast(batchInputLg, "float32") 

     """ 
     ...the rest of the graph... 
     """ 

     self.trainingOp = tf.assign(self.weightageVects, newWeightagesOp) 

     ##INITIALIZE SESSION 
     self.sess = tf.Session() 

     ##INITIALIZE VARIABLES 
     initOp = tf.global_variables_initializer() 
     self.sess.run(initOp) 

入力パイプライン機能

""" 
Read in the features and metadata from the CSV files for each chromosome. 
""" 
def readFromCsv(self, fileNameQ, dim): 
    reader = tf.TextLineReader() 
    _, csvLine = reader.read(fileNameQ) 
    recordDefaults = [["\0"] for cl in range(dim - 1)] 
    recordStr = tf.decode_csv(csvLine, record_defaults=recordDefaults) 
    self.label = tf.stack(recordStr[0:2]) 
    #self.label.eval(session = tf.Session(graph=self.label.graph)) 
    self.features = tf.to_float(tf.stack(recordStr[3:dim - 1])) 
    return (self.features, self.label) 

""" 
Read in the features and metadata from the CSV files for each chromosome. 
""" 
def inputPipeline(self, batchSize, fileNameList, dim, num_epochs=None): 
    fileNameQ = tf.train.string_input_producer(fileNameList, shuffle = True) 
    minAfterDequeue = 10000 
    capacity = minAfterDequeue + 3 * batchSize 
    example, label = self.readFromCsv(fileNameQ, dim) 
    exampleBatchStr, labelBatch = tf.train.shuffle_batch([example, label], batch_size=batchSize, capacity=capacity, min_after_dequeue=minAfterDequeue) 
    exampleBatch = tf.cast(exampleBatchStr, "float") 
    return (exampleBatch, labelBatch) 

トレーニング機能

def train(self, batchSize, fileNameList, dim): 
    #Start the queue runners. 
    # Start input enqueue threads. 
    coordFile = tf.train.Coordinator() 
    self.coord = tf.train.Coordinator() 
    threadsFile = tf.train.start_queue_runners(sess=self.sess, coord=coordFile) 
    self.threads = tf.train.start_queue_runners(sess=self.sess, coord=self.coord) 

    #Training iterations 
    self.iterationInput = 0 
    try: 
     for iter in range(self.iterations): 
      #Train with each vector one by one 
      self.iterationInput += 1 
      while not self.coord.should_stop(): 
       #Fill in input data. 
       [self.batchInput, self.label] = self.inputPipeline(batchSize, fileNameList, dim) 
       self.sess.run(self.trainingOp) 

    except tf.errors.OutOfRangeError: 
     print('Done training -- epoch limit reached') 

    # When done, ask the threads to stop. 
    self.coord.request_stop() 

答えて

1

解決策を見つけました。代わりに、グラフにラベルとバッチインプットテンソルを初期化し、電車()関数内でそれらを割り当てるので、私はそうのように、グラフ内の代入文を置いておく必要があります。

##TRAINING INPUTS 
self.batchInput, self.label = self.inputPipeline(batchSize, fileNameList, dim) 

その後、列車の関数は次のようになります。

def train(self, batchSize, fileNameList, dim): 
    with self.sess: 
     # Start populating the filename queue. 
     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord) 

     #Training iterations 
     self.iterationInput = 0 
     try: 
      for iter in range(self.iterations): 
       #Train with each vector one by one 
       self.iterationInput += 1 
       while not coord.should_stop(): 
        #Fill in input data. 
        self.sess.run([self.batchInput, self.label]) 
        self.sess.run(self.trainingOp) 
        print self.label.eval(session = self.sess) 
     except tf.errors.OutOfRangeError: 
      print('Done training -- epoch limit reached') 

     # When done, ask the threads to stop. 
     coord.request_stop() 
     coord.join(threads) 
関連する問題