TensorFlow如何读取CSV数据

  介绍

这篇文章主要介绍TensorFlow如何读取CSV数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

<强>方法一:

<强>详细读取tf_read。csv代码

#编码:utf - 8   ,   import  tensorflow  as  tf   ,   时间=filename_queue  tf.train.string_input_producer ([“/home/yongcai/tf_read.csv"])   时间=reader  tf.TextLineReader ()   关键,value =, reader.read (filename_queue)   ,   record_defaults =, [[1], [1], [1], [1]]。   col2, col1,还以为,col3, col4 =, tf.decode_csv(价值,record_defaults=record_defaults)   ,   时间=features  tf.stack ([col1, col2, col3])   ,   时间=init_op  tf.global_variables_initializer ()   时间=local_init_op  tf.local_variables_initializer ()   ,   with  tf.Session (), as 税:   ,sess.run (init_op)   ,sess.run (local_init_op)   ,   ,# Start  populating 从而filename 队列。=,,coord  tf.train.Coordinator ()=,,threads  tf.train.start_queue_runners (coord=coord)   ,   ,试一试:   for 才能小姐:拷贝范围(30):   ,,,,label =, sess.run([特性,,col4])   ,才能打印(示例)   ,,#,打印(标签)   ,except  tf.errors.OutOfRangeError:   print 才能;& # 39;Done  ! ! ! & # 39;   ,   ,最后:   coord.request_stop才能()   coord.join才能(线程)

<强> tf_read。csv数据:

-0.76 15.67 15.67——0.12   -0.48 12.52 12.51——0.06   1.33 9.11 0.12 9.1   -0.88 20.35 20.36——0.18   -0.25 3.99 3.99——0.01   -0.87 26.25 26.25——0.23   -1.03 2.87 2.87——0.03   -0.51 7.81 7.81——0.04   -1.57 14.46 14.46——0.23   -0.1 10.02 10.02——0.01   -0.56 8.92 8.92——0.05   -1.2 4.1 4.1——0.05   -0.77 5.15 5.15——0.04   -0.88 4.48 4.48——0.04   -2.7 10.82 10.82——0.3   -1.23 2.4 2.4——0.03   -0.77 5.16 5.15——0.04   -0.81 6.15 6.15——0.05   -0.6 5.01 - 0.03 - 5所示   -1.25 4.75 4.75——0.06   -2.53 7.31 7.3——0.19   -1.15 16.39 16.39——0.19   -1.7 5.19 5.18——0.09   -0.62 3.23 3.22——0.02   -0.74 17.43 17.41——0.13   -0.77 15.41 15.41——0.12   47个0 47.01   0.25 3.98 0.01 3.98   -1.1 9.01 9.01——0.1   3.87 -1.02 - 0.04 3.87

<强>方法二:

详细读取Iris_train。csv, Iris_test。csv代码

#编码:utf - 8   ,   import  tensorflow  as  tf   import 操作系统   ,   os.chdir (“/home/yongcai/?   print (os.getcwd ())   ,   ,   def  read_data (file_queue):=,,reader  tf.TextLineReader (skip_header_lines=1)   关键的不同之处是,value =, reader.read (file_queue)   ,defaults =, [[0], [0],, [0], [0],, [0], [& # 39; & # 39;]]   ,Id, SepalLengthCm, SepalWidthCm,, PetalLengthCm,, PetalWidthCm,, Species =, tf.decode_csv(值,默认值)   ,=,,preprocess_op  tf.case ({   tf.equal才能(物种,tf.constant (& # 39; Iris-setosa& # 39;)):,λ:,tf.constant (0)   tf.equal才能(物种,tf.constant (& # 39; Iris-versicolor& # 39;)):,λ:,tf.constant (1),   tf.equal才能(物种,tf.constant (& # 39; Iris-virginica& # 39;)):,λ:,tf.constant (2),   ,},λ:tf.constant(1),独家=True)   ,   ,return  tf.stack ([SepalWidthCm, SepalLengthCm,还以为,PetalLengthCm, PetalWidthCm]), preprocess_op   ,   ,   batch_size, def  create_pipeline(文件名,num_epochs=None):=,,file_queue  tf.train.string_input_producer((文件名),num_epochs=num_epochs)   ,例如,label =, read_data (file_queue)   ,   min_after_dequeue =, 1000=,,capacity  min_after_dequeue  + batch_size   ,example_batch, label_batch =, tf.train.shuffle_batch (   ,,,,标签,,batch_size=batch_size,能力=能力,   min_after_dequeue=min_after_dequeue才能   ,)   ,   ,return  example_batch label_batch   ,   ,   #,x_train_batch, y_train_batch =, create_pipeline (& # 39; Iris-train.csv& # 39;,, 50岁,num_epochs=1000)   x_test, y_test =, create_pipeline (& # 39; Iris-test.csv& # 39;,, 60)   ,   时间=init_op  tf.global_variables_initializer ()   时间=local_init_op  tf.local_variables_initializer ()   #,output  read  data 结果   with  tf.Session (), as 税:   ,sess.run (init_op)   ,sess.run (local_init_op)=,,coord  tf.train.Coordinator ()=,,thread  tf.train.start_queue_runners (coord=coord)   ,   ,试一试:   ,   ,,例如,label =, sess.run ([y_test x_test也])   print 才能的例子   print 才能;标签   ,   ,except  tf.errors.OutOfRangeError:   print 才能;& # 39;Done  ! ! ! & # 39;   ,   ,最后:   coord.request_stop才能()   coord.join才能(线程=线程)

TensorFlow如何读取CSV数据