金华建设网站的公司,如何打开国外网站,互联网发展趋势,做阿里巴巴好还是网站好大家好#xff0c;尽管大多数关于神经网络的文章都强调数学#xff0c;而TensorFlow文档则强调使用现成数据集进行快速实现#xff0c;但将这些资源应用于真实世界数据集是很有挑战性的#xff0c;很难将数学概念和现成数据集与我的具体用例联系起来。本文旨在提供一个实用…大家好尽管大多数关于神经网络的文章都强调数学而TensorFlow文档则强调使用现成数据集进行快速实现但将这些资源应用于真实世界数据集是很有挑战性的很难将数学概念和现成数据集与我的具体用例联系起来。本文旨在提供一个实用的、逐步的教程介绍如何使用TensorFlow训练深度学习模型并重点介绍如何将数据集重塑为TensorFlow对象以便TensorFlow框架能够识别。
本文主要内容包括 将DataFrame转换为TensorFlow对象 从头开始训练深度学习模型 使用预训练的模型训练深度学习模型 评估、预测和绘制训练后的模型。
安装TensorFlow和其他必需的库
首先你需要安装TensorFlow。你可以通过在终端或Anaconda中运行以下命令来完成
# 安装所需的软件包
!pip install tensorflow
!pip install tensorflow-datasets
安装TensorFlow之后导入其他必需的库如Numpy、Matplotlib和Sklearn。
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_splitfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
加载数据集
一旦导入了所有必需的库下一步是获取数据集来搭建模型。TensorFlow允许使用各种输入格式包括CSV、TXT和图像文件有些数据集可以从TensorFlow-dataset中导入这些数据集已准备好用作深度学习模型的输入。然而在许多情况下数据集是以DataFrame格式而不是TensorFlow对象格式存在的。本文我们将使用Sklearn中的MNIST数据集其格式为Pandas DataFrame。MNIST数据集广泛用于图像分类任务包括70000个手写数字的灰度图像每个图像大小为28x28像素。该数据集被分为60000个训练图像和10000个测试图像。
from sklearn.datasets import fetch_openml# 加载MNIST数据集
# mnist fetch_openml(mnist_784)# 输出MNIST数据集
print(Dataset type:, type(mnist.data))# 浏览一下加载的数据集
mnist.data.head() 通过输出DataFrame的前部我们可以观察到它包含784列每列代表一个像素。 将DataFrame转换为TensorFlow数据集对象
加载了Pandas DataFrame注意到TensorFlow不支持Pandas DataFrame作为模型的输入因此必须将DataFrame转换为可以用于训练或评估模型的张量。这个转换过程确保数据以与TensorFlow API兼容的格式存在为了将MNIST数据集从DataFrame转换为tf.data.Dataset对象可以执行以下步骤 将数据和目标转换为NumPy数组并对数据进行归一化处理 使用scikit-learn中的train_test_split将数据集拆分为训练集和测试集 将训练和测试数据重塑为28x28x1的图像 使用from_tensor_slices为训练集和测试集创建tf.data.Dataset对象
def get_dataset(mnist):# 加载MNIST数据集# mnist fetch_openml(mnist_784)# 将数据和目标转换成numpy数组X mnist.data.astype(float32)y mnist.target.astype(int32)# 将数据归一化使其数值在0和1之间X / 255.0# 将数据集分成训练集和测试集X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)# 将训练数据重塑为28x28x1的图像X_train X_train.values.reshape((-1, 28, 28, 1))X_test X_test.values.reshape((-1, 28, 28, 1))# 为训练和测试集创建TensorFlow数据集对象train_dataset tf.data.Dataset.from_tensor_slices((X_train, y_train))test_dataset tf.data.Dataset.from_tensor_slices((X_test, y_test))# 输出训练和测试集的形状print(Training data shape:, X_train.shape)print(Training labels shape:, y_train.shape)print(Testing data shape:, X_test.shape)print(Testing labels shape:, y_test.shape)return X_test, y_test, X_train, y_train 再来看一下我们的训练和测试TensorFlow对象 经过这个过程原始数据集已经成功转换为形状为560028281的TensorFlow对象。
经过以上的步骤我们已经完成了实战的前半部分后文将继续讲解有关定义深度学习模型、训练模型和评估模型的内容。
文章转载自: http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.nsppc.cn.gov.cn.nsppc.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn