Я нахожусь в самом начале изучения TensorFlow. Я хочу прочитать матрицу 3x3 из файла csv в hdfs и умножить ее на себя.Прочитайте одну (упакованную) матрицу из файла в TensorFlow
файл выглядит следующим образом:
1,2,3
4,5,6
7,8,9
До сих пор я мог придумал следующий код с помощью TensorFlow tutorial:
def read_and_decode(filename_queue):
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Type information and column names based on the decoded CSV.
record_defaults = [[0.0], [0.0], [0.0]]
f1,f2,f3 = tf.decode_csv(value, record_defaults=record_defaults)
# Turn the features back into a tensor.
features = tf.pack([
f1,
f2,
f3])
return features
def input_pipeline(filename_queue, batch_size, num_threads):
example = read_and_decode(filename_queue)
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
example_batch = tf.train.batch(
[example], batch_size=batch_size, capacity=capacity,
num_threads=num_threads, allow_smaller_final_batch=True)
return example_batch
def get_all_records(FILE):
with tf.Session() as sess:
filename_queue = tf.train.string_input_producer([FILE], num_epochs=1, shuffle=False)
batch_size = 1
num_threads = 4
#batch = input_pipeline(filename_queue, batch_size, num_threads)
batch = read_and_decode(filename_queue)
init_op = tf.local_variables_initializer()
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
while True:
example = sess.run([batch])
print(example)
except tf.errors.OutOfRangeError, e:
coord.request_stop(e)
finally:
coord.request_stop()
coord.join(threads)
get_all_records('hdfs://default/test.csv')
Это будет печатать каждую строку матрицы в правильный порядок. Однако, когда я использую пакетную обработку, применяя input_pipeline(), результат не будет в правильном порядке.
Мы также можем прочитать файл в Matrix Market format. Это устранит ограничение на порядок.
Итак, мой вопрос заключается в том, как я могу получить результирующие строки (или партии) в матрицу (или пакетную матрицу) масштабируемым образом (т.е. матрица действительно большая), так что я могу применить умножение матрицы как:
result = tf.matmul(Matrix,Matrix)
result = tf.batch_matmul(batched_Matrix,batched_Matrix)
И как продолжение вопроса: какое из них является самым быстрым решением, особенно когда речь идет о распределенном исполнении?
Спасибо за вашу помощь, Felix