设为首页 加入收藏

TOP

Python TensorFlow深度神经网络回归:keras.Sequential(一)
2023-07-25 21:26:18 】 浏览:91
Tags:Python TensorFlow 经网络 keras.Sequential

??本文介绍基于Python语言中TensorFlowKeras接口,实现深度神经网络回归的方法。

1 写在前面

??前期一篇文章Python TensorFlow深度学习回归代码:DNNRegressor详细介绍了基于TensorFlow tf.estimator接口的深度学习网络;而在TensorFlow 2.0中,新的Keras接口具有与 tf.estimator接口一致的功能,且其更易于学习,对于新手而言友好程度更高;在TensorFlow官网也建议新手从Keras接口入手开始学习。因此,本文结合TensorFlow Keras接口,加以深度学习回归的详细介绍与代码实战。

??和上述博客类似,本文第二部分为代码的分解介绍,第三部分为完整代码。一些在上述博客介绍过的内容,在本文中就省略了,大家如果有需要可以先查看上述文章Python TensorFlow深度学习回归代码:DNNRegressor

??相关版本信息:Python版本:3.8.5TensorFlow版本:2.4.1;编译器版本:Spyder 4.1.5

2 代码分解介绍

2.1 准备工作

??首先需要引入相关的库与包。

import os
import glob
import openpyxl
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import scipy.stats as stats
import matplotlib.pyplot as plt
from sklearn import metrics
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import regularizers
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers.experimental import preprocessing

??由于后续代码执行过程中,会有很多数据的展示与输出,其中多数数据都带有小数部分;为了让程序所显示的数据更为整齐、规范,我们可以对代码的浮点数、数组与NumPy对象对应的显示规则加以约束。

np.set_printoptions(precision=4,suppress=True)

??其中,precision设置小数点后显示的位数,默认为8suppress表示是否使用定点计数法(即与科学计数法相对)。

2.2 参数配置

??深度学习代码一大特点即为具有较多的参数需要我们手动定义。为避免调参时上下翻找,我们可以将主要的参数集中在一起,方便我们后期调整。

??其中,具体参数的含义在本文后续部分详细介绍。

# Input parameters.
DataPath="G:/CropYield/03_DL/00_Data/AllDataAll.csv"
ModelPath="G:/CropYield/03_DL/02_DNNModle"
CheckPointPath="G:/CropYield/03_DL/02_DNNModle/Weights"
CheckPointName=CheckPointPath+"/Weights_{epoch:03d}_{val_loss:.4f}.hdf5"
ParameterPath="G:/CropYield/03_DL/03_OtherResult/ParameterResult.xlsx"
TrainFrac=0.8
RandomSeed=np.random.randint(low=21,high=22)
CheckPointMethod='val_loss'
HiddenLayer=[64,128,256,512,512,1024,1024]
RegularizationFactor=0.0001
ActivationMethod='relu'
DropoutValue=[0.5,0.5,0.5,0.3,0.3,0.3,0.2]
OutputLayerActMethod='linear'
LossMethod='mean_absolute_error'
LearnRate=0.005
LearnDecay=0.0005
FitEpoch=500
BatchSize=9999
ValFrac=0.2
BestEpochOptMethod='adam'

2.3 数据导入与数据划分

??我的数据已经保存在了.csv文件中,因此可以用pd.read_csv直接读取。

??其中,数据的每一列是一个特征,每一行是全部特征与因变量(就是下面的Yield)组合成的样本。

# Fetch and divide data.
MyData=pd.read_csv(DataPath,names=['EVI0610','EVI0626','EVI0712','EVI0728','EVI0813','EVI0829',
                                   'EVI0914','EVI0930','EVI1016','Lrad06','Lrad07','Lrad08',
                                   'Lrad09','Lrad10','Prec06','Prec07','Prec08','Prec09',
                                   'Prec10','Pres06','Pres07','Pres08','Pres09','Pres10',
                                   'SIF161','SIF177','SIF193','SIF209','SIF225','SIF241',
                                   'SIF257','SIF273','SIF289','Shum06','Shum07','Shum08',
                                   'Shum09','Shum10','SoilType','Srad06','Srad07','Srad08',
                                   'Srad09','Srad10','Temp06','Temp07','Temp08','Temp09',
                                   'Temp10','Wind06','Wind07','Wind08','Wind09','Wind10',
                                   'Yield'],header=0)

??随后,对导入的数据划分训练集与测试集。

TrainData=MyData.sample(frac=TrainFrac,random_state=RandomSeed)
TestData=MyData.drop(TrainData.index)

??其中,TrainFrac为训练集(包括验证数据)所占比例,RandomSeed为随即划分数据时所用的随机数种子。

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python工具箱系列(十七) 下一篇flask,uwsgi,nginx部署配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目