39 深度学习(三):tensorflow.data模块的使用(基础,可跳)

这篇具有很好参考价值的文章主要介绍了39 深度学习(三):tensorflow.data模块的使用(基础,可跳)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

data模块的使用

在训练的过程中,当数据量一大的时候,我们纯读取一个文件,然后每次训练都调用相同的文件,然后进行处理是很不科学的,或者说,当我们需要进行多次训练的时候,我们实际上可以将数据先切分,打乱到对应的位置,然后存储到文件夹当中,下次读取然后进行训练。这样子也可以避免一下子加载太多的数据。(这对于大数据的图像切割领域尤其重要)

基础api的介绍

import numpy as np
import pandas as pd
import tensorflow as tf

# 经过下面的使用得到的dataset可以当作是迭代器 或者 就是数据提取器

# from_tensor_slices数据切片 返回的是一个迭代器 需要for去取 
dataset = tf.data.Dataset.from_tensor_slices(np.arange(3))
for item in dataset:
    print(item)
print('-'*50)

# from_tensors 不对数据进行处理 就是一个长数据 但是一样得用for去取数据
dataset = tf.data.Dataset.from_tensors(np.arange(3))
for item in dataset:
    print(item)
print('-'*50)

# batch(2)一次性取多少数据
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10)).batch(4)
for item in dataset:
    print(item)
print('-'*50)

# repeat把数据进行复制处理
dataset = tf.data.Dataset.from_tensor_slices(np.arange(2)).repeat(2)
for item in dataset:
    print(item)
print('-'*50)

# interleave 就是提取数据的方式 和下面进行对比
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10)).batch(2)
dataset2 = dataset.interleave(
    lambda v: tf.data.Dataset.from_tensors(v).repeat(3), # map_fn
    cycle_length = 3, # cycle_length,每一个cycle提取的个数
    block_length = 2, # block_length
)
for item in dataset2:
    print(item)

# interleave 就是提取数据的方式
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10)).batch(2)
dataset2 = dataset.interleave(
    lambda v: tf.data.Dataset.from_tensor_slices(v).repeat(3), # map_fn
    cycle_length = 3, # cycle_length,竖选3个为一个cycle
    block_length = 2, # block_length,横选2个
)
for item in dataset2:
    print(item)
print('-'*50)


x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array(['cat', 'dog', 'fox'])
#输入的参数是元祖的情况下
dataset3 = tf.data.Dataset.from_tensor_slices((x, y))
print(dataset3)

for item_x, item_y in dataset3:
    print(item_x, item_y)
#输入的参数是字典的情况下
dataset4 = tf.data.Dataset.from_tensor_slices({"feature1": x,"label": y})
print(dataset4)
for item in dataset4:
    print(item["feature1"], item["label"])

输出:

tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
--------------------------------------------------
tf.Tensor([0 1 2], shape=(3,), dtype=int64)
--------------------------------------------------
tf.Tensor([0 1 2 3], shape=(4,), dtype=int64)
tf.Tensor([4 5 6 7], shape=(4,), dtype=int64)
tf.Tensor([8 9], shape=(2,), dtype=int64)
--------------------------------------------------
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
--------------------------------------------------
tf.Tensor([0 1], shape=(2,), dtype=int64)
tf.Tensor([0 1], shape=(2,), dtype=int64)
tf.Tensor([2 3], shape=(2,), dtype=int64)
tf.Tensor([2 3], shape=(2,), dtype=int64)
tf.Tensor([4 5], shape=(2,), dtype=int64)
tf.Tensor([4 5], shape=(2,), dtype=int64)
tf.Tensor([0 1], shape=(2,), dtype=int64)
tf.Tensor([2 3], shape=(2,), dtype=int64)
tf.Tensor([4 5], shape=(2,), dtype=int64)
tf.Tensor([6 7], shape=(2,), dtype=int64)
tf.Tensor([6 7], shape=(2,), dtype=int64)
tf.Tensor([8 9], shape=(2,), dtype=int64)
tf.Tensor([8 9], shape=(2,), dtype=int64)
tf.Tensor([6 7], shape=(2,), dtype=int64)
tf.Tensor([8 9], shape=(2,), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(5, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(5, shape=(), dtype=int64)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
tf.Tensor(2, shape=(), dtype=int64)
tf.Tensor(3, shape=(), dtype=int64)
tf.Tensor(4, shape=(), dtype=int64)
tf.Tensor(5, shape=(), dtype=int64)
tf.Tensor(6, shape=(), dtype=int64)
tf.Tensor(7, shape=(), dtype=int64)
tf.Tensor(8, shape=(), dtype=int64)
tf.Tensor(9, shape=(), dtype=int64)
tf.Tensor(6, shape=(), dtype=int64)
tf.Tensor(7, shape=(), dtype=int64)
tf.Tensor(8, shape=(), dtype=int64)
tf.Tensor(9, shape=(), dtype=int64)
tf.Tensor(6, shape=(), dtype=int64)
tf.Tensor(7, shape=(), dtype=int64)
tf.Tensor(8, shape=(), dtype=int64)
tf.Tensor(9, shape=(), dtype=int64)
--------------------------------------------------
<_TensorSliceDataset element_spec=(TensorSpec(shape=(2,), dtype=tf.int64, name=None), TensorSpec(shape=(), dtype=tf.string, name=None))>
tf.Tensor([1 2], shape=(2,), dtype=int64) tf.Tensor(b'cat', shape=(), dtype=string)
tf.Tensor([3 4], shape=(2,), dtype=int64) tf.Tensor(b'dog', shape=(), dtype=string)
tf.Tensor([5 6], shape=(2,), dtype=int64) tf.Tensor(b'fox', shape=(), dtype=string)
<_TensorSliceDataset element_spec={'feature1': TensorSpec(shape=(2,), dtype=tf.int64, name=None), 'label': TensorSpec(shape=(), dtype=tf.string, name=None)}>
tf.Tensor([1 2], shape=(2,), dtype=int64) tf.Tensor(b'cat', shape=(), dtype=string)
tf.Tensor([3 4], shape=(2,), dtype=int64) tf.Tensor(b'dog', shape=(), dtype=string)
tf.Tensor([5 6], shape=(2,), dtype=int64) tf.Tensor(b'fox', shape=(), dtype=string)

csv文件

执行流程:

!rm -rf generate_csv

存数据

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import os

# 数据准备
# -----------------------------------------------------------------------------
housing = fetch_california_housing()
x_train_all, x_test, y_train_all, y_test = train_test_split(
    housing.data, housing.target, random_state = 7)
x_train, x_valid, y_train, y_valid = train_test_split(
    x_train_all, y_train_all, random_state = 11)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)
# -----------------------------------------------------------------------------

# 下面要把特征工程后的数据存为csv文件
output_dir = "generate_csv"
if not os.path.exists(output_dir):
    os.mkdir(output_dir)

def save_to_csv(output_dir, data, name_prefix, header=None, n_parts=10):
    #生成文件名
    path_format = os.path.join(output_dir, "{}_{:02d}.csv")
    #把数据分为n_parts部分,写到文件中去 enumerate就是多一个i从0开始,不断加上去的
    for file_idx, row_indices in enumerate(np.array_split(np.arange(len(data)), n_parts)):
        #生成子文件名
        part_csv = path_format.format(name_prefix, file_idx)
        with open(part_csv, "wt", encoding="utf-8") as f:
            #先写头部
            if header is not None:
                f.write(header + "\n")
            for row_index in row_indices:
                #把字符串化后的每个字符串用逗号拼接起来
                f.write(",".join([repr(col) for col in data[row_index]]))
                f.write('\n')

#np.c_把x和y合并起来
train_data = np.c_[x_train_scaled, y_train]
valid_data = np.c_[x_valid_scaled, y_valid]
test_data = np.c_[x_test_scaled, y_test]
#头部,特征,也有目标
header_cols = housing.feature_names + ["MidianHouseValue"]
#把列表变为字符串
header_str = ",".join(header_cols)
print(header_str)
save_to_csv(output_dir, train_data, "train",header_str, n_parts=20)
save_to_csv(output_dir, valid_data, "valid",header_str, n_parts=10)
save_to_csv(output_dir, test_data, "test",header_str, n_parts=10)

输出:

(11610, 8) (11610,)
(3870, 8) (3870,)
(5160, 8) (5160,)
MedInc,HouseAge,AveRooms,AveBedrms,Population,AveOccup,Latitude,Longitude,MidianHouseValue
!cd generate_csv;ls

输出:

test_00.csv  test_06.csv   train_02.csv  train_08.csv  train_14.csv  valid_00.csv  valid_06.csv
test_01.csv  test_07.csv   train_03.csv  train_09.csv  train_15.csv  valid_01.csv  valid_07.csv
test_02.csv  test_08.csv   train_04.csv  train_10.csv  train_16.csv  valid_02.csv  valid_08.csv
test_03.csv  test_09.csv   train_05.csv  train_11.csv  train_17.csv  valid_03.csv  valid_09.csv
test_04.csv  train_00.csv  train_06.csv  train_12.csv  train_18.csv  valid_04.csv
test_05.csv  train_01.csv  train_07.csv  train_13.csv  train_19.csv  valid_05.csv

提取数据:

import pprint

# 提取数据的流程
# 获取文件名称
filenames = os.listdir('./generate_csv')
train_filenames = []
test_filenames = []
valid_filenames = []
for filename in filenames:
  if filename[0] == 'v':
    valid_filenames.append('generate_csv/'+filename)
  elif filename[1] == 'e':
    test_filenames.append('generate_csv/'+filename)
  else:
    train_filenames.append('generate_csv/'+filename)

def parse_csv_line(line, n_fields = 9):
    #先写一个默认的格式,就是9个nan,如果从csv中读取缺失数据,就会变为nan
    defs = [tf.constant(np.nan)] * n_fields
    #使用decode_csv解析
    parsed_fields = tf.io.decode_csv(line, record_defaults=defs)
    #前8个是x,最后一个是y
    x = tf.stack(parsed_fields[0:-1])
    y = tf.stack(parsed_fields[-1:])
    return x, y


def csv_reader_dataset(filenames, n_readers=5,batch_size=32, n_parse_threads=5,shuffle_buffer_size=10000):
    # 将数据集放入这个dataset当中,他会默认进行shuffle
    dataset = tf.data.Dataset.list_files(filenames)
    #变为repeat dataset可以让读到最后一个样本时,从新去读第一个样本
    dataset = dataset.repeat()
    dataset = dataset.interleave(
        #skip(1)是因为每个文件存了特征名字,target名字
        lambda filename: tf.data.TextLineDataset(filename).skip(1),
        cycle_length = n_readers
    )
    dataset.shuffle(shuffle_buffer_size) #对数据进行洗牌,混乱
    #map,通过parse_csv_line对数据集进行映射,map只会给函数传递一个参数
    dataset = dataset.map(parse_csv_line,num_parallel_calls=n_parse_threads)
    dataset = dataset.batch(batch_size)
    return dataset

# 得到dataset类
train_set = csv_reader_dataset(train_filenames, batch_size=4)


print(train_set)
#是csv_reader_dataset处理后的结果,
for x_batch, y_batch in train_set.take(2):
    print("x:")
    pprint.pprint(x_batch)
    print("y:")
    pprint.pprint(y_batch)

输出:

<_BatchDataset element_spec=(TensorSpec(shape=(None, 8), dtype=tf.float32, name=None), TensorSpec(shape=(None, 1), dtype=tf.float32, name=None))>
x:
<tf.Tensor: shape=(4, 8), dtype=float32, numpy=
array([[-1.1199750e+00, -1.3298433e+00,  1.4190045e-01,  4.6581370e-01,
        -1.0301778e-01, -1.0744184e-01, -7.9505241e-01,  1.5304717e+00],
       [ 4.9710345e-02, -8.4924191e-01, -6.2146995e-02,  1.7878747e-01,
        -8.0253541e-01,  5.0660671e-04,  6.4664572e-01, -1.1060793e+00],
       [-6.6722274e-01, -4.8239522e-02,  3.4529406e-01,  5.3826684e-01,
         1.8521839e+00, -6.1125383e-02, -8.4170932e-01,  1.5204847e+00],
       [-3.2652634e-01,  4.3236190e-01, -9.3454592e-02, -8.4029920e-02,
         8.4600359e-01, -2.6631648e-02, -5.6176794e-01,  1.4228760e-01]],
      dtype=float32)>
y:
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[0.66 ],
       [2.286],
       [1.59 ],
       [2.431]], dtype=float32)>
x:
<tf.Tensor: shape=(4, 8), dtype=float32, numpy=
array([[-1.0775077 , -0.4487407 , -0.5680568 , -0.14269263, -0.09666677,
         0.12326469, -0.31448638, -0.4818959 ],
       [-0.9490939 ,  0.6726626 ,  0.28370556,  0.1065553 , -0.65464777,
        -0.06239493,  0.21273656,  0.0024705 ],
       [-1.453851  ,  1.8741661 , -1.1315714 ,  0.36112761, -0.3978858 ,
        -0.03273859, -0.73906416,  0.64662784],
       [ 1.5180511 , -0.52884096,  0.81024706, -0.1921417 ,  0.44135395,
         0.02733506, -0.81838083,  0.8563535 ]], dtype=float32)>
y:
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[0.978],
       [0.607],
       [1.875],
       [2.898]], dtype=float32)>

tfrecord

Tfrecord是TensorFlow独有的数据格式,有读取速度快的优势,正常情况下我们训练文件数据集经常会生成 train, test 或者val文件夹,这些文件夹内部往往会存着成千上万的图片或文本等文件,这些文件被散列存着,这样不仅占用磁盘空间,并且再被一个个读取的时候会非常慢,繁琐。占用大量内存空间(有的大型数据不足以一次性加载)。此时我们TFRecord格式的文件存储形式会很合理的帮我们存储数据。TFRecord内部使用了“Protocol Buffer”二进制数据编码方案,它只占用一个内存块,只需要一次性加载一个二进制文件的方式即可,简单,快速,尤其对大型训练数据很友好。而且当我们的训练数据量比较大的时候,可以将数据分成多个TFRecord文件,来提高处理效率。

之后补,这边采用csv也可以,方法类似,这边先跳,之后补充。文章来源地址https://www.toymoban.com/news/detail-716164.html

到了这里,关于39 深度学习(三):tensorflow.data模块的使用(基础,可跳)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深度学习基础之《TensorFlow框架(2)—图》

    一、什么是图结构 1、图包含了一组tf.Operation代表的计算单元对象和tf.Tensor代表的计算单元之间流动的数据 图结构:数据(Tensor) + 操作(Operation) 二、图相关操作 1、默认图 通常TensorFlow会默认帮我们创建一张图 查看默认图的两种方法: (1)通过调用tf.compat.v1.get_default_graph()访

    2024年02月20日
    浏览(28)
  • 深度学习基础之《TensorFlow框架(6)—张量》

    一、张量 1、什么是张量 张量Tensor和ndarray是有联系的,当我们print()打印值的时候,它返回的就是ndarray对象 TensorFlow的张量就是一个n维数组,类型为tf.Tensor。Tensor具有以下两个重要的属性: (1)type:数据类型 (2)shape:形状(阶) 2、张量的类型 张量,在计算机当中如何存

    2024年02月21日
    浏览(28)
  • 深度学习基础之《TensorFlow框架(3)—TensorBoard》

    一、TensorBoard可视化学习 1、TensorFlow有一个亮点就是,我们能看到自己写的程序的可视化效果,这个功能就是TensorBoard 2、TensorFlow可用于训练大规模深度神经网络所需的计算,使用该工具涉及的计算往往复杂而深奥。为了方便TensorFlow程序的理解、调试和优化,TensorFlow提供了

    2024年02月21日
    浏览(27)
  • 深度学习基础之《TensorFlow框架(14)—TFRecords》

    一、什么是TFRecords文件 1、TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是它能更好的利用内存,更方便复制和移动,并且不需要单独的标签文件 2、使用步骤 (1)获取数据 (2)将数据填入到Example协议内存块(protocol buffer) (3)将协议内存块序列化为字

    2024年04月26日
    浏览(29)
  • 深度学习基础之《TensorFlow框架(4)—Operation》

    一、常见的OP 1、举例 类型 实例 标量运算 add,sub,mul,div,exp,log,greater,less,equal 向量运算 concat,slice,splot,canstant,rank,shape,shuffle 矩阵运算 matmul,matrixinverse,matrixdateminant 带状态的运算 variable,assgin,assginadd 神经网络组件 softmax,sigmoid,relu,convolution,max_pool 存

    2024年02月20日
    浏览(31)
  • 深度学习TensorFlow2基础知识学习前半部分

    目录 测试TensorFlow是否支持GPU: 自动求导:  数据预处理 之 统一数组维度  定义变量和常量  训练模型的时候设备变量的设置 生成随机数据 交叉熵损失CE和均方误差函数MSE  全连接Dense层 维度变换reshape 增加或减小维度 数组合并 广播机制: 简单范数运算  矩阵转置 框架本

    2024年02月04日
    浏览(36)
  • 17- TensorFlow中使用Keras创建模型 (TensorFlow系列) (深度学习)

    知识要点 Keras 是一个用 Python 编写的高级神经网络 API 数据的开方:  np.sqrt(784)       # 28 代码运行调整到 CPU 或者 GPU: 模型显示: model.summary () 创建模型: 模型创建: model = Sequential () 添加卷积层: model.add (Dense(32, activation=\\\'relu\\\', input_dim=100))  # 第一层需要 input_dim 添加dropout: mod

    2024年02月01日
    浏览(76)
  • 使用TensorFlow训练深度学习模型实战(上)

    大家好,尽管大多数关于神经网络的文章都强调数学,而TensorFlow文档则强调使用现成数据集进行快速实现,但将这些资源应用于真实世界数据集是很有挑战性的,很难将数学概念和现成数据集与我的具体用例联系起来。本文旨在提供一个实用的、逐步的教程,介绍如何使用

    2024年02月15日
    浏览(35)
  • 使用TensorFlow训练深度学习模型实战(下)

    大家好,本文接TensorFlow训练深度学习模型的上半部分继续进行讲述,下面将介绍有关定义深度学习模型、训练模型和评估模型的内容。 定义深度学习模型 数据准备完成后,下一步是使用TensorFlow搭建神经网络模型,搭建模型有两个选项: 可以使用各种层,包括Dense、Conv2D和

    2024年02月15日
    浏览(25)
  • 【深度学习】实验06 使用TensorFlow完成线性回归

    TensorFlow是由Google开发的一个开源的机器学习框架。它可以让开发者更加轻松地构建和训练深度学习模型,从而解决各种自然语言处理、计算机视觉、语音识别、推荐系统等领域的问题。 TensorFlow的主要特点是灵活性和可伸缩性。它实现了一种基于数据流图的计算模型,使得用

    2024年02月10日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包