IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> keras实现回归预测 -> 正文阅读

[人工智能]keras实现回归预测

机器学习小白一枚? 目前在做机器学习和光学的交叉? 希望各位大佬多多指正

背景:通过光纤结构的5个不同参数实现对其纯净度的预测

直接上图

?我最终的目标是要实现通过图中 k n L r?λ?这5个参数? 实现对每个模式的purity? Aeff? dispersion?以及keer这四个参数的预测? ? 那么这篇文章只做一个简单的对其中purity参数的预测? 具体最终的多维度预测我会在接下来的文章中更新...

? 进入代码

首先就是对数据集的一个读取? 然后创建两个数组分别承载他们

这个代码就是给他转化成一个np数组然后进行一个预处理? ? 数据分割那块根据自己的数据集自行决定? ?像我最后的数据集的话可能直接分成了两个文件? 就不需要在代码内给他们分割?

一个简单的网络架构? ?这里他的输入维度是隐式包含在第一个Dense层里? 我得输入维度有5个就设置为5? 第一个全连接层也设置为5?然后再搭一个Dense和一个output (这块可以根据你自己的需要去调整? 因为对于回归预测来说的话可能并不需要太复杂的结构? 当然如果你想提高一下模型的拟合能力和泛化效果的话可以加深网络层数? 但也不是越多越好? 要根据你自己的网络结构去调试)

下面compile函数就是进行一个拟合?这里选用Adam优化? 学习率设置为0.0001? 损失函数用mse? 评价函数用mae(对于mse?和?mae?这两个函数可以自行抉择? 因为看了好多回归的网络基本上用的都是这两个? 甚至有的根本不需要评价函数? 直接上损失就够了)?

自后fit函数? batchsize和epochs次数也是根据自己决定? bitchsize根据自己电脑计算能力?可以尽可能调小? ?validation_split是一个动态分配训练集? 比如我设置的是0.2?就是动态分配20%的训练数据作为测试? 主要目的是为了更直观的防止一个过拟合问题的出现? verbose就是迭代过程中的详细程度? 主要有0 1 2? 根据自己需求设置

预测部分代码? 和最开始对训练数据的处理差不多

最后是完整代码

import os

import xlrd
import xlwt
import numpy as np
from random import randint
from sklearn.utils import shuffle
from sklearn.preprocessing import MinMaxScaler, StandardScaler
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation,Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
import matplotlib.pyplot as plt

# data = xlrd.open_workbook('puity.xlsx')
# sheet = data.sheet_by_index(0)
#row1 = sheet.row_values(1)       #行
#col1 = sheet.col_values(3)        #列
#cell = sheet.cell(1,1).value

import tensorflow as tf
print("Num GPUS Available:",len(tf.config.experimental.list_physical_devices('GPUs')))
os.environ["CUDA_VISIBLE_DEVICES"]="0"

data = xlrd.open_workbook('puity_train.xlsx')
sheet = data.sheet_by_index(0)
nrows = sheet.nrows   #行数
ncols = sheet.ncols   #列数

samples = []    #训练样本
labels = []     #对应标签

for i in range(nrows):
    row = sheet.row_values(i)
    samples.append(row[0:5])
    labels.append(row[12])

from sklearn.model_selection import train_test_split
train_samples,test_samples,train_labels,test_labels =train_test_split(samples,labels,random_state = 0,test_size = 0.20)

train_labels=np.array(train_labels)
train_samples=np.array(train_samples)
test_samples=np.array(test_samples)
test_labels=np.array(test_labels)
# #乱序
train_labels,train_samples=shuffle(train_labels,train_samples)

# print(train_samples)
# print(test_samples)

#数据预处理
scaler = StandardScaler()
train_samples = scaler.fit_transform(train_samples)
test_samples = scaler.fit_transform(test_samples)
#
# print(train_samples)
# # print(train_labels)
# print(test_samples)
# # print(test_labels)


#
# scaler = MinMaxScaler(feature_range=(0,1))
# scaled_train_samples = scaler.fit_transform(np.array(train_samples).reshape(-1,1))

model = Sequential([
     Dense(units=5,input_shape=(5,),activation='relu'),
     Dense(units=32,activation='relu'),
     Dense(units=1)
 ])
# # #可视化网络模型
model.summary()
#
model.compile(optimizer=Adam(learning_rate=0.0001),loss='mse', metrics='mae')

# #
# # # 参数 validation_split=0.2   动态分配20%的训练集作为测试  防止出现过拟合问题
history = model.fit(x=train_samples,y=train_labels,validation_split=0.2,batch_size=64,epochs=2000,shuffle=True,verbose=2)
# mae = history.history['mae']
# val_mae = history.history['val_mae']
# loss = history.history['loss']
# val_loss = history.history['val_loss']
# epochs = range(1, len(loss) + 1)
# # acc = history.history['acc']  # 获取训练集准确性数据
# # val_acc = history.history['val_acc']  # 获取验证集准确性数据
# loss = history.history['loss']  # 获取训练集错误值数据
# val_loss = history.history['val_loss']  # 获取验证集错误值数据
# epochs = range(1, len(loss) + 1)
#
# plt.figure()  # 创建一个新的图表
# plt.plot(epochs, loss, 'bo', label='Trainning loss')
# plt.plot(epochs, val_loss, 'b', label='Vaildation loss')
# plt.legend()  ##绘制图例,即标明图中的线段代表何种含义
#
# plt.show()  # 显示所有图表
mae = history.history['mae']
val_mae = history.history['val_mae']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 1)
plt.figure()  # 创建一个新的图表
plt.plot(epochs, loss,  label='Trainning loss mse',color="blue",linewidth=0.3,linestyle='-.')
plt.plot(epochs, val_loss,  label='Vaildation loss mse',color="blue",linewidth=0.3,linestyle='--')
#预测

test_labels,test_samples = shuffle(test_labels,test_samples)
print(test_samples)
print(test_labels)
predictions = model.predict(x=test_samples,batch_size=32,verbose=0)
for i in predictions:
    print(i)

x = np.linspace(0,1000,800)
y1 = test_labels
y2 = predictions
plt.figure()
plt.plot(x,y1,label='test labels')
plt.plot(x,y2,label='predict labels',color="red",linewidth=1.0,linestyle='--')
plt.show()

?

?

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-08-03 17:22:11  更:2021-08-03 17:22:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 11:42:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码