使用Colab运行pytorch文件

本文介绍了使用PyTorch和BERT进行深度学习模型训练的过程,展示了如何在Colab环境中设置环境并绘制训练和验证的损失与准确率曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#numpy报错改这行return self.cpu().detach().numpy()
#1. 查看pytorch版本:
import torch
print(torch.__version__)
#2.查看cuda版本:
print(torch.version.cuda)

#看gpu
!/opt/bin/nvidia-smi
#挂硬盘
from google.colab import drive
drive.mount('/content/drive/')
#重启
import os
os.kill(os.getpid(), 9)

#!pip install torch==1.10.2 torchvision==0.11.3 -f https://download.pytorch.org/whl/torch_stable.html
!pip install transformers==3.4.0
!pip install numpy==1.19
!pip install matplotlib==2.2.4


%run "/content/drive/MyDrive/model/nlp/bert_model.py"
#或者
!python "/content/drive/MyDrive/model/nlp/bert_model.py"
#画图
import matplotlib.pyplot as plt


epoches_list=[1,2,3,4]
loss_list=[0.85896,0.50016,0.38255,0.31071]
val_loss_list=[20.46452309191227,18.75466927140951,16.660616226494312,16.67046620696783]
Train_acc_list=[0.72114,0.85610,0.88537,0.90488]
val_acc_list=[0.8538961038961039,0.8571428571428571,0.8668831168831169,0.8766233766233766]

plt.plot(epoches_list, loss_list, c='red', label="loss")
plt.plot(epoches_list, val_loss_list, c='green', linestyle='--', label="val_loss")
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['Train','Valid'],loc='upper left')
plt.show()
#plt.subplot(212)
plt.plot(epoches_list, Train_acc_list, c='red')
plt.plot(epoches_list, val_acc_list, c='green', linestyle='--')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['Train_acc', 'Valid_acc'], loc='upper left')
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值