深度学习平台

news2024/11/25 14:39:33

深度学习平台(只能有线连接校园网)

账户

yeguifeng

密码

yguifeng

env3 : pytorch1.9+cuda+jupyter+matplotlib
pycuda: pycuda
Timesformer

–data_dir ./trainingVideo --category my_fire --output_dir ./output --image_size 100 --num_chain 10 --batch_size 14 --lr 0.001 --num_frames 30

export LD_LIBRARY_PATH=$LDLIBRARY_PATH:/usr/local/cuda-9.0/lib64
export PATH=$PATH:/usr/local/cuda-9.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-9.0

使用指南

因为这个设备的docker环境无法在线或者本地安装环境。所以,所有的相关的包都应该在自己的电脑上装好,并且一定要装 jupyter,不然用起来不方便。

1.创建镜像

创建基础的镜像,比如我自己来说需要用到pytorch

  1. 拉取镜像
sudo docker pull pytorch/pytorch:1.9.0-cuda10.2-cudnn7-runtime
sudo docker pull pytorch/pytorch:1.2-cuda10.0-cudnn7-devel
  1. 查看镜像列表,拉取成功
sudo docker images
REPOSITORY          TAG                             IMAGE ID       CREATED         SIZE
jupyterv2/pytorch   latest                          589b7f07ccc4   17 hours ago    3.58GB
jupyter             latest                          6ebc0b4666fe   1RuntimeError: CUDA error: out of memory CUDA kernel errors might be asynchro7 hours ago    3.58GB
ubuntu              latest                          a8780b506fa4   6 days ago      77.8MB
nvidia/cuda         10.0-cudnn7-devel-ubuntu18.04   ab9007c84133   5 months ago    3.21GB
python              3.6                             54260638d07c   10 months ago   902MB
hello-world         latest                          feb5d9fea6a5   13 months ago   13.3kB
pytorch/pytorch     1.9.0-cuda10.2-cudnn7-runtime   3850639cdf7a   17 months ago   4.42GB
pytorch/pytorch     1.6.0-cuda10.1-cudnn7-runtime   6a2d656bcf94   2 years ago     3.47GB

  1. 运行镜像
sudo docker run -it pytorch/pytorch:1.9.0-cuda10.2-cudnn7-runtime

打开成功会这样显示

root@92ede4d54808:/workspace#
  1. 安装相关的库
root@92ede4d54808:/workspace# pip install jupyter

安装成功提示

  Attempting uninstall: traitlets
    Found existing installation: traitlets 5.0.5
    Uninstalling traitlets-5.0.5:
      Successfully uninstalled traitlets-5.0.5
  Attempting uninstall: ipython
    Found existing installation: ipython 7.22.0
    Uninstalling ipython-7.22.0:
      Successfully uninstalled ipython-7.22.0

Successfully installed Send2Trash-1.8.0 anyio-3.6.2 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 attrs-22.1.0 bleach-5.0.1 debugpy-1.6.3 defusedxml-0.7.1 entrypoints-0.4 fastjsonschema-2.16.2 importlib-metadata-5.0.0 importlib-resources-5.10.0 ipykernel-6.16.2 ipython-7.34.0 ipywidgets-8.0.2 jsonschema-4.17.0 jupyter-1.0.0 jupyter-client-7.4.4 jupyter-console-6.4.4 jupyter-core-4.11.2 jupyter-server-1.23.0 jupyterlab-pygments-0.2.2 jupyterlab-widgets-3.0.3 matplotlib-inline-0.1.6 mistune-2.0.4 nbclassic-0.4.8 nbclient-0.7.0 nbconvert-7.2.3 nbformat-5.7.0 nest-asyncio-1.5.6 notebook-6.5.2 notebook-shim-0.2.2 packaging-21.3 pandocfilters-1.5.0 pkgutil-resolve-name-1.3.10 prometheus-client-0.15.0 pyparsing-3.0.9 pyrsistent-0.19.2 python-dateutil-2.8.2 pyzmq-24.0.1 qtconsole-5.4.0 qtpy-2.3.0 sniffio-1.3.0 terminado-0.17.0 tinycss2-1.2.1 tornado-6.2 traitlets-5.5.0 webencodings-0.5.1 websocket-client-1.4.2 widgetsnbextension-4.0.3 zipp-3.10.0

测试能否打开

root@92ede4d54808:/workspace# jupyter notebook --allow-root
[I 02:05:39.087 NotebookApp] Serving notebooks from local directory: /workspace
[I 02:05:39.087 NotebookApp] Jupyter Notebook 6.5.2 is running at:
[I 02:05:39.087 NotebookApp] http://localhost:8888/?token=88f4ced3086866824ee9755f9aa33338ce8428871a1e92cb
[I 02:05:39.087 NotebookApp]  or http://127.0.0.1:8888/?token=88f4ced3086866824ee9755f9aa33338ce8428871a1e92cb
[I 02:05:39.087 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 02:05:39.093 NotebookApp] No web browser found: could not locate runnable browser.
[C 02:05:39.093 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-19-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=88f4ced3086866824ee9755f9aa33338ce8428871a1e92cb
     or http://127.0.0.1:8888/?token=88f4ced3086866824ee9755f9aa33338ce8428871a1e92cb

成功打开!

root@92ede4d54808:/workspace# python
Python 3.7.10 (default, Feb 26 2021, 18:47:35) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torvision
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torvision'
>>> import torchvision
>>> import torch
>>> from torch.utils.data import DataLoader
>>> from tqdm import tqdm
>>> import copy
>>> import random

runtest:

import numpy as np
from sklearn import datasets

import torch
import torch.nn as nn

import torch.optim as optim

import torchbnn as bnn
import matplotlib.pyplot as plt
import torch.nn.functional as F
import torchvision
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset

model_o = nn.Sequential(
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=28*28, out_features=1024),
    nn.ReLU(),
    bnn.BayesLinear(prior_mu=0, prior_sigma=0.1, in_features=1024, out_features=10),
)
batch_size = 60000
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'  # 0.001
print("device: ", device)
import copy
from tqdm import tqdm

# get data
def get_data():
    train = torchvision.datasets.MNIST(root="./data", train=True, download=True,
                                       transform=torchvision.transforms.Compose([
                                           torchvision.transforms.ToTensor(), # to tensor
                                           torchvision.transforms.Normalize((0.1307,), (0.3081,))  # standardization
                                       ]))
    train_loader = DataLoader(train, batch_size=batch_size)  
    test = torchvision.datasets.MNIST(root="./data", train=False, download=True,
                                      transform=torchvision.transforms.Compose([
                                          torchvision.transforms.ToTensor(),  # to tensor
                                          torchvision.transforms.Normalize((0.1307,), (0.3081,))  # standardization
                                      ]))
    test_loader = DataLoader(test, batch_size=batch_size)  
    return train_loader, test_loader
train_loader, test_loader = get_data()
for _, (X, y) in enumerate(train_loader):
    X = X.reshape(batch_size,-1).to(device)
    y = y.to(device)
for _, (x_test,y_test) in enumerate(test_loader):
    x_test = x_test.reshape(10000,-1).to(device)
    y_test = y_test.to(device)
class bnnHmc():
    def __init__(self,net,alpha):
        super().__init__()
        self.net = net
        self.alpha = alpha
        self.d = sum(p.numel() for p in self.net.parameters())
        self.loss = torch.nn.CrossEntropyLoss().to(device)
        self.loss_list = []
        self.train_acc = []
        self.test_acc = []

    def step(self, s, path_len=0.001, step_size=0.1):
        # Step 1:
        proposal_net = copy.deepcopy(self.net)  # Copy the sample to put the sample after iteration
        p_old = torch.randn(self.d).to(device) * 0.0005  # Randomly choose the original speed
        p_new = copy.deepcopy(p_old).to(device)  # Randomly choose a new speed
        # Calculate potential energy U(x)=-log(p(x))
        yhat = self.net(X)
        x_0_nlp = - self.loss(yhat, y)
        # Calculate kinetic energy
        p_0_nlp = (p_old * p_old).sum() / 2
        H_0 = p_0_nlp + x_0_nlp

        x_0_nlp.backward()
        du_dx = torch.tensor([]).to(device)
        for i in self.net.parameters():
            i = i.grad.reshape(-1)
            shape = i.shape[0]
            du_dx = torch.cat([du_dx, i.reshape(shape, 1)])

        du_dx = du_dx.reshape(self.d)
        # leapfrog dynamic iteration
        # 1. P take a half step
        p_new += step_size * du_dx / 2  # as potential energy increases, kinetic energy

        # 2. Parameters go one step
        sum = 0
        for i, j in zip(proposal_net.parameters(), range(self.d)):
            size = i.numel()
            i.data += step_size * p_new[sum:sum + size].reshape(i.data.shape)
            sum += size
        # 3.Update the parameters required in the second half of the step
        yhat = proposal_net(X)
        x_1_nlp = - self.loss(yhat, y)
        x_1_nlp.backward()
        du_dx = torch.tensor([]).to(device)
        for i in proposal_net.parameters():
            i = i.grad.reshape(-1)
            shape = i.shape[0]
            du_dx = torch.cat([du_dx, i.reshape(shape, 1)])
        # 4. take half a step
        du_dx = du_dx.reshape(self.d)
        p_new += step_size * du_dx.reshape(self.d) / 2  # second half-step "leapfrog" update to momentum

        p_1_nlp = (p_new * p_new).sum() / 2
        yhat = proposal_net(X)
        x_1_nlp = - self.loss(yhat, y)
        H_1 = x_1_nlp + p_1_nlp
        acceptance = torch.exp((- H_0 + H_1) * 1000)
        rand = torch.rand(1)[0].to(device)
        if acceptance > rand:
            self.net = proposal_net
            self.loss_list.append(-x_1_nlp.data)
        else:
            self.loss_list.append(- x_0_nlp.data)
        if s % 50 == 0:
            print("epoch",s,"loss = ",-x_1_nlp)
            correct = (yhat.argmax(1) == y).type(torch.float).sum().item()
            self.train_acc.append(correct / 60000 )
            print("train_acc:",correct / 60000)
            correct = (proposal_net(x_test).argmax(1) == y_test).type(torch.float).sum().item()
            self.test_acc.append(correct / 10000 )
            print("test_acc:",correct / 10000)

    def fit(self, num_steps=1000):
        trajectory = []
        for s in tqdm(range(num_steps)):
            self.step(s)
            # 记录每个参数的值
            parameters = torch.cat([param.view(-1) for param in self.net.parameters()])[:100]
            trajectory.append(parameters.cpu().detach().numpy().tolist())
        return np.array(self.loss_list), np.array(self.train_acc), np.array(self.test_acc),np.array(trajectory)
model_hmc = copy.deepcopy(model_o)
model_hmc = model_hmc.to(device)
alpha = 0.001
num_steps = 30000
trainer = bnnHmc(model_hmc, alpha=alpha)
loss,train_acc,test_acc,samples = trainer.fit(num_steps=num_steps)

# save data
np.save('mnist_SPhmc_loss'+'.npy', loss)
np.save('mnist_SPhmc_train_acc'+'.npy', train_acc)
np.save('mnist_SPhmc_test_acc'+'.npy', test_acc)
np.save('mnist_SPhmc_samples'+'.npy', samples)
  1. 从容器创建一个新的镜像
sudo docker commit 92ede4d54808 ygf_pytorch1.9.0
  1. 查看镜像列表,有ygf_pytorch1.9.0表示创建成功
sudo docker images
REPOSITORY          TAG                             IMAGE ID       CREATED         SIZE
ygf_pytorch1.9.0    latest                          9d1753a8697d   8 seconds ago   4.53GB
jupyterv2/pytorch   latest                          589b7f07ccc4   17 hours ago    3.58GB
jupyter             latest                          6ebc0b4666fe   17 hours ago    3.58GB
ubuntu              latest                          a8780b506fa4   6 days ago      77.8MB
nvidia/cuda         10.0-cudnn7-devel-ubuntu18.04   ab9007c84133   5 months ago    3.21GB
python              3.6                             54260638d07c   10 months ago   902MB
hello-world         latest                          feb5d9fea6a5   13 months ago   13.3kB
pytorch/pytorch     1.9.0-cuda10.2-cudnn7-runtime   3850639cdf7a   17 months ago   4.42GB
pytorch/pytorch     1.6.0-cuda10.1-cudnn7-runtime   6a2d656bcf94   2 years ago     3.47GB

  1. 保存镜像为.tar格式,并修改权限,不然不能打开也无法上传到服务器
ygf@ygf:~$ sudo docker save -o ygf_pytorch1_9_0.tar ygf_pytorch1.9.0:latest 
ygf@ygf:~$ sudo chmod 777 ygf_pytorch1_9_0.tar 

2. 上传到服务器

  1. Images ->import

在这里插入图片描述
2. 选择之前的文件,在这里插入图片描述
3. 查看镜像库
在这里插入图片描述
说明导入成功。

3. 创建新的Container

  1. Containers->Add container
    在这里插入图片描述

  2. Container初始化
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  3. 查看container
    在这里插入图片描述
    都没错。

4. 运行

  1. 打开终端

    在这里插入图片描述

  2. 打开jupyter

jupyter notebook --ip 0.0.0.0 --no-browser --allow-root

在这里插入图片描述
3. 远程访问jupyter

http://127.0.0.1:8888/?token=b939a51424abb997922495d813a62103fef9de8151d3efa7
改为
http://172.20.67.216:19981/?token=b939a51424abb997922495d813a62103fef9de8151d3efa7
# http://172.20.67.216:35545/?token=23ed775cc21a3caf096a0f470f3ce797e4dc5a9e7bd79b5d
# http://172.20.67.216:23187/?token=?e6ccc855e0c78a9846daf89ccb6c851315e2d53d511fe0e2
# http://172.20.67.216:36544/?token=eb8d2584c37d52b6dd06cb28884ac59b137aef306d13aeb2

修改方式如下:

访问成功!
在这里插入图片描述


import copy
import numpy
import matplotlib.pyplot as plt
import altair as alt
import pandas as pd
import scipy.stats
import numpy as np
from scipy.stats import norm
from scipy import stats
from tqdm import tqdm
import math
import time 
import numpy as np
from pycuda import autoinit
import pycuda.gpuarray as gpuarray
import numpy as np
from pycuda.compiler import SourceModule
import pycuda.driver as drv

import tensorflow as tf
import tensorflow_probability as tfp

N = 500
beta0 = -1.0
beta_true = 2.0
sigma_true = 0.5

X = np.random.uniform(-1,1,N).astype(numpy.float32)
Y = beta0 + X *beta_true + sigma_true*np.random.normal(loc=0, scale=1, size=N) 
Y = Y.astype(numpy.float32)
data = {'x' : X,'y' : Y}

# Define the CUDA kernel for log likelihood computation
# 似然计算函数 GPU版本
mhmod = SourceModule(
"""
#include <cmath>
__global__ void log_likelihood_kernel(float *x, float *y, float *net, float *result,int data_num) {
    const int idx = threadIdx.x;
    const float MPI = 3.14159265359;
    for(int i=0;i<data_num;i++){
        float y_hat = net[0] + net[1] * x[i];
        double temp = (y[i] - y_hat) / net[2];
        result[i] = -0.5 * log(2 * MPI * net[2] * net[2]) - 0.5 * temp * temp;
    }
    
}
"""
)
pmod = SourceModule(
"""
#include <cmath>
__global__ void log_likelihood_kernel(float *x, float *y, float *nets, float *result,int num_nets,int data_num, int nets_num, float *trans_data) {
    const int idx = threadIdx.x;
    const float MPI = 3.14159265359;
    for(int i=0;i<data_num;i++){
        float y_hat = nets[idx * num_nets] + nets[idx * num_nets + 1] * x[i];
        double temp = (y[i] - y_hat) / nets[idx * num_nets+2];
        result[idx *data_num +i] = -0.5 * log(2 * MPI * nets[idx * num_nets+2] * nets[idx * num_nets+2]) - 0.5 * temp * temp;
    }
    for(int i=0;i<nets_num;i++){
        if(idx != i){
            for(int j=0;j<num_nets;j++){
            double temp = nets[idx*num_nets + j] - nets[i*num_nets+j];
            trans_data[idx] += -0.5 * log(2 * MPI) - 0.5 * temp * temp;
            }
        }
    }
    
}
"""
)

# Get the CUDA kernel function
muti_loglik_cuda = pmod.get_function("log_likelihood_kernel")

def muti_loglik_pycuda(data, nets):
    # Allocate memory for the result on the GPU
    result = np.zeros(nets.shape[0]*data['x'].shape[0]).astype(numpy.float32)
    nets_num = nets.shape[0]
    nets_size = nets.shape[1]
    nets = nets.reshape(-1)
    data_num = data['x'].shape[0]
    trans_data = np.zeros(nets_num).astype(numpy.float32)
    # Run the CUDA kernel
    muti_loglik_cuda(drv.In(data['x']), drv.In(data['y']), drv.In(nets), drv.Out(result),np.int32(nets_size),np.int32(data_num),np.int32(nets_num),drv.Out(trans_data),block=(nets_num,1, 1), grid=(1, 1))
    return np.sum(result.reshape([nets_num,-1]),axis=1)/ data["y"].shape[0] * 50+trans_data

# Get the CUDA kernel function
log_likelihood_cuda = mhmod.get_function("log_likelihood_kernel")

def loglik_pycuda(data, net):
    # Allocate memory for the result on the GPU
    result = np.zeros_like(data['x'])
    # Set up grid and block dimensions
    data_num = data['x'].shape[0]
    
    # Run the CUDA kernel
    log_likelihood_cuda(drv.In(data['x']), drv.In(data['y']), drv.In(net), drv.Out(result),np.int32(data_num),block=(1,1, 1), grid=(1, 1))
    # Return the mean log likelihood
    return np.sum(result) / data["y"].shape[0] * 50
# Define the CUDA kernel for log likelihood computation
# 似然计算函数 GPU版本



#result[idx] = -0.5 * log(2 * MPI * net[2] * net[2]) - 0.5 * temp * temp;
# Compile the CUDA kernel
pmpmod = SourceModule(
"""
#include <cmath>
__global__ void log_likelihood_kernel(float *x, float *y, float *nets, float *result,int num_nets,int data_num,float *tran_table,float *trans_values, int deep) {
    const int idx = threadIdx.x;
    const float MPI = 3.14159265359;
    trans_values[idx] = 0;
    for(int i=0;i<data_num;i++){
        float y_hat = nets[idx * num_nets] + nets[idx * num_nets + 1] * x[i];
        double temp = (y[i] - y_hat) / nets[idx * num_nets+2];
        result[idx *data_num +i] = -0.5 * log(2 * MPI * nets[idx * num_nets+2] * nets[idx * num_nets+2]) - 0.5 * temp * temp;
    }
    
    for(int d=0; d<deep; d++){
        int net_from_index = tran_table[idx*deep*2+d*2];
        int net_to_index = tran_table[idx*deep*2+d*2+1]; 
        for(int j=0;j<num_nets;j++){
            double temp = nets[net_from_index*num_nets+j] - nets[net_to_index*num_nets+j];
            trans_values[idx] += -0.5 * log(2 * MPI) - 0.5 * temp * temp;
        }
    }    
   
}
"""
)

# Get the CUDA kernel function
muti_loglik_cuda_pmp = pmpmod.get_function("log_likelihood_kernel")

def muti_loglik_pycuda_pmp(data, nets, tran_table):
    # Allocate memory for the result on the GPU
    result = np.zeros(nets.shape[0]*data['x'].shape[0]).astype(numpy.float32)
    nets_num = nets.shape[0]
    nets_size = nets.shape[1]
    nets = nets.reshape(-1)
    tran_table = tran_table.reshape(-1).astype(numpy.float32)
    data_num = data['x'].shape[0]
    trans_values = np.zeros(nets_num).astype(numpy.float32)
    deep = np.log2(nets_num)
    
    # Run the CUDA kernel
    muti_loglik_cuda_pmp(drv.In(data['x']), drv.In(data['y']), drv.In(nets), drv.Out(result),np.int32(nets_size),np.int32(data_num),drv.In(tran_table),drv.Out(trans_values),np.int32(deep),block=(nets_num,1, 1), grid=(1, 1))
    return np.sum(result.reshape([nets_num,-1]),axis=1)/ data["y"].shape[0] * 50+trans_values

            

class MHtrain():
    def __init__(self, net, alpha):
        super().__init__()
        self.net = net.astype(numpy.float32)
        self.alpha = alpha
        self.d = self.net.shape[0]
    def updata(self,net):
        rand_step = np.random.normal(0, self.alpha, net.shape).astype(numpy.float32)
        return rand_step + net

    def step(self, data, function):
        new_net = self.updata(self.net)
       
        ratio = math.exp(function(data, new_net) - function(data, self.net))

        random_number = np.random.rand()
        if(random_number<ratio):
            self.net = new_net

    def fit(self, data=None, num_steps=1000, function = loglik_pycuda):
        parameter_trace = np.empty([num_steps, self.d])
        t = time.time()
        dalte_time = 1
        time_trace = []
        index_begin = 0
        ess_list = []
        for s in tqdm(range(num_steps)):
            self.step(data, function)
            if time.time()-t>dalte_time:
                t = time.time()
                index_end = s
                parameters = parameter_trace[index_begin:index_end,0]
                ess = tfp.mcmc.effective_sample_size(tf.convert_to_tensor(parameters))
                ess_list.append(ess)
                time_trace.append(self.net)
                index_begin = s

            parameter_trace[s:(s + 1),:] = self.net
        return parameter_trace,np.array(ess_list)

net_init = np.array([1,1,1]).astype(numpy.float32)
class MPtrain():
    def __init__(self, net, alpha, N):
        super().__init__()
        self.net = net.astype(numpy.float32)
        self.alpha = alpha
        self.N = N
        self.d = self.net.shape[0]
    def updata(self,net):
        rand_step = np.random.normal(0, self.alpha, net.shape).astype(numpy.float32)
        return rand_step + net
    def log_trans_prob(self, net, net_star):
        log_trans_prob = np.array([0.0])
        len = net.shape[0]
        for i in range(len):
            log_trans_prob += np.log(stats.norm.pdf(net[i],net_star[i]))
        return log_trans_prob

    def step(self, data, nets, function):
        # Step 1:
        # 计算接受率(可并行)
        A = np.empty([self.N+1 , 1])
        K = np.empty([self.N+1 , 1])
       
        
        A[:, 0] = function(data,nets)
        

        # 根据接受率采样
        A = (A - np.mean(A)) / np.std(A)
        B = pd.DataFrame(np.exp(A).reshape(-1))
        index = pd.DataFrame(np.linspace(0, self.N, self.N + 1).astype(np.int32))
        index_weight = index.sample(self.N+1,replace=True, weights=B[0]).values.reshape(-1)
     
        new_proposal_nets = copy.deepcopy(nets)
        for i,j in zip(index_weight,index.values.reshape(-1)):
            new_proposal_nets[j] = nets[i]
        
        I = np.random.choice(np.linspace(0, self.N, self.N + 1).astype(np.int32), 1)
        self.net = new_proposal_nets[I[0]]
       
        return new_proposal_nets

    def fit(self, data=None, num_steps=1000, function = muti_loglik_pycuda):
        parameter_trace = np.empty([num_steps*(self.N+1), self.d])
        nets = np.empty([self.N+1, self.d]).astype(numpy.float32)
        t = time.time()
        dalte_time = 1
        time_trace = []
        ess_list = []
        index_begin = 0
        for s in tqdm(range(num_steps)):
            # 1.产生建议参数
            nets[0] = self.net
            for i in range(1,self.N+1):
                nets[i] = self.updata(nets[0])
            # 2.优化采样
            new_proposal_nets = self.step(data,nets,function)
            
            if (time.time()-t>dalte_time):
                t = time.time()
                index_end = s
                parameters = parameter_trace[index_begin*(self.N+1):index_end*(self.N+1),0]
                ess = tfp.mcmc.effective_sample_size(tf.convert_to_tensor(parameters))
                ess_list.append(ess)
                time_trace.append(self.net)
                index_begin = s

            parameter_trace[s*(self.N+1):(s + 1)*(self.N+1),:] = new_proposal_nets
        return parameter_trace,np.array(ess_list)

# GPU 
# ess_data = []
# Ns = [3,7,15,31,63,127]
# for N in Ns:
#     print(N)
#     train = MPtrain(net_init,alpha = 0.02, N=N)
#     parameter_trace,ess_list = train.fit(ess_data,10000,function=muti_loglik_pycuda)
#     ess_data.append(ess_list[-10:])
# np.save("MP_3_7_15_31_63_127",np.array(ess_data))

# sample_num = parameter_trace.shape[0]
# line = np.linspace(0,sample_num,sample_num)
# for i in range(train.d):
#     plt.plot(line,parameter_trace[:,i],label="par"+str(i))
# plt.show()

# plt.boxplot(np.array(data))
class PMPtrain():
    def __init__(self, net, alpha, N):
        super().__init__()
        self.net = net.astype(numpy.float32)
        self.alpha = alpha
        self.N = N
        self.d = self.net.shape[0]
    def updata(self,net):
        rand_step = np.random.normal(0, self.alpha, net.shape).astype(numpy.float32)
        return rand_step + net
    def log_trans_prob(self, net, net_star):
        log_trans_prob = np.array([0.0])
        len = net.shape[0]
        for i in range(len):
            log_trans_prob += np.log(stats.norm.pdf(net[i],net_star[i]))
        return log_trans_prob

    def step(self, data, nets,tran_table, function):
        # Step 1:
        # 计算接受率(可并行)
        tree_deep = math.log2(self.N+1)
        A = np.empty([self.N+1 , 1])
        K = np.empty([self.N+1 , 1])
        
       
        A[:, 0] = function(data,nets,tran_table)
        
        # 根据接受率采样
        A = (A - np.mean(A)) / np.std(A)
        B = pd.DataFrame(np.exp(A).reshape(-1))
        index = pd.DataFrame(np.linspace(0, self.N, self.N + 1).astype(np.int32))
        index_weight = index.sample(self.N+1,replace=True, weights=B[0]).values.reshape(-1)
     
        new_proposal_nets = copy.deepcopy(nets)
        for i,j in zip(index_weight,index.values.reshape(-1)):
            new_proposal_nets[j] = nets[i]
        
        I = np.random.choice(np.linspace(0, self.N, self.N + 1).astype(np.int32), 1)
        self.net = new_proposal_nets[I[0]]
        return new_proposal_nets
        
      

    def fit(self, data=None, num_steps=1000, function = muti_loglik_pycuda_pmp):
        parameter_trace = np.empty([num_steps *(self.N+1), self.d])
        nets = np.empty([self.N+1, self.d]).astype(numpy.float32)
        tree_deep = math.log2(self.N+1)
        tran_table = np.ones([self.N+1,int(tree_deep),2]).astype(np.int32())*-1
        t = time.time()
        dalte_time = 1
        time_trace = []
        index_begin = 0
        ess_list = []
        for s in tqdm(range(num_steps)):
            # 1.产生建议参数
            nets[0] = self.net
            for i in range(int(tree_deep)):
                j = int(math.pow(2,i))
                for k in range(int(j)):
                    nets[k+j] = copy.deepcopy(self.updata(nets[k]))
                    tran_table[k][i] = np.array([k,k+j])
                    tran_table[k+j][i] = np.array([k+j,k])
                    if i-1>-1 & tran_table[k+j][i-1][0]==-1:
                        tran_table[k+j][:i] = tran_table[k][:i]

            
            # 2.优化采样
            new_proposal_nets = self.step(data, nets, tran_table, function)
            if (time.time()-t>dalte_time):
                t = time.time()
                index_end = s
                parameters = parameter_trace[index_begin*(self.N+1):index_end*(self.N+1),0]
                ess = tfp.mcmc.effective_sample_size(tf.convert_to_tensor(parameters))
                ess_list.append(ess)
                time_trace.append(self.net)
                index_begin = s

            parameter_trace[s*(self.N+1):(s + 1)*(self.N+1),:] = new_proposal_nets
        return parameter_trace,np.array(ess_list)

# GPU 
# train = PMPtrain(net_init,alpha = 0.02, N=7)
# parameter_trace,ess_list = train.fit(data,10000,function= muti_loglik_pycuda_pmp)
# plt.boxplot(ess_list[-10:])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1565686.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android Monkey自动化测试

monkey一般用于压力测试&#xff0c;用户模拟用户事件 monkey 基本用法 adb shell monkey [参数] [随机事件数]monkey常用命令 -v&#xff1a;用于指定反馈信息级别&#xff0c;总共分三个等级-v -v -vadb shell mokey -v -v -v 100-s&#xff1a;用于指定伪随机数生成器的种…

vue3源码解析——watch和watchEffect区别

watch和watchEffect是Vue 3.0中新增的两个响应式API&#xff0c;用于监听数据的变化。watch适用于需要获取新值和旧值&#xff0c;或者需要懒执行的场景&#xff0c;而watchEffect适用于需要监听多个数据源&#xff0c;并且需要立即执行的场景。它们之间的区别如下&#xff1a;…

OSPF-路由过滤、OSPFv3

OSPF——过滤&#xff0c;故障排除 OSPFv2——路由过滤 1、路由引入的过滤&#xff1a; import-route static route-policy //用于实现路由引入的过滤 filter-policy export&#xff0c;针对本区域传出的路由执行过滤 filter-policy import&#xff0c;针对传入本区域的路…

STM32学习和实践笔记(4): 分析和理解GPIO_InitTypeDef GPIO_InitStructure (a)

深入分析及学习一下上面这一段代码的构成与含义。 首先&#xff0c;这个GPIO_InitTypeDef GPIO_InitStructure;其实与int a 是完全类似的语法格式以及含义。 GPIO_InitStructure就相当于a这样一个变量。不过从这个变量的名字可以知道&#xff0c;这是一个用于GPIO初始化的结构…

部署项目遇到的各种问题总结

文章目录 前言一、后端问题 jar包运行出现错误宝塔面板使用jdk17二、数据库问题 版本问题三、前端问题 连不上后端总结 前言 在做完项目之后&#xff0c;为了让别人访问到自己的网站&#xff0c;就需要部署前端后端以及数据库&#xff0c;但是在部署的过程中出现了各种问题和困…

AcWing 786. 第k个数——算法基础课题解

AcWing 786. 第k个数 题目描述 给定一个长度为 n的整数数列&#xff0c;以及一个整数 k&#xff0c;请用快速选择算法求出数列从小到大排序后的第 k 个数。 输入格式 第一行包含两个整数 n 和 k。 第二行包含 n 个整数&#xff08;所有整数均在 1∼10^9 范围内&#xff09…

用于无人机小型化设计的高精度温补晶振

用于无人机小型化设计的高精度温补晶振:TG2016SMN和TG2520SMN。无人机的发展可以说是非常的迅速&#xff0c;在安防&#xff0c;农业&#xff0c;交通&#xff0c;电力&#xff0c;直播等领域经常能看到无人机大显身手。无人机的应用场最是非常的广泛&#xff0c;功能更强&…

[蓝桥杯练习]通电

kruskal做法(加边) #include <bits/stdc.h> using namespace std; int x[10005],y[10005],z[10005];//存储i点的x与y坐标 int bcj[10005];//并查集 struct Edge{//边 int v1,v2; double w; }edge[2000005]; int cmp(Edge a, Edge b){return a.w < b.w;} int find(i…

数控加工4轴初探

4轴加工之前一直觉得很神秘&#xff0c;最近画了些时间研究了一下&#xff0c;做过之后发现起始也不是特别复杂。 图中是两步&#xff0c;一步是粗开&#xff0c;已不是用指形铣刀精加工螺旋槽。

百度地图 JavaScript API GL快速升级 和mapV的使用

一、百度地图 JavaScript API GL快速升级 JavaScript API GL升级指南 1、在原来的链接中添加type webgl 2、批量替换页面中的BMap直接替换为BMapGL 二、mapV的使用 MapVGL mapVGL的效率确实要快很多&#xff0c;一万个点基本实现秒现 1、加载mapvgl.min.js export const…

【群晖】部署UptimeKuma监控服务

【群晖】部署UptimeKuma监控服务 点击标题查看原文 本文讲解在群晖系统中使用docker方式部署UptimeKuma服务并通过外网地址正确访问 配置及版本 DSM&#xff1a;7.2&#xff08;7.x以上均可&#xff09; UptimeKuma&#xff1a;louislam/uptime-kuma:latest 安装 docker中下…

webapi 允许跨域

1.在Nuget安装webapi.cors 添加完会有这个包 然后在项目App_Start 目录下的WebApiConfig.cs里面添加 // Web API 配置和服务// 添加跨域设置config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

鸿蒙分布式音乐播放-如何完成播放、暂停、上一曲、下一曲功能

介绍 本示例使用fileIo获取指定音频文件&#xff0c;并通过AudioPlayer完成了音乐的播放完成了基本的音乐播放、暂停、上一曲、下一曲功能&#xff1b;并使用DeviceManager完成了分布式设备列表的显示和分布式能力完成了音乐播放状态的跨设备分享。 本示例用到了与用户进行交…

[计算机效率] 磁盘空间分析工具:FolderSize

3.15 磁盘空间分析工具&#xff1a;FolderSize FolderSize是一款磁盘管理工具&#xff0c;提供预约交互式磁盘空间分析体验&#xff0c;可以可视化观察磁盘空间使用情况。程序可以帮助用户快速查看并统计硬盘中的各个分区所占用的空间大小以及文件夹和文件的大小&#xff0c;并…

跨境电商新纪元:独立站系统,让您的品牌飞跃国际舞台

在全球化的大潮中&#xff0c;跨境电商已不再是新鲜事物&#xff0c;而是众多企业实现品牌国际化、拓展市场份额的重要战略手段。然而&#xff0c;如何在竞争激烈的国际市场中脱颖而出&#xff0c;成为众多跨境电商企业面临的难题。今天&#xff0c;我要为大家介绍的&#xff0…

docker配置github仓库ghcr国内镜像加速

文章目录 说明ghcr.io简介配置镜像命令地址命令行方式1panel面板方式方式一&#xff1a;配置镜像加速&#xff0c;命令行拉取方式二&#xff1a;配置镜像仓库&#xff0c;可视化拉取 说明 由于使用的容器需要从github下载镜像&#xff0c;服务器在国外下载速度很慢&#xff0c…

CTF下加载CTFtraining题库以管理员身份导入 [HCTF 2018]WarmUp,之后以参赛者身份完成解题全过程

-------------------搭建CTFd------------------------------ 给大家介绍一个本地搭建比较好用的CTF比赛平台&#xff1a;CTFD。 CTFd是一个Capture The Flag框架&#xff0c;侧重于易用性和可定制性。它提供了运行CTF所需的一切&#xff0c;并且可以使用插件和主题轻松进行自…

二分图、匈牙利算法

目录 一&#xff0c;二分图 CodeForces 687A NP-Hard Problem 力扣 785. 判断二分图 二&#xff0c;完全二分图 1&#xff0c;完全二分图 2&#xff0c;K2,3 3&#xff0c;K3,3 三&#xff0c;匈牙利算法 1&#xff0c;二分图最大匹配 2&#xff0c;其他图论问题 一&…

交易所上币:区块链项目上线交易所流程

一、了解交易所/申请上币 在区块链项目上线交易所之前,首先需要对交易所进行充分的了解,包括交易所的基本信息、交易规则、飞BTC5186上币标准等。还需要了解交易所的申请上币流程,以便为后续的操作做好准备。 1.1 选择合适的交易所 在众多的交易所中 飞(BTC5186),如何选择一个…

NineData云原生智能数据管理平台新功能发布|2024年3月版

数据库 DevOps - 大功能升级 SQL 开发早期主要提供 SQL 窗口&#xff08;IDE&#xff09;功能&#xff0c;在产品经过将近两年时间的打磨&#xff0c;新增了大量的企业级功能&#xff0c;已经服务了上万开发者&#xff0c;覆盖了数据库设计、开发、测试、变更等生命周期的功能…