I am using t-SNE python implementation for dimensionality reduction on X which contains 100 instances each described by 1024 parameters for cnn visualization.
X.shape = [100, 1024]
X.dtype = float32
When I run :
Y = tsne.tsne(X)
The first warning pops out in tsne.py, line 23 :
RuntimeWarning: divide by zero encountered in log
H = Math.log(sumP) + beta * Math.sum(D * P) / sumP
Then there is a couple more warnings like this one on the following lines :
RuntimeWarning: invalid value encountered in divide
And finally I get this result after each iteration during the processing :
Iteration xyz : error is nan
The code ends without "errors" and I get an empty scatter plot at the end.
EDIT:
-> I have tried it with a different data set and it worked perfectly. However I would need it to work on my first set as well (the one that seems to cause problems)
Question :
Does anyone know what might be causing this? Is there a workaround?
sumP = sum(P)+np.finfo(np.double).eps
H = np.log(sumP) + beta * np.sum(D * P) / sumP;
This should fix the problem
Related
I'm made a program that calculates the line of best fit of a set of data points using gradient descent. I generate a 1000 random points and then it calculates the line of best fit training on these 1000 points. My confusion lies in the theory of my code.
In the part of my code where the training function is, by using the current m and b values for y= mx +b, the function makes a guess of the y values when it goes through the training points x values. This is supervised learning, so I know what the actual y value is, the function calculates the error and using that error adjusts the m and b values. <-- What is happening in the program when adjusting the line of best fit
I get everything above ^. what I'm confused about is the part of the code that calculates how to adjust these m and b values. Here it is:
guess = m * x + b;
error = y - guess;
m = m + (error * x) * learningrate;
b = b + error * learningrate;
Im confused about why we add instead of subtract that delta m (the (error * x) *learningrate)) part. Ignoring the learningrate, the error * x part is the partial derivative of the error with respect to m. But if we took the partial derivative of something with respect to something, wouldn't it give us the direction of the steepest ascent? Shouldn't we go the opposite direction (subtract the delta m) to get the proper m value? Isn't our goal to reduce the error?
Surprisingly to me, the above code works, if you add the delta m, it adjusts the m and b values in the right direction. So basically my question is: Why aren't we subtracting the delta m part (error *x) as it is pointing in the direction of steepest ascent, and we want to get the opposite of that?
Thanks!
I'm writing a code in python to evolve the time-dependent Schrodinger equation using the Crank-Nicolson scheme. I didn't know how to deal with the potential so I looked around and found a way from this question, which I have verified from a couple other sources. According to them, for a harmonic oscillator potential, the C-N scheme gives
AΨn+1=A∗Ψn
where the elements on the main diagonal of A are dj=1+[(iΔt) / (2m(Δx)^2)]+[(iΔt(xj)^2)/4] and the elements on the upper and lower diagonals are a=−iΔt/[4m(Δx)^2]
The way I understand it, I'm supposed to give an initial condition(I've chosen a coherent state) in the form of the matrix Ψn and I need to compute the matrix Ψn+1 , which is the wave function after time Δt. To obtain Ψn+1 for a given step, I'm inverting the matrix A and multiplying it with the matrix A* and then multiplying the result with Ψn. The resulting matrix then becomes Ψn for the next step.
But when I'm doing this, I'm getting an incorrect animation. The wave packet is supposed to oscillate between the boundaries but in my animation, it is barely moving from its initial mean value. I just don't understand what I'm doing wrong. Is my understanding of the problem wrong? Or is it a flaw in my code?Please help! I've posted my code below and the video of my animation here. I'm sorry for the length of the code and the question but it's driving me crazy not knowing what my mistake is.
import numpy as np
import matplotlib.pyplot as plt
L = 30.0
x0 = -5.0
sig = 0.5
dx = 0.5
dt = 0.02
k = 1.0
w=2
K=w**2
a=np.power(K,0.25)
xs = np.arange(-L,L,dx)
nn = len(xs)
mu = k*dt/(dx)**2
dd = 1.0+mu
ee = 1.0-mu
ti = 0.0
tf = 100.0
t = ti
V=np.zeros(len(xs))
u=np.zeros(nn,dtype="complex")
V=K*(xs)**2/2 #harmonic oscillator potential
u=(np.sqrt(a)/1.33)*np.exp(-(a*(xs - x0))**2)+0j #initial condition for wave function
u[0]=0.0 #boundary condition
u[-1] = 0.0 #boundary condition
A = np.zeros((nn-2,nn-2),dtype="complex") #define A
for i in range(nn-3):
A[i,i] = 1+1j*(mu/2+w*dt*xs[i]**2/4)
A[i,i+1] = -1j*mu/4.
A[i+1,i] = -1j*mu/4.
A[nn-3,nn-3] = 1+1j*mu/2+1j*dt*xs[nn-3]**2/4
B = np.zeros((nn-2,nn-2),dtype="complex") #define A*
for i in range(nn-3):
B[i,i] = 1-1j*mu/2-1j*w*dt*xs[i]**2/4
B[i,i+1] = 1j*mu/4.
B[i+1,i] = 1j*mu/4.
B[nn-3,nn-3] = 1-1j*(mu/2)-1j*dt*xs[nn-3]**2/4
X = np.linalg.inv(A) #take inverse of A
plt.ion()
l, = plt.plot(xs,np.abs(u),lw=2,color='blue') #plot initial wave function
T=np.matmul(X,B) #multiply A inverse with A*
while t<tf:
u[1:-1]=np.matmul(T,u[1:-1]) #updating u but leaving the boundary conditions unchanged
l.set_ydata((abs(u))) #update plot with new u
t += dt
plt.pause(0.00001)
After a lot of tinkering, it came down to reducing my step size. That did the job for me- I reduced the step size and the program worked. If anyone is facing the same problem as I am, I recommend playing around with the step sizes. Provided that the rest of the code is fine, this is the only possible area of error.
I am new in R and I try to use apply function on the xts zoo class, however it shows error. I have a formula: ((2*Close-High-Low)/(High-Low)) * Volume
Input:
y <- getSymbols("0005.HK", auto.assign = FALSE, src = "yahoo")
Error:
y$II <- apply(y,2,function(x) (2Cl(x) - Hi(x) - Lo(x)) / ((Hi(x) - Lo(x)) * Vo(stk)))
Error: unexpected symbol in "apply(y,2,function(x) (2Cl"
and then I tried another one:
Error:
y$II <- apply(y,2,function(x) (2(x[,4]) - x[,2] - x[,3]) / (x[,2] - x[,3]) * x[,5])
Error in FUN(newX[, i], ...) : attempt to apply non-function
After that, I would like to sum the y$II 21 days but I don't know how to do apply function to sum 21 days between every 21 days
IIstd = Sum of 21 ((2*C-H-L)/(H-L)) * V
IInorm = (IIstd / Sum 21 day V) * 100
Anyone can help me ? Please advice, thanks.
There are two problems here:
2Cl(x) i s not valid R -- use 2 * Cl(x)
all operations on the right hand side are already vectorized so we do not need apply in the first place
For clarity here we have assumed that II = (2C - H - L)/((H-L) * V)and you want 100 times the 21 period volume weighted moving average of that. Modify if that is not what you want.
Try this:
y$II <- (2*Cl(y) - Hi(y) - Lo(y)) / ((Hi(y) - Lo(y)) * Vo(y))
Regarding the second part of the question try this -- rollapplyr is in the zoo package.
wmean <- function(x) weighted.mean(x$II, Vo(x))
y$MeanII <- 100 * rollapplyr(y, 21, wmean, by.column = FALSE, fill = NA)
Also check out the TTR package.
UPDATE: Added answer to second part of question.
Executing the following code gives a plot where the last 2 minor grid lines are missing in both x and y. If I remove the section of the code that trims the data or extend the amount of data which is accepted (xylimit) then this can be obviated. Can anyone see what I'm doing wrong?
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
l=201
x=linspace(-l,l,201)
y=linspace(-l,l,201)
z=np.random.rand(l,l)
xylimit=100
i=0
while i<len(x[:]):
if abs(x[i])>xylimit:
x=np.delete(x,i,0)
y=np.delete(y,i,0)
else:
i+=1
i=0
while i<len(y[:]):
if abs(y[i])>xylimit:
x=np.delete(x,i,0)
y=np.delete(y,i,0)
else:
i+=1
z=np.random.rand(len(x),len(x))
xgridlines = getp(gca(), 'xgridlines')
ygridlines = getp(gca(), 'ygridlines')
plt.minorticks_on()
plt.grid(b=True, which='both',linestyle='-')
l=plt.contourf(x,y,z,np.linspace(0,1,255))
plt.show()
It seems to be a bug in matplotlib's minor tick locator (matplotlib.ticker.AutoMinorLocator). The tick at 90.0 just does not exist. If you do this:
xlim(-98.5,100.0)
The tick miraculously appears. But if you use any number which will prevent the last major line to be drawn (i.e. anything below 100.0), the tick disappears.
The lack of the tick can be verified by:
>>> gca().get_xaxis().get_minorticklocs()
array([-90., -80., -70., -60., -40., -30., -20., -10., 10., 20., 30.,
40., 60., 70., 80.])
There is an ugly manual hack to get around the problem
gca().get_xaxis().set_minor_locator(matplotlib.ticker.FixedLocator(
array([-90., -80., -70., -60., -40., -30., -20., -10., 10., 20., 30.,
40., 60., 70., 80., 90.])))
This unfortunately requires setting the positions by hand.
A minimal example of the bug (namespaces omitted, I use ipython+pylab):
figure()
plot([0,100], [0,100])
axis([0,99.5,0,99.5])
minorticks_on()
grid(which='both')
And the bug... It seems to be on line 1732 of ticker.py now available at github (or line 1712 in version 1.3.1). As the version may change, I'll paste it here:
if len(majorlocs) > 0:
t0 = majorlocs[0]
tmin = np.ceil((vmin - t0) / minorstep) * minorstep
tmax = np.floor((vmax - t0) / minorstep) * minorstep
locs = np.arange(tmin, tmax, minorstep) + t0
cond = np.abs((locs - t0) % majorstep) > minorstep / 10.0
locs = locs.compress(cond)
else:
locs = []
The bug itself is on the line locs = .... Actually there are two bugs:
the use of arange in a way shown here, because in my simple example tmin = 0.0, tmax = 95.0 and minorstep = 5.0; in general case it is sheer luck if arangewill give the 95.0 or not. (Actually with integers it works always the wrong way but with anything else is very prone to round-off errors.)
even if the maths went right above (they do in the example), the last sample will be missed
In my opinion this should be rewritten to use integers. As a temporary cure, one could replace the line with:
locs = np.arange(tmin, tmax + minorstep / 2., minorstep) + t0
This got the lines back at least in my matplotlib.
So, the short answer: Find the ticker.py somewhere in python package folders, make the small edit, and you are done.
I have really interesting problem, but I am solving it for 3 hours and I just can't figure out what is going on and why it isn't working. I tried google it, but with no results.
I am coding program on CUDA. I have this really simple piece of code:
__global__ void calcErrorOutputLayer_kernel(*arguments...*)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
float gradient;
float derivation;
derivation = pow((2/(pow(euler, neuron_device[startIndex + idx].outputValue) +
pow(euler, -neuron_device[startIndex + idx].outputValue))), 2);
gradient = (backVector_device[idx] - neuron_device[startIndex + idx].outputValue);
gradient = gradient * derivation; //this line doesn't work
gradient = gradient * 2.0; //this line works
ok, so gradient is calculated correctly and also derivation. but when comes line, where should be these two variables multiplicated with each other nothing happens (value of gradient isn't changed) and on next line CUDA debugger tells me that: " 'derivation' has no value at the target location "
gradient * 2.0 works correctly and it change value of gradient 2 times.
Can anyone help me please?
a = pow(euler, neuron_device[startIndex + idx].outputValue);
b = pow(euler, -neuron_device[startIndex + idx].outputValue);
derivation = pow((2/(a + b),2);
Pow gives an error when:
the base is negative and exponent is not an integral value, or
the base is zero and the exponent is negative, a domain error occurs, setting the global variable errno to the value EDOM.
I guess that you are facing precision problems, and both 'a' and 'b' are 0. You probably are getting derivation = 0 or "inf".
Can you change floats to doubles?