代码改变世界

TensorFlow读取CSV数据

  猎手家园  阅读(3915)  评论(0编辑  收藏  举报

代码来源于官方文档,做了一些小小的调整:

复制代码
# -*- coding:utf-8 -*-
import tensorflow as tf

filename_queue = tf.train.string_input_producer(["file01.csv", "file02.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1]]
col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults)
features = tf.stack([col1, col2])

init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()  # local variables like epoch_num, batch_size 可以不初始化local
with tf.Session() as sess:
    sess.run(init_op)
    sess.run(local_init_op)

    # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(5):
        # Retrieve a single instance:
        example, label = sess.run([features, col3])
        print(example)
        print(label)

    coord.request_stop()
    coord.join(threads)
复制代码

 

file01.csv 和 file02.csv 格式一样:

19,3,1
10,2,3
11,3,1
12,4,2
17,5,1
18,6,2
......

 

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2016-05-12 如何使用Hive&R从Hadoop集群中提取数据进行分析
2016-05-12 CentOS6.5下实现R绘图
点击右上角即可分享
微信分享提示