1. 程式人生 > >tensorflow-讀文件

tensorflow-讀文件

讀取文件 thread asp efault ans session tor runner imp

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Sep 15 10:54:53 2018 @author: myhaspl @email:[email protected] 讀取文件 """ import tensorflow as tf import os g=tf.Graph() with g.as_default(): #生成文件名隊列 fileName=os.getcwd()+"/diabetes.csv" fileNameQueue=tf.train.string_input_producer([fileName]) #生成記錄鍵值對 reader=tf.TextLineReader(skip_header_lines=1) key,value=reader.read(fileNameQueue) with tf.Session(graph=g) as sess: # 開始產生文件名隊列 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) print "key:" print sess.run(key)#文件名 print "values:" print sess.run(value)#讀取一行的內容 coord.request_stop() coord.join(threads)

key:
/Users/xxxxx/Documents/AIstudy/tf/diabetes.csv:2
values:
1,85,66,29,0,26.6,0.351,31,0

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018

@author: myhaspl
@email:[email protected]
讀取文件
Pregnancies,Glucose,BloodPressure,SkinThickness,Insulin,BMI,DiabetesPedigreeFunction,Age,Outcome
"""
import tensorflow as tf
import os

g=tf.Graph()
with g.as_default():
    #生成文件名隊列
    fileName=os.getcwd()+"/1.csv"
    fileNameQueue=tf.train.string_input_producer([fileName])
    #生成記錄鍵值對
    reader=tf.TextLineReader(skip_header_lines=1)
    key,value=reader.read(fileNameQueue)
    recordDefaults=[[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.],[1.]]
    decoded=tf.decode_csv(value,record_defaults=recordDefaults)
    pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age,outcome=tf.train.shuffle_batch(decoded,batch_size=2,capacity=1000,min_after_dequeue=1)    
    features=tf.transpose(tf.stack([pregnancies,glucose,bloodPressure,skinThickness,insulin,bmi,diabetespedigreefunction,age,outcome]))

with tf.Session(graph=g) as sess:
    # 開始產生文件名隊列
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    print "鍵:"
    print sess.run(key)#文件名
    print "值:"
    print sess.run(value)#讀取一行的內容
    print "屬性:"
    print sess.run(features)   

    coord.request_stop()
    coord.join(threads)

鍵:
/Users/xxx/Documents/AIstudy/tf/1.csv:2
值:
1,89,66,23,94,28.1,0.167,21,0
屬性:
[[ 1. 89. 66. ... 0.167 21. 0. ]
[ 6. 148. 72. ... 0.627 50. 1. ]]

1.csv

Pregnancies,Glucose,BloodPressure,SkinThickness,Insulin,BMI,DiabetesPedigreeFunction,Age,Outcome
6,148,72,35,0,33.6,0.627,50,1

1,85,66,29,0,26.6,0.351,31,0
8,183,64,0,0,23.3,0.672,32,1
1,89,66,23,94,28.1,0.167,21,0

tensorflow-讀文件