# Numerical Operations
import math
import numpy as np
# Reading/Writing Data
import pandas as pd
import os
import csv
# For Progress Bar
from tqdm import tqdm
# Pytorch
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
# For plotting learning curve
from torch.utils.tensorboard import SummaryWriter
def same_seed(seed):
'''Fixes random number generator seeds for reproducibility.'''
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def train_valid_split(data_set, valid_ratio, seed):
'''Split provided training data into training set and validation set'''
valid_set_size = int(valid_ratio * len(data_set))
train_set_size = len(data_set) - valid_set_size
train_set, valid_set = random_split(data_set, [train_set_size, valid_set_size], generator=torch.Generator().manual_seed(seed))
return np.array(train_set), np.array(valid_set)
def predict(test_loader, model, device):
model.eval() # Set your model to evaluation mode.
preds = []
for x in tqdm(test_loader):
x = x.to(device)
with torch.no_grad():
pred = model(x)
preds.append(pred.detach().cpu())
preds = torch.cat(preds, dim=0).numpy()
return preds
class COVID19Dataset(Dataset):
'''
x: Features.
y: Targets, if none, do prediction.
'''
def __init__(self, x, y=None):
if y is None:
self.y = y
else:
self.y = torch.FloatTensor(y)
self.x = torch.FloatTensor(x)
def __getitem__(self, idx):
if self.y is None:
return self.x[idx]
else:
return self.x[idx], self.y[idx]
def __len__(self):
return len(self.x)
class My_Model(nn.Module):
def __init__(self, input_dim):
super(My_Model, self).__init__()
# TODO: modify model's structure, be aware of dimensions.
self.layers = nn.Sequential(
nn.Linear(input_dim, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 1)
)
def forward(self, x):
x = self.layers(x)
x = x.squeeze(1) # (B, 1) -> (B)
return x
def select_feat(train_data, valid_data, test_data, select_all=True):
'''Selects useful features to perform regression'''
y_train, y_valid = train_data[:,-1], valid_data[:,-1]
raw_x_train, raw_x_valid, raw_x_test = train_data[:,:-1], valid_data[:,:-1], test_data
if select_all:
feat_idx = list(range(raw_x_train.shape[1]))
else:
feat_idx = [0,1,2,3,4] # TODO: Select suitable feature columns.
return raw_x_train[:,feat_idx], raw_x_valid[:,feat_idx], raw_x_test[:,feat_idx], y_train, y_valid
def trainer(train_loader, valid_loader, model, config, device):
criterion = nn.MSELoss(reduction='mean') # Define your loss function, do not modify this.
# Define your optimization algorithm.
# TODO: Please check https://pytorch.org/docs/stable/optim.html to get more available algorithms.
# TODO: L2 regularization (optimizer(weight decay...) or implement by your self).
optimizer = torch.optim.SGD(model.parameters(), lr=config['learning_rate'], momentum=0.9)
writer = SummaryWriter() # Writer of tensoboard.
if not os.path.isdir('./models'):
os.mkdir('./models') # Create directory of saving models.
n_epochs, best_loss, step, early_stop_count = config['n_epochs'], math.inf, 0, 0
for epoch in range(n_epochs):
model.train() # Set your model to train mode.
loss_record = []
# tqdm is a package to visualize your training progress.
train_pbar = tqdm(train_loader, position=0, leave=True)
for x, y in train_pbar:
optimizer.zero_grad() # Set gradient to zero.
x, y = x.to(device), y.to(device) # Move your data to device.
pred = model(x)
loss = criterion(pred, y)
loss.backward() # Compute gradient(backpropagation).
optimizer.step() # Update parameters.
step += 1
loss_record.append(loss.detach().item())
# Display current epoch number and loss on tqdm progress bar.
train_pbar.set_description(f'Epoch [{epoch+1}/{n_epochs}]')
train_pbar.set_postfix({'loss': loss.detach().item()})
mean_train_loss = sum(loss_record)/len(loss_record)
writer.add_scalar('Loss/train', mean_train_loss, step)
model.eval() # Set your model to evaluation mode.
loss_record = []
for x, y in valid_loader:
x, y = x.to(device), y.to(device)
with torch.no_grad():
pred = model(x)
loss = criterion(pred, y)
loss_record.append(loss.item())
mean_valid_loss = sum(loss_record)/len(loss_record)
print(f'Epoch [{epoch+1}/{n_epochs}]: Train loss: {mean_train_loss:.4f}, Valid loss: {mean_valid_loss:.4f}')
writer.add_scalar('Loss/valid', mean_valid_loss, step)
if mean_valid_loss < best_loss:
best_loss = mean_valid_loss
torch.save(model.state_dict(), config['save_path']) # Save your best model
print('Saving model with loss {:.3f}...'.format(best_loss))
early_stop_count = 0
else:
early_stop_count += 1
if early_stop_count >= config['early_stop']:
print('\nModel is not improving, so we halt the training session.')
return
device = 'cuda' if torch.cuda.is_available() else 'cpu'
config = {
'seed': 5201314, # Your seed number, you can pick your lucky number. :)
'select_all': True, # Whether to use all features.
'valid_ratio': 0.2, # validation_size = train_size * valid_ratio
'n_epochs': 3000, # Number of epochs.
'batch_size': 256,
'learning_rate': 1e-5,
'early_stop': 400, # If model has not improved for this many consecutive epochs, stop training.
'save_path': './models/model.ckpt' # Your model will be saved here.
}
# Set seed for reproducibility
same_seed(config['seed'])
# train_data size: 2699 x 118 (id + 37 states + 16 features x 5 days)
# test_data size: 1078 x 117 (without last day's positive rate)
train_data, test_data = pd.read_csv('./covid.train.csv').values, pd.read_csv('./covid.test.csv').values
train_data, valid_data = train_valid_split(train_data, config['valid_ratio'], config['seed'])
# Print out the data size.
print(f"""train_data size: {train_data.shape}
valid_data size: {valid_data.shape}
test_data size: {test_data.shape}""")
# Select features
x_train, x_valid, x_test, y_train, y_valid = select_feat(train_data, valid_data, test_data, config['select_all'])
# Print out the number of features.
print(f'number of features: {x_train.shape[1]}')
train_dataset, valid_dataset, test_dataset = COVID19Dataset(x_train, y_train), \
COVID19Dataset(x_valid, y_valid), \
COVID19Dataset(x_test)
# Pytorch data loader loads pytorch dataset into batches.
train_loader = DataLoader(train_dataset, batch_size=config['batch_size'], shuffle=True, pin_memory=True)
valid_loader = DataLoader(valid_dataset, batch_size=config['batch_size'], shuffle=True, pin_memory=True)
test_loader = DataLoader(test_dataset, batch_size=config['batch_size'], shuffle=False, pin_memory=True)
model = My_Model(input_dim=x_train.shape[1]).to(device) # put your model and data on the same computation device.
trainer(train_loader, valid_loader, model, config, device)
def save_pred(preds, file)
李宏毅教授ML-2022Spring-HW1-SampleCode-TrainingData
需积分: 0 100 浏览量
更新于2023-01-17
收藏 1023KB ZIP 举报
《李宏毅教授2022春季机器学习课程——作业1样本代码与训练数据解析》
在信息技术领域,机器学习(Machine Learning, ML)作为一门关键的子领域,一直在推动着人工智能的发展。2022年春季,台湾大学的李宏毅教授开设了一门面向深度学习(Deep Learning)和机器学习的课程,旨在让学生掌握这一领域的核心概念和技术。本篇将详细探讨该课程的第一次作业,包括提供的Sample Code和训练数据。
作业1的资料中包含了两位助教的Sample Code,这无疑是学生们理解和实践机器学习算法的重要参考资料。Sample Code通常会展示如何运用编程语言(如Python)以及相关框架(如PyTorch)来实现机器学习模型。PyTorch是目前广泛使用的深度学习框架,以其灵活性和易用性受到开发者们的青睐。通过分析和理解这些代码,学生可以学习到如何构建神经网络、训练模型、优化参数以及评估模型性能等关键步骤。
训练数据是机器学习模型训练的基础,文件名为“NTU_ML_2022Spring_HW1”很可能包含了一系列用于训练模型的数据集。在实际应用中,数据集通常分为训练集、验证集和测试集。训练集用于训练模型,验证集用于调整模型参数,而测试集则用于最后的性能评估。对于机器学习初学者来说,理解数据预处理、特征工程以及数据划分的重要性是至关重要的。
在李宏毅教授的课程中,这份训练数据可能涵盖了各种机器学习问题,如分类、回归或者序列预测等。学生需要根据数据的特性选择合适的模型架构,比如使用卷积神经网络(CNN)处理图像数据,循环神经网络(RNN)处理时间序列数据,或者使用全连接网络(FCN)解决一般分类问题。同时,模型的训练过程中涉及的优化器选择(如SGD、Adam)、损失函数设计(如交叉熵损失、均方误差损失)以及超参数调优也是重点内容。
通过对Sample Code的深入剖析和对训练数据的合理利用,学生可以逐步掌握机器学习的基本流程,从数据预处理、模型构建、模型训练到模型评估。同时,这也有助于他们了解如何在实际问题中应用所学知识,提升解决问题的能力。因此,李宏毅教授的这项作业不仅是对理论知识的检验,更是对实践技能的锻炼,对于培养具有实战能力的机器学习工程师至关重要。
CLC-overflow
- 粉丝: 208
- 资源: 1
最新资源
- 基于遗忘因子递推最小二乘FFRLS与EKF算法的锂电池参数与状态联合SOC估计:算法介绍、参考文献及模型文档使用说明,基于遗忘因子递推最小二乘FFRLS和EKF的锂电池参数与状态联合SOC估计 1、采
- "基于HPPC工况二阶RC卡尔曼滤波EKF算法的锂电池SOC估计仿真研究:包含仿真数据与算法说明文档的详细分析",基于二阶RC卡尔曼滤波EKF的锂电池SOC估计仿真 仿真数据:HPPC工况,模型中自带
- 网上拍卖系统 免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程.zip
- 《基于Comsol仿真的热光伏电池(TPV)研究》,comsol仿真:热光伏电池(TPV) #comsol仿真 ,comsol仿真;热光伏电池(TPV);光伏模拟,"Comsol仿真技术在热光伏电池(
- 小区停车位管理系统 免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程.zip
- 养老院管理系统 免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程.zip
- 游乐园管理系统 免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程.zip
- 地下城堡_sign.apk
- **《三相静止无功发生器SVG并网仿真模型及结果分析(附详细31页Word报告)》**,三相静止无功发生器SVG并网仿真模型(附说明报告) 仿真带一份与仿真完全对应的31页Word报告可结合仿真快速入
- "comsol模拟下的双裂纹损伤扩展与压裂行为研究",comsol 双裂纹损伤扩展 压裂 ,comsol;双裂纹损伤;损伤扩展;压裂;,"Comsol模拟双裂纹损伤扩展及压裂行为研究"
- 光伏储能系统三相PQ恒功率并网控制策略仿真研究-基于双闭环控制与MPPT算法的优化实践(附参考文献及文档),光伏储能三相PQ恒功率并网控制仿真(附参考文献及文档) ①网侧:采用PQ恒功率控制,参考文
- "COMSOL仿真分析电缆局部放电超声波传播特性及影响因素研究",comsol仿真电缆局部放电产生的超声波在电缆中传播特征 ,仿真; 局部放电; 超声波; 传播特征; 电缆;,COMSOL仿真电缆局部
- COMSOL软件多物理场仿真教程:电热力耦合传热及微波加热优化实践,comsol软件教程,电热力耦合,动网格,传热,优化,微波加热,压电(非comsol官网搬运) comsol仿真教程,多物理场,建模
- "Comsol 6.2版本流固耦合注浆分析:融入达西定律与固体力学模块的渗流场与结构场耦合实践案例",Comsol流固耦合注浆及冒浆分析(6.2版本) 采用其中达西定律模块及固体力学模块,通过建立质量
- 《TC3XX Autosar系统中文配置手册:涵盖19个模块的详细指南与联系信息》,tc3xx autosar EB中文配置手册,需要联系 一共有大约19个模块 ,tc3xx;autosar;E
- 基于改进滑模观测器的永磁同步电机无传感器控制:降低抖振,提高转子速度与位置估计精度的新方法,基于smo的永磁同步电机无传感器控制 为了减少传统滑模观测器(SMO)中由于控制函数不连续而引起的抖振,提出