2017-03-02 105 views
0

我有一個文本文件有110行和1024列的浮點值。我正在嘗試加載文本文件,但它沒有讀取任何內容。閱讀文本文件返回張量流中的空變量

filename = '300_faults.txt' 
filename_queue = tf.train.string_input_producer([filename]) 
reader = tf.TextLineReader() 
_,a = reader.read(filename_queue) 
#x = np.loadtxt('300_faults.txt') # working 
#a = tf.constant(x,tf.float32)  # working 

model = tf.initialize_all_variables() 
with tf.Session() as session: 
    session.run(model) 
    print(session.run(tf.shape(a))) 

打印變量的形狀返回[]

回答

0

首先 - tf.shape(a) == []並不意味着該變量是空的。所有的標量和字符串都有形狀[]

https://www.tensorflow.org/programmers_guide/dims_types

可能是你可以檢查「等級」,而不是 - 它會爲標量和字符串爲0。 除此之外,它看起來像string_input_producer是一個隊列,它需要額外的接線,以使ti工作。

請試試這個

filename = '300_faults.txt' 
filename_queue = tf.train.string_input_producer([filename]) 
reader = tf.TextLineReader() 
_,a = reader.read(filename_queue) 
#x = np.loadtxt('300_faults.txt') # working 
#a = tf.constant(x,tf.float32)  # working 

model = tf.initialize_all_variables() 
with tf.Session() as session: 
    session.run(model) 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    print(session.run(tf.shape(a))) 

    print(session.run((a))) 
    coord.request_stop() 
    coord.join(threads) 
+0

但是,當我打印可變我只能打印的第一行。如果我訪問變量(例如print(session.run(a [1,1]))),它顯示錯誤「Value Error:Index out of range using input dim 0; input'0'dims for'stride_slice' – Raady

+0

我認爲你需要解析這條線,我的意思是如果它是一個字符串,你不能像訪問數組那樣訪問它(就像你在香草python中那樣) https://www.tensorflow.org/ programmers_guide/reading_data 檢查「cvs文件」部分! – avloss