tensorflow随笔-读文件

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

@author: liuxing

读取文件
"""
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: liuxing

读取文件
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

你可能感兴趣的:(AI)