tensorflow-读写数据最佳代码组合方式
生活随笔
收集整理的這篇文章主要介紹了
tensorflow-读写数据最佳代码组合方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最佳組合代碼模式為:
# Create the graph, etc. init_op = tf.global_variables_initializer()# Create a session for running operations in the Graph. sess = tf.Session()# Initialize the variables (like the epoch counter). sess.run(init_op)# Start input enqueue threads. coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord)try:while not coord.should_stop():# Run training steps or whateversess.run(train_op)except tf.errors.OutOfRangeError:print('Done training -- epoch limit reached') finally:# When done, ask the threads to stop.coord.request_stop()# Wait for threads to finish. coord.join(threads) sess.close()#!/usr/bin/env python2 # -*- coding: utf-8 -*- """讀取文件 """ import tensorflow as tf import osg=tf.Graph() with g.as_default():#生成文件名隊列fileName=os.getcwd()+"/1.csv"print fileNamefileNameQueue=tf.train.string_input_producer([fileName])#生成記錄鍵值對reader=tf.TextLineReader(skip_header_lines=1)key,value=reader.read(fileNameQueue)recordDefaults=[[""],[1],[1]]decoded=tf.decode_csv(value,record_defaults=recordDefaults)name,age,source=tf.train.shuffle_batch(decoded,batch_size=2,capacity=2,min_after_dequeue=1) features=tf.transpose(tf.stack([age,source]))with tf.Session(graph=g) as sess:# 開始產生文件名隊列coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=coord)print sess.run(features)coord.request_stop()coord.join(threads)[[32 99]
[36 75]]
[[32 36]
[99 75]]
/Users/xxxxx/Documents/AIstudy/tf/1.csv:3
lisi,36,75
$ cat 1.csv
name,age,source
zhanghua,32,99
liuzhi,29,69
lisi,36,75
總結
以上是生活随笔為你收集整理的tensorflow-读写数据最佳代码组合方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mockjs语法规范、设置mockjs拦
- 下一篇: 清理linux 服务器的命令行,使用Li