李宏毅2022机器学习HW1收获

  1. colab的使用
  2. 把训练集取出一部分作为验证集
  3. 选择特征
  4. tqdm,tensorboard使用
  5. 在训练和验证时要关闭梯度计算
  6. 要把模型和数据放在同一个device上
  7. 保证模型可复现性
  8. pytorch和numpy在生成数组上的区别

一、colab的使用

1 把谷歌云盘mount到colab上面:

from google.colab import drive
drive.mount('/content/device')

/ 表示当前站点的根目录(域名映射的硬盘目录)

2魔法方法:
percentage (%) affects the process associated with the notebook, and it is called a magic command.

%cd /content/device/MyDrive/

Use % instead of ! for cd (change directory) command

3其他命令
exclamation mark (!) starts a new shell, does the operations, and
then kills that shell

二、从train_set提取做验证集

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))#返回的是dataset类,实现了__getitem__方法                                                                                                                           # fix the generator for reproducible results
    return np.array(train_set), np.array(valid_set)#必须得有这一步,否则返回的和列表差不多,无法选择特征(

三、选择特征

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[:,37:-1], valid_data[:,37:-1], test_data#所有行且37到倒数第二列

    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

四、tdqm,tensorboard使用

显示进度条

 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())

为什么要进入验证模式:
关闭batch_Norm和dropout
为什么要停止梯度计算
不需要更新模型,不需要求梯度了

六、模型和数据在同一个设备上

device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = My_Model(input_dim=x_train.shape[1]).to(device)
        for x, y in valid_loader:
            x, y = x.to(device), y.to(device)

七、保证模型可复现性

def same_seed(seed): 
    '''Fixes random number generator seeds for reproducibility.'''
    torch.backends.cudnn.deterministic = True#每次返回的卷积算法将是确定的
    torch.backends.cudnn.benchmark = False#A bool that, if True, causes cuDNN to benchmark multiple convolution algorithms and select the fastest.
    np.random.seed(seed)#保证每次生成随机数一样(可是np怎么确保torch)
    torch.manual_seed(seed)#设置生成随机数的种子
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)#为当前GPU设置生成随机数的种子

八、pytorch和numpy在生成数组上的区别

numpy可以用含有numpy数组的列表生成数组,但pytorch不可以
numpy可以用含有numpy数组的列表生成数组,但pytorch不可以

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值