码迷,mamicode.com
首页 > 其他好文 > 详细

tensorflow-读文件

时间:2019-01-17 23:41:28      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:str   asp   .com   NPU   runner   features   ...   xxxx   import   

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Sep 15 10:54:53 2018 @author: myhaspl @email:myhaspl@myhaspl.com 读取文件 """ 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:myhaspl@myhaspl.com
读取文件
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-读文件

标签:str   asp   .com   NPU   runner   features   ...   xxxx   import   

原文地址:http://blog.51cto.com/13959448/2343954

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!