当前位置: 首页 > news >正文

网站建设专家旅行网站建设方案策划书

网站建设专家,旅行网站建设方案策划书,wordpress添加友情练级,深圳开发小程序文章目录 data模块的使用基础api的介绍csv文件tfrecord data模块的使用 在训练的过程中#xff0c;当数据量一大的时候#xff0c;我们纯读取一个文件#xff0c;然后每次训练都调用相同的文件#xff0c;然后进行处理是很不科学的#xff0c;或者说#xff0c;当我们需… 文章目录 data模块的使用基础api的介绍csv文件tfrecord 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_fncycle_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_fncycle_length 3, # cycle_length,竖选3个为一个cycleblock_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(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) tf.Tensor(2, shape(), dtypeint64) -------------------------------------------------- tf.Tensor([0 1 2], shape(3,), dtypeint64) -------------------------------------------------- tf.Tensor([0 1 2 3], shape(4,), dtypeint64) tf.Tensor([4 5 6 7], shape(4,), dtypeint64) tf.Tensor([8 9], shape(2,), dtypeint64) -------------------------------------------------- tf.Tensor(0, shape(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) tf.Tensor(0, shape(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) -------------------------------------------------- tf.Tensor([0 1], shape(2,), dtypeint64) tf.Tensor([0 1], shape(2,), dtypeint64) tf.Tensor([2 3], shape(2,), dtypeint64) tf.Tensor([2 3], shape(2,), dtypeint64) tf.Tensor([4 5], shape(2,), dtypeint64) tf.Tensor([4 5], shape(2,), dtypeint64) tf.Tensor([0 1], shape(2,), dtypeint64) tf.Tensor([2 3], shape(2,), dtypeint64) tf.Tensor([4 5], shape(2,), dtypeint64) tf.Tensor([6 7], shape(2,), dtypeint64) tf.Tensor([6 7], shape(2,), dtypeint64) tf.Tensor([8 9], shape(2,), dtypeint64) tf.Tensor([8 9], shape(2,), dtypeint64) tf.Tensor([6 7], shape(2,), dtypeint64) tf.Tensor([8 9], shape(2,), dtypeint64) tf.Tensor(0, shape(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) tf.Tensor(2, shape(), dtypeint64) tf.Tensor(3, shape(), dtypeint64) tf.Tensor(4, shape(), dtypeint64) tf.Tensor(5, shape(), dtypeint64) tf.Tensor(0, shape(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) tf.Tensor(2, shape(), dtypeint64) tf.Tensor(3, shape(), dtypeint64) tf.Tensor(4, shape(), dtypeint64) tf.Tensor(5, shape(), dtypeint64) tf.Tensor(0, shape(), dtypeint64) tf.Tensor(1, shape(), dtypeint64) tf.Tensor(2, shape(), dtypeint64) tf.Tensor(3, shape(), dtypeint64) tf.Tensor(4, shape(), dtypeint64) tf.Tensor(5, shape(), dtypeint64) tf.Tensor(6, shape(), dtypeint64) tf.Tensor(7, shape(), dtypeint64) tf.Tensor(8, shape(), dtypeint64) tf.Tensor(9, shape(), dtypeint64) tf.Tensor(6, shape(), dtypeint64) tf.Tensor(7, shape(), dtypeint64) tf.Tensor(8, shape(), dtypeint64) tf.Tensor(9, shape(), dtypeint64) tf.Tensor(6, shape(), dtypeint64) tf.Tensor(7, shape(), dtypeint64) tf.Tensor(8, shape(), dtypeint64) tf.Tensor(9, shape(), dtypeint64) -------------------------------------------------- _TensorSliceDataset element_spec(TensorSpec(shape(2,), dtypetf.int64, nameNone), TensorSpec(shape(), dtypetf.string, nameNone)) tf.Tensor([1 2], shape(2,), dtypeint64) tf.Tensor(bcat, shape(), dtypestring) tf.Tensor([3 4], shape(2,), dtypeint64) tf.Tensor(bdog, shape(), dtypestring) tf.Tensor([5 6], shape(2,), dtypeint64) tf.Tensor(bfox, shape(), dtypestring) _TensorSliceDataset element_spec{feature1: TensorSpec(shape(2,), dtypetf.int64, nameNone), label: TensorSpec(shape(), dtypetf.string, nameNone)} tf.Tensor([1 2], shape(2,), dtypeint64) tf.Tensor(bcat, shape(), dtypestring) tf.Tensor([3 4], shape(2,), dtypeint64) tf.Tensor(bdog, shape(), dtypestring) tf.Tensor([5 6], shape(2,), dtypeint64) tf.Tensor(bfox, shape(), dtypestring)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, headerNone, n_parts10):#生成文件名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, encodingutf-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_parts20) save_to_csv(output_dir, valid_data, valid,header_str, n_parts10) save_to_csv(output_dir, test_data, test,header_str, n_parts10)输出 (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中读取缺失数据就会变为nandefs [tf.constant(np.nan)] * n_fields#使用decode_csv解析parsed_fields tf.io.decode_csv(line, record_defaultsdefs)#前8个是x最后一个是yx tf.stack(parsed_fields[0:-1])y tf.stack(parsed_fields[-1:])return x, ydef csv_reader_dataset(filenames, n_readers5,batch_size32, n_parse_threads5,shuffle_buffer_size10000):# 将数据集放入这个dataset当中他会默认进行shuffledataset 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_callsn_parse_threads)dataset dataset.batch(batch_size)return dataset# 得到dataset类 train_set csv_reader_dataset(train_filenames, batch_size4)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), dtypetf.float32, nameNone), TensorSpec(shape(None, 1), dtypetf.float32, nameNone)) x: tf.Tensor: shape(4, 8), dtypefloat32, numpy array([[-1.1199750e00, -1.3298433e00, 1.4190045e-01, 4.6581370e-01,-1.0301778e-01, -1.0744184e-01, -7.9505241e-01, 1.5304717e00],[ 4.9710345e-02, -8.4924191e-01, -6.2146995e-02, 1.7878747e-01,-8.0253541e-01, 5.0660671e-04, 6.4664572e-01, -1.1060793e00],[-6.6722274e-01, -4.8239522e-02, 3.4529406e-01, 5.3826684e-01,1.8521839e00, -6.1125383e-02, -8.4170932e-01, 1.5204847e00],[-3.2652634e-01, 4.3236190e-01, -9.3454592e-02, -8.4029920e-02,8.4600359e-01, -2.6631648e-02, -5.6176794e-01, 1.4228760e-01]],dtypefloat32) y: tf.Tensor: shape(4, 1), dtypefloat32, numpy array([[0.66 ],[2.286],[1.59 ],[2.431]], dtypefloat32) x: tf.Tensor: shape(4, 8), dtypefloat32, 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 ]], dtypefloat32) y: tf.Tensor: shape(4, 1), dtypefloat32, numpy array([[0.978],[0.607],[1.875],[2.898]], dtypefloat32)tfrecord Tfrecord是TensorFlow独有的数据格式有读取速度快的优势正常情况下我们训练文件数据集经常会生成 train, test 或者val文件夹这些文件夹内部往往会存着成千上万的图片或文本等文件这些文件被散列存着这样不仅占用磁盘空间并且再被一个个读取的时候会非常慢繁琐。占用大量内存空间有的大型数据不足以一次性加载。此时我们TFRecord格式的文件存储形式会很合理的帮我们存储数据。TFRecord内部使用了“Protocol Buffer”二进制数据编码方案它只占用一个内存块只需要一次性加载一个二进制文件的方式即可简单快速尤其对大型训练数据很友好。而且当我们的训练数据量比较大的时候可以将数据分成多个TFRecord文件来提高处理效率。 之后补这边采用csv也可以方法类似这边先跳之后补充。
http://www.zqtcl.cn/news/314018/

相关文章:

  • o2o网站运维建设方案宿州市网站建设
  • 上海网站排名优化公司马蜂窝是什么做的网站
  • 邢台网站建设网站淘宝小程序入口
  • 公司起名自动生成器网站优化方法
  • 太原网站开发团队wordpress 链接 插件
  • 阿克顿巴网站建设的目的智通人才招聘网
  • 网站快速优化成营销型网站制作
  • 网站建设mus18php 网站缓存文件
  • 建设企业网站企业网上银行助手下载泰安网签数据
  • 网站开发产权保护如何去掉2345网址导航
  • 网站数据库怎么备份自建网站步骤
  • 多语言版本的网站做网站西安
  • 蚌埠市住房建设部网站在线免费做网站
  • 天津网站建设网络企业网址模板
  • 如何做京东购物网站旅游前 做攻略有什么网站好用
  • 嘉兴网站推广优化公司wordpress显示系统
  • 网站建站定做空间里怎么放多个网站
  • 清远市企业网站seo联系方式深圳人才市场现场招聘信息
  • 网站制作在哪能看网站开发前台开发
  • 一般网站用什么数据库游戏源码搭建
  • 做国外搞笑网站上海建设工程网站
  • 泰州免费网站建站模板网站建设用python怎么样
  • 优秀作文网站都有哪些企业网站的开发与应用
  • 织梦做网站被告如何再工商局网站做设备抵押
  • 什么是自适应网站怎么做国际购物网站
  • 促销活动推广文案网站加alt属性对优化有影响吗
  • 平湖网站改版洛卡博网站谁做的
  • 买卖平台有哪些网站三航奔腾建设有限公司官方网站
  • 网站建设的企业wordpress teamtalk
  • 公司起名字大全免费查询网站的哪些标签需要优化