python常用代碼


常用代碼片段及技巧

自動選擇GPU和CPU

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# model and tensor to device
vgg = models.vgg16().to(device)

切換當前目錄

import os
try:
	os.chdir(os.path.join(os.getcwd(), '..'))
	print(os.getcwd())
except:
	pass

臨時添加環境目錄

import sys
sys.path.append('引用模塊的地址')
print(sys.path)

打印模型參數

from torchsummary import summary
# 1 means in_channels
summary(model, (1, 28, 28))

將tensor的列表轉換為tensor

x = torch.stack(tensor_list)

內存不夠

  • Smaller batch size
  • torch.cuda.empty_cache()every few minibatches
  • 分布式計算
  • 訓練數據和測試數據分開
  • 每次用完之后刪去variable,采用del x

debug tensor memory

resource` module is a Unix specific package as seen in https://docs.python.org/2/library/resource.html which is why it worked for you in Ubuntu, but raised an error when trying to use it in Windows.

Here is what solved it for me.

  1. Downgrade to the Apache Spark 2.3.2 prebuild version
  2. Install (or downgrade) jdk to version 1.8.0
    • My installed jdk was 1.9.0, which doesn't seem to be compatiable with spark 2.3.2 or 2.4.0
  3. make sure that when you run java -version in cmd (command prompt), it show java version 8. If you are seeing version 9, you will need to change your system ENV PATH to ensure it points to java version 8.
  4. Check this link to get help on changing the PATH if you have multiple java version installed.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def debug_memory():
    import collections, gc, resource, torch
    print('maxrss = {}'.format(
        resource.getrusage(resource.RUSAGE_SELF).ru_maxrss))
    tensors = collections.Counter((str(o.device), o.dtype, tuple(o.shape))
                                  for o in gc.get_objects()
                                  if torch.is_tensor(o))
    for line in sorted(tensors.items()):
        print('{}\t{}'.format(*line))
        
        
 # example
import tensor
 x = torch.tensor(3,3)
 debug_memory()
 
 y = torch.tensor(3,3)
 debug_memory()
 
 z = [torch.randn(i).long() for i in range(10)]
 debug_memory()

10-18-2019


Matlab繪虛線圖

%matplotlib inline
from matplotlib import pyplot as plt
from IPython import display
import torch
import math

x = torch.arange(-7, 7, 0.01)
# Mean and variance pairs
parameters = [(0,1), (0,2), (3,1)]

# Display SVG rather than JPG
display.set_matplotlib_formats('svg')
plt.figure(figsize=(10, 6))
for (mu, sigma) in parameters:
    p = (1/math.sqrt(2 * math.pi * sigma**2)) * torch.exp(-(0.5/sigma**2) * (x-mu)**2)
    plt.plot(x.numpy(), p.numpy(), label='mean ' + str(mu) + ', variance ' + str(sigma))
plt.axhline(y=0, color='black', linestyle='dashed')
plt.legend()
plt.show()

loss訓練代碼(訓練集與驗證集)

lr = 0.03  # Learning rate
num_epochs = 3  # Number of iterations
net = linreg  # Our fancy linear model
loss = squared_loss  # 0.5 (y-y')^2

for epoch in range(num_epochs):
    # Assuming the number of examples can be divided by the batch size, all
    # the examples in the training data set are used once in one epoch
    # iteration. The features and tags of mini-batch examples are given by X
    # and y respectively
    for X, y in data_iter(batch_size, features, labels):
        l = loss(net(X, w, b), y)  # Minibatch loss in X and y
        l.mean().backward()  # Compute gradient on l with respect to [w,b]
        sgd([w, b], lr, batch_size)  # Update parameters using their gradient
    with torch.no_grad():
        train_l = loss(net(features, w, b), labels)
        print('epoch %d, loss %f' % (epoch + 1, train_l.mean().numpy()))

保存最佳模型

def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
    since = time.time() # 計時開始

    best_model_wts = model.state_dict() # 讀取訓練好的模型權重
    best_acc = 0.0

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # 每個epoch中游訓練和驗證部分
        for phase in ['train', 'val']:
            if phase == 'train':
                scheduler.step()
                model.train(True)  
            else:
                model.train(False)  

            running_loss = 0.0
            running_corrects = 0


            for data in dataloaders[phase]:

                inputs, labels = data

                # 如果使用GPU,則使用Variable
                if use_gpu:
                    inputs = Variable(inputs.cuda())
                    labels = Variable(labels.cuda())
                else:
                    inputs, labels = Variable(inputs), Variable(labels)

                # 初始化梯度值
                optimizer.zero_grad()

                # 前向
                outputs = model(inputs)
                _, preds = torch.max(outputs.data, 1)
                loss = criterion(outputs, labels)

                # 后向,如果為訓練集則進行梯度優化
                if phase == 'train':
                    loss.backward()
                    optimizer.step()

                # 統計損失
                running_loss += loss.data[0]
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / dataset_sizes[phase]
            epoch_acc = running_corrects / dataset_sizes[phase]

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                phase, epoch_loss, epoch_acc))

            # 深度復制該模型
            if phase == 'val' and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = model.state_dict()

        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_acc))

    # 載入最佳的模型
    model.load_state_dict(best_model_wts)
    return model


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM