使用Colab运行pytorch文件

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

使用Colab运行pytorch文件_第1张图片

你可能感兴趣的:(python,pytorch,深度学习,python)