How to get the full disenGCN ? - graph

this is my code of disenGCN,but i dont khonw its format of dataset。
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import *
from layers import *
class DisGCN(nn.Module):
def init(self, in_dim, channels, C_dim, iterations, beta, layer_num, dropout,
out_dim): # 输入维数, 通道数目, 每个通道的输出维数, 迭代次数, 平衡因子
super(DisGCN, self).init()
self.dropout_rate = dropout
self.channels = channels
self.C_dim = C_dim
self.iterations = iterations
self.beta = beta
self.layer_num = layer_num
self.disconv1 = DisConv(in_dim[0], channels[0], C_dim[0], iterations, beta)
self.disconv2 = DisConv(in_dim[1], channels[1], C_dim[1], iterations, beta)
self.W_o = torch.nn.Parameter(torch.empty(size=(channels[-1] * C_dim[-1], out_dim), dtype=torch.float),
requires_grad=True)
self.bias = torch.nn.Parameter(torch.empty(size=(1, out_dim), dtype=torch.float), requires_grad=True)
self.init_parameters()
def init_parameters(self):
for i, item in enumerate(self.parameters()):
torch.nn.init.normal_(item, mean=0, std=1)
def forward(self, adj, features):
h = features
h = self.disconv1(adj, h)
h = F.dropout(h, self.dropout_rate, training=self.training)
h = self.disconv2(adj, h)
h = F.dropout(h, self.dropout_rate, training=self.training)
output = torch.mm(h, self.W_o) + self.bias
return output

Related

RuntimeWarning: overflow encountered in exp error in synthesising Planck curve

Can anyone please correct this code?
Aim of this code is to synthesize a Planck Curve and to integrate it.
import matplotlib, sys, os, time
import matplotlib.pyplot as plt
import numpy as np
from scipy.constants import c, h, hbar, pi, k, e
from scipy.constants import c, h, hbar, pi, k, e
import scipy.integrate
f = np.linspace(10**9, 10**19, 1000)
for T in [3000., 5000., 10000., 20000., 30000.,40000.]: ## define the temperatures you
wish to inspect
#T = 9e2
L=(2*h*f**3/c**2)*1/(np.exp(h*f/(k*T)) - 1)
import scipy.integrate
print (scipy.integrate.trapz(L, f) * pi / T**4 ) # integrate by frequency, verify
Stefan-Boltzmann
#print L
plt.plot(f,L)
totalL = scipy.integrate.trapz(L, f) * pi # integrate by frequency
print ("Radiosity at T=%g K (integrated from f=%g to %g): %g W/m^2" % (T,
np.min(f),np.max(f), totalL))
print ("\t\tCompared to the radiosity of black body over entire spectrum ratio (Stefan-
Boltzmann law): %.4g" % (totalL / 5.670367e-8 /T**4))
plt.plot(f,L, label="T = %g K" % T)
## Simple axes
#plt.ylim((-0.1,1.1));
plt.yscale('log')
#plt.xlim((-0.1,1.1));
plt.xscale('linear')
## ==== Outputting ====
## Finish the plot + save
plt.xlabel(u"freq [Hz]");
plt.ylabel(u"luminance [W/sr/m$^2$]" );
plt.xlabel(u"Radiation frequency [Hz]");
plt.ylabel(u"Spectral radiosity [W/m$^2/Hz$]" );
plt.grid()
plt.legend(prop={'size':10}, loc='upper right')
plt.savefig("output.png", bbox_inches='tight')

Bridge sampling for Brownian motion

I am producing code for bridge sampling for Brownian motion to simulate sample paths but I keep getting all zeros for my answer. I have got my code and picture algorithm below.
#Brownian bridge for GBM
Z<-rnorm(1, mean=0,sd=1)
T=1
W_[0]=0
W_[T]=sqrt(T)*Z
k=5
j<-2^(k-1)
W_=rep(NA,nt-1)
for(k in 1:K){
h=h/2
for(j in 1:j){
Z<-rnorm(1,mean=0,sd=1)
W_[2*(j-1)*h]=0.5*(W_[2*(j-1)*h]+W_[2*j*h])+sqrt(h)*Z
print(W_[2*(j-1)*h])
}
}
The algorithm is below:
If I am not mistaken with the formulas, in Python it should be something like this:
import numpy as np
import pandas as pd
# Samples a Brownian bridge in the [a,b] interval, see https://en.wikipedia.org/wiki/Brownian_bridge#General_case
def BB(
T: int = 252,
a: float = 0,
b: float = 0,
alpha: float = 0,
sigma: float = 1
) -> pd.Series:
X0 = a
X = pd.Series([X0])
t1 = 0
t2 = T
for i in np.arange(1, T):
t = i
mu = (a + (t - t1)/(t2 - t1)*(b - a))
s = ((t2 - t)*(t - t1)/(t2 - t1))
ei = np.random.normal(alpha+mu, sigma+s)
X.loc[i] = ((T - t)/T**0.5)*ei*((t/(T-t))**0.5)
XT = b
X[T] = XT
return X
Show it:
from matplotlib import pyplot as plt
plt.plot(BB(a=0,b=0, T=100))

Monitor GPU performance with nvprof does not work

I am trying to use nvprof to monitor the performance of the GPU. I would like to know the time consuming of HtoD(host to device), DtoH(device to host) and device execution.
It worked very well with a standard code from numba cuda website:
from numba import cuda
#cuda.jit
def add_kernel(x, y, out):
tx = cuda.threadIdx.x # this is the unique thread ID within a 1D block
ty = cuda.blockIdx.x # Similarly, this is the unique block ID within the 1D grid
block_size = cuda.blockDim.x # number of threads per block
grid_size = cuda.gridDim.x # number of blocks in the grid
start = tx + ty * block_size
stride = block_size * grid_size
# assuming x and y inputs are same length
for i in range(start, x.shape[0], stride):
out[i] = x[i] + y[i]
if __name__ == "__main__":
import numpy as np
n = 100000
x = np.arange(n).astype(np.float32)
y = 2 * x
out = np.empty_like(x)
threads_per_block = 128
blocks_per_grid = 30
add_kernel[blocks_per_grid, threads_per_block](x, y, out)
print(out[:10])
Here is the out come from nvprfo:
However, when I add the usage of multiprocessing with the following code:
import multiprocessing as mp
from numba import cuda
def fun():
#cuda.jit
def add_kernel(x, y, out):
tx = cuda.threadIdx.x # this is the unique thread ID within a 1D block
ty = cuda.blockIdx.x # Similarly, this is the unique block ID within the 1D grid
block_size = cuda.blockDim.x # number of threads per block
grid_size = cuda.gridDim.x # number of blocks in the grid
start = tx + ty * block_size
stride = block_size * grid_size
# assuming x and y inputs are same length
for i in range(start, x.shape[0], stride):
out[i] = x[i] + y[i]
import numpy as np
n = 100000
x = np.arange(n).astype(np.float32)
y = 2 * x
out = np.empty_like(x)
threads_per_block = 128
blocks_per_grid = 30
add_kernel[blocks_per_grid, threads_per_block](x, y, out)
print(out[:10])
return out
# check gpu condition
p = mp.Process(target = fun)
p.daemon = True
p.start()
p.join()
nvprof seems to monitor the process but it doesn't outcome anything though it reports that nvprof is profiling:
Furthermore, when I used Ray (a package for doing distributed computation):
if __name__ == "__main__":
import multiprocessing
def fun():
from numba import cuda
import ray
#ray.remote(num_gpus=1)
def call_ray():
#cuda.jit
def add_kernel(x, y, out):
tx = cuda.threadIdx.x # this is the unique thread ID within a 1D block
ty = cuda.blockIdx.x # Similarly, this is the unique block ID within the 1D grid
block_size = cuda.blockDim.x # number of threads per block
grid_size = cuda.gridDim.x # number of blocks in the grid
start = tx + ty * block_size
stride = block_size * grid_size
# assuming x and y inputs are same length
for i in range(start, x.shape[0], stride):
out[i] = x[i] + y[i]
import numpy as np
n = 100000
x = np.arange(n).astype(np.float32)
y = 2 * x
out = np.empty_like(x)
threads_per_block = 128
blocks_per_grid = 30
add_kernel[blocks_per_grid, threads_per_block](x, y, out)
print(out[:10])
return out
ray.shutdown()
ray.init(redis_address = "***")
out = ray.get(call_ray.remote())
# check gpu condition
p = multiprocessing.Process(target = fun)
p.daemon = True
p.start()
p.join()
nvprof doesn't show anything! It even doesn't show the line telling that nvprof is profiling the process (but the code is indeed executed):
Does anyone know how to figure this out? Or do I have any other choices to acquire these data for distributed computation?

Bayesian Lifetime estimates using pymc3/theano

I am trying to estimate the following model
where I provide uniform priors and I code the likelihood . The latter comes from this paper and goes as follows:
In the theano/pymc3 implementation I am computing the first and second term on the rhs in first_term and second_term. Finally logp sum over the entire sample.
Theano, on his own is producing some output yet when I integrate it in a pymc3 model the following error appears:
TypeError: ('Bad input argument to theano function with name "<ipython-input-90-a5304bf41c50>:27" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
I think the problem is how to supply pymc3 variables to theano.
from pymc3 import Model, Uniform, DensityDist
import theano.tensor as T
import theano
import numpy as np
p_test, theta_test = .1, .1
X = np.asarray([[1,2,3],[1,2,3]])
### theano test
theano.config.compute_test_value = 'off'
obss = T.matrix('obss')
p, theta = T.scalar('p'), T.scalar('theta')
def first_term(obs, p, theta):
x, tx, n = obs[0], obs[1], obs[2]
first_comp = p ** x * (1 - p) ** (n - x) * (1 - theta) ** n
return(first_comp)
def second_term(obs, p, theta):
x, tx, n = obs[0], obs[1], obs[2]
components, updates = theano.scan(
lambda t, p, theta, x, tx: p ** x * (1 - theta) ** (tx-x+t) * theta * (1 - theta) ** (tx + t),
sequences=theano.tensor.arange(n), non_sequences = [p, theta, x, tx]
)
return(components)
def logp(X, p_hat, theta_hat):
contributions, updates = theano.scan(lambda obs, p, theta: first_term(obs, p, theta) + T.sum( second_term(obs, p, theta) ) ,
sequences = obss, non_sequences = [p, theta]
)
ll = contributions.sum()
get_ll = theano.function(inputs = [obss, p, theta], outputs = ll)
return(get_ll(X, p_hat , theta_hat))
print( logp( X, p_test, theta_test ) ) # It works!
### pymc3 implementation
with Model() as bg_model:
p = Uniform('p', lower = 0, upper = 1)
theta = Uniform('theta', lower = 0, upper = .2)
def first_term(obs, p, theta):
x, tx, n = obs[0], obs[1], obs[2]
first_comp = p ** x * (1 - p) ** (n - x) * (1 - theta) ** n
return(first_comp)
def second_term(obs, p, theta):
x, tx, n = obs[0], obs[1], obs[2]
components, updates = theano.scan(
lambda t, p, theta, x, tx: p ** x * (1 - theta) ** (tx-x+t) * theta * (1 - theta) ** (tx + t),
sequences=theano.tensor.arange(n), non_sequences = [p, theta, x, tx]
)
return(components)
def logp(X):
contributions, updates = theano.scan(lambda obs, p, theta: first_term(obs, p, theta) + T.sum( second_term(obs, p, theta) ) ,
sequences = obss, non_sequences = [p, theta]
)
ll = contributions.sum()
get_ll = theano.function(inputs = [obss, p, theta], outputs = ll)
return(get_ll(X, p, theta))
y = pymc3.DensityDist('y', logp, observed = X) # Nx4 y = f(f,x,tx,n | p, theta)
My first guess was modifying logp with return(get_ll(X, p.eval(), theta.eval())) but then theano complains about some mysterious p_interval missing from the graph. Any clue?
I figured it out by: i) simplifying things ii) avoiding using theano operators when coding the likelihood and iii) using the build-in wrapper Deterministic for deterministic transformations of variables (my lifetime). To speed up computation I vectorised the likelihood by writing the second term on the rhs as solution of a geometric series. Here is the code in case someone want to test it on his own lifetime application.
from pymc3 import Model, Uniform, Deterministic
import pymc3
from scipy import optimize
import theano.tensor as T
X = array([[ 5, 64, 8, 13],[ 4, 71, 23, 41],[ 7, 70, 4, 19])
#f, n, x, tx where f stands for the frequency of the triple (n, x, tx)
class CustomDist(pymc3.distributions.Discrete):
def __init__(self, p, theta, *args, **kwargs):
super(CustomDist, self).__init__(*args, **kwargs)
self.p = p
self.theta = theta
def logp(self, X):
p = self.theta
theta = self.theta
f, n, x, tx = X[0],(X[1] + 1),X[2],(X[3] + 1) #time indexed at 0, x > n
ll = f * T.log( p ** x * (1 - p) ** (n - x) * (1 - theta) ** n +
[(1 - p) ** (tx - x) * (1 - theta) ** tx - (1 - p) ** (n - x) * (1 - theta) ** n] / (1 - (1 - p) * (1 - theta)) )
# eliminated -1 since would result in negatice ll
return(T.sum( ll ))
with Model() as bg_model:
p = Uniform('p', lower = 0, upper = 1)
theta = Uniform('theta', lower = 0, upper = 1)
like = CustomDist('like', p = p, theta = theta, observed=X.T) #likelihood
lt = pymc3.Deterministic('lt', p / theta)
# start = {'p':.5, 'theta':.1}
start = pymc3.find_MAP(fmin=optimize.fmin_powell)
step = pymc3.Slice([p, theta, lt])
trace = pymc3.sample(5000, start = start, step = [step])
pymc3.traceplot(trace[2000:])

How to interpret the result of cvxopt.solvers.qp?

There is no enough documentation and my math knowledge is limited.
The model
sol = solvers.qp(P=P, q=q,G=G,h=h, A=L, b=t)
pcost dcost gap pres dres
0: 6.3316e+08 6.3316e+08 7e+00 3e+00 7e-10
1: 6.3316e+08 6.3316e+08 2e+00 1e+00 3e-10
2: 6.3316e+08 6.3316e+08 2e+00 1e+00 3e-10
3: 1.3393e+10 -3.5020e+09 2e+10 9e-01 2e-10
4: 7.6898e+08 -1.7925e+10 4e+10 8e-01 2e-10
5: 2.1728e+09 -3.8363e+09 6e+09 6e-16 2e-14
6: 2.1234e+09 2.0310e+09 9e+07 2e-16 3e-16
7: 2.0908e+09 2.0899e+09 1e+06 1e-18 2e-16
8: 2.0905e+09 2.0905e+09 1e+04 7e-21 9e-17
9: 2.0905e+09 2.0905e+09 1e+02 2e-16 3e-16
Optimal solution found.
The result
{'dual infeasibility': 2.538901219845834e-16,
'dual objective': 2090476416.743256,
'dual slack': 59.256743485146764,
'gap': 95.35084344459145,
'iterations': 9,
'primal infeasibility': 2.220446049250331e-16,
'primal objective': 2090476512.0941,
'primal slack': 1.0136346108956689e-08,
'relative gap': 4.561201584523881e-08,
's': <2x1 matrix, tc='d'>,
'status': 'optimal',
'x': <2x1 matrix, tc='d'>,
'y': <1x1 matrix, tc='d'>,
'z': <2x1 matrix, tc='d'>}
How can I inteprert s,x,z which one is the result of my variable ?
If I print s and x, I always get something near to 1 and another near to 0, which seems to be infinitely incorrect.
>>> np.array(sol['x'])
>>> array([[ 9.99999990e-01],[ 1.01363461e-08]])
I think this short text from Stanford might be helpful for you. Highlighting the quoted section about the result:
Many properties about the solution can be extracted from the sol variable >
(dictionary).
In particular, the ‘status’, ‘x’, and ‘primal objective’ are probably the most >important. If status is optimal, then the latter two give the optimal solution and >its objective
value. You can access these fields as you would with a regular Python dictionary:
Also, this documentation provides a in-depth explanation. For understanding the result, please go to page 69
#full explain here
https://blog.dominodatalab.com/fitting-support-vector-machines-quadratic-programming
#full example here
import numpy as np
import pandas as pd
import cvxopt
import matplotlib
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
cvxopt.solvers.options['show_progress'] = True
if __name__ == '__main__':
x_min = 0
x_max = 5.5
y_min = 0
y_max = 2
iris = load_iris()
iris_df = pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=iris['feature_names'] + ['target'])
#Retain only 2 linearly separable classes
iris_df = iris_df[iris_df["target"].isin([0,1])]
iris_df["target"] = iris_df[["target"]].replace(0,-1)
#Select only 2 attributes
iris_df = iris_df[["petal length (cm)", "petal width (cm)", "target"]]
print(iris_df.head())
print()
X = iris_df[["petal length (cm)", "petal width (cm)"]].to_numpy()
y = iris_df[["target"]].to_numpy()
n = X.shape[0]
H = np.dot(y * X, (y * X).T)
q = np.repeat([-1.0], n)[..., None]
A = y.reshape(1, -1)
b = 0.0
G = np.negative(np.eye(n))
h = np.zeros(n)
P = cvxopt.matrix(H)
q = cvxopt.matrix(q)
G = cvxopt.matrix(G)
h = cvxopt.matrix(h)
A = cvxopt.matrix(A)
b = cvxopt.matrix(b)
solved = cvxopt.solvers.qp(P, q, G, h, A, b)
print()
alphas = np.array(solved["x"])
w = np.dot((y * alphas).T, X)[0]
S = (alphas > 1e-5).flatten()
b = np.mean(y[S] - np.dot(X[S], w.reshape(-1, 1)))
print("W:", w)
print("b:", b)
xx = np.linspace(x_min, x_max)
a = -w[0] / w[1]
yy = a * xx - (b) / w[1]
margin = 1 / np.sqrt(np.sum(w ** 2))
yy_neg = yy - np.sqrt(1 + a ** 2) * margin
yy_pos = yy + np.sqrt(1 + a ** 2) * margin
plt.figure(figsize=(8, 8))
plt.plot(xx, yy, "b-")
plt.plot(xx, yy_neg, "m--")
plt.plot(xx, yy_pos, "m--")
colors = ["steelblue", "orange"]
plt.scatter(X[:, 0],
X[:, 1],
c=y.ravel(),
alpha=0.5,
cmap=matplotlib.colors.ListedColormap(colors),
edgecolors="black")
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.title('svm line')
plt.show()

Resources