normalization in theano for any image? - python

I write below code but that is very slow. And, of course, does not work correct!
I have what I do?
for i in range(image_shape[0]):
for j in range(filter_shape[0]):
pmin = self.pooled_out[i][j].min()
pmax = self.pooled_out[i][j].max()
self.pooled_out = T.set_subtensor(T.set_subtensor(self.pooled_out[i], self.pooled_out[i])[j],self.pooled_out[i][j] - pmin)
self.pooled_out = T.set_subtensor(T.set_subtensor(self.pooled_out[i], self.pooled_out[i])[j],self.pooled_out[i][j] / pmax)

First that section of your code do nothing. Just remove this line
T.set_subtensor(self.pooled_out[i], self.pooled_out[i])
Without full code, I can't test my solution, but I think this would do what you want:
pmin = self.pooled_out.min(axis=[2,3], keepdims=True)
pmax = self.pooled_out.max(axis=[2,3], keepdims=True)
normalized_pooled_out = (self.pooled_out - pmin)/pmax
Then normalized_pooled_out contain the symbolic variable that have the value I think you want.

Related

variables changing after a function call in python

Ive been working on some code recently for a project in python and im confused by the output im getting from this code
def sigmoid(input_matrix):
rows = input_matrix.shape[0]
columns = input_matrix.shape[1]
for i in range(0,rows):
for j in range(0,columns):
input_matrix[i,j] = (1 / (1 + math.exp(-(input_matrix[i,j]))))
return input_matrix
def feed_forward(W_L_1 , A_L_0 , B_L_1):
weighted_sum = np.add(np.dot(W_L_1,A_L_0), B_L_1)
activation = sigmoid(weighted_sum)
return [weighted_sum,activation]
a = np.zeros((1,1))
b = feed_forward(a,a,a)
print(b[0])
print(b[1])
when I print both b[0] and b[1] give values of .5 even though b[0] should equal 0. Also in addition to this when I place the '''weighted_sum = np.add(np.dot(W_L_1,A_L_0), B_L_1)''' again after the 'actvation' line it provides the correct result. Its as if the 'activation' line has changed the value of the weighted sum value.
Was wondering if anyone could spread some light on this I can work around this but am interested as to why this is happening. Thanks!!
Inside sigmoid, you're changing the value of the matrix passed as parameter, in this line:
input_matrix[i,j] = ...
If you want to prevent this from happening, create a copy of the matrix before calling sigmoid, and call it like this: sigmoid(copy_of_weighted_sum).

SymPy with advanced rewrite and simplification

For instance, I want to expand the following simultaneous equations in first order difference with respect to x, y and z1:
$$x^\alpha y^(1-\alpha) = z_1$$
$$x^\beta y^(1-\beta) = z_2$$
It is obviously that
$$\alpha \hat{x} + (1-\alpha) \hat{y} = \hat{z_1}$$
$$\beta \hat{x} + (1-\beta) \hat{y} = 0$$
where $\hat{variable}$ means the elasticity of the variable, namely, $\frac{d varibale}{variable}$.
We have:
$$\hat{x} = \frac{1-\beta}{\alpha - \beta} \hat{z_1}$$
$$\hat{y} = -\frac{\beta}{\alpha - \beta} \hat{z_1}$$
The corresponding code for python using SymPy will be:
import sympy as sp
x,y,z1,z2,alpha,beta = sp.symbols('x,y,z_1,z_2,alpha,beta',positive=True)
eq1 = x**alpha*y**(1-alpha) - z1
eq2 = x**beta*y**(1-beta) - z2
hat_x,hat_y,hat_z1 = sp.symbols('\hat{x},\hat{y},\hat{z_1})
diff_eq1 = eq1.diff(x)*hat_x*x + eq1.diff(y)*hat_y*y + eq1.diff(z1)*hat_z1*z1
diff_eq2 = eq2.diff(x)*hat_x*x + eq2.diff(y)*hat_y*y + eq2.diff(z1)*hat_z1*z1
root = sp.solve([diff_eq1,diff_eq2],[hat_x,hat_y])
which gives the result
As you can see, the expression is right, but without further simplification. The reason is that it does not take the advantage of eq1 = 0 and eq2 = 0. My question is, how to make further simplifications using the information given by the original equations? Thanks!!
BTW, how can I declare variables with ranges? For instance, I want to declare $\alpha \in (0,1)$ and the $1-\alpha$ will also be positive and facilitate the following manipulation.
My question is, how to make further simplifications using the information given by the original equations?
In general, the solution to a simultaneous equation will not have one side of the equation in the solution. So I can only answer your question in this specific case. root is a dictionary and we will loop through all the values and substitute the RHS of the equations with the LHS.
import sympy as sp
x,y,z1,z2,alpha,beta = sp.symbols('x,y,z_1,z_2,alpha,beta',positive=True)
eq1 = x**alpha*y**(1-alpha) - z1
eq2 = x**beta*y**(1-beta) - z2
hat_x,hat_y,hat_z1 = sp.symbols('\hat{x},\hat{y},\hat{z_1}')
diff_eq1 = eq1.diff(x)*hat_x*x + eq1.diff(y)*hat_y*y + eq1.diff(z1)*hat_z1*z1
diff_eq2 = eq2.diff(x)*hat_x*x + eq2.diff(y)*hat_y*y + eq2.diff(z1)*hat_z1*z1
root = sp.solve([diff_eq1,diff_eq2],[hat_x,hat_y])
for key, value in root.items():
root[key] = value.subs({z1: x**alpha*y**(1-alpha), z2: x**beta*y**(1-beta)}).simplify()
BTW, how can I declare variables with ranges? For instance, I want to declare $\alpha \in (0,1)$ and the $1-\alpha$ will also be positive and facilitate the following manipulation.
There is no explicit way to do so in SymPy. There are a few work-arounds. See this Stack Overflow question.

Solving simultaneous equations (>2) without conversion to matrices in R or Python

I have a set of 4 simultaneous equations:
0.059z = x
0.06w = y
z+w = 8093
x+y = 422
All the solutions I've found so far seem to be for equations that have all the variables present in each equation, then convert to matrices and use the solve function.
Is there an easier way to do this in R or Python using the equations in their original form?
Also, how can I ensure that only positive numbers are returned in the solution?
Hope this makes sense...many thanks for the help
You can use sympy for this:
from sympy import symbols, linsolve, Eq
x,y,z,w = symbols('x y z w')
linsolve([Eq(0.059*z, x), Eq(0.06*w, y), Eq(z+w, 8093), Eq(x+y, 422)], (x, y, z, w))
Output:
Regarding your comments about negative values - there is only one solution to the system of equations, and it has negative values for y and w. If there was more than one solution, sympy would return them, and you could filter the solutions from there to only positive values.
In R, maybe you try it like below:
library(rootSolve)
library(zeallot)
model <- function(v){
c(x,y,z,w) %<-% v
return(c(0.059*z-x, 0.06*w-y, z+w-8093, x+y-422))
}
res <- multiroot(f = model, start = c(0,0,0,0))
then you can get the solution res as
> res
[1] 3751.22 -3329.22 63580.00 -55487.00
there are a few things going on here. first as CDJB notes: if there were any positive solutions then sympy would find them. I searched for those numbers and found this paper which suggests you should be using 7088 instead of 8093. we can do a quick sanity check:
def pct(value):
return f"{value:.1%}"
print(pct(422 / 8093)) # ~5.2%
print(pct(422 / 7088)) # ~6.0%
confirming that you're going to struggle averaging ~5.9% and ~6.0% towards ~5.2%, and explaining the negative solutions in the other answers. further, these are presumably counts so all your variables also need to be whole numbers.
once this correct denominator is used, I'd comment that there are many solutions (11645 by my count) e.g:
cases = [1, 421]
pop = [17, 7071]
rates = [pct(c / p) for c, p in zip(cases, pop)]
gives the appropriate output, as does:
cases = [2, 420]
pop = [34, 7054]
this is because the data was rounded to two decimal places. you probably also don't want to use either of the above, they're just the first two valid solutions I got.
we can define a Python function to enumerate all solutions:
from math import floor, ceil
def solutions(pop, cases, rate1, rate2, err):
target = (pct(rate1), pct(rate2))
for pop1 in range(1, pop):
pop2 = pop - pop1
c1_lo = ceil(pop1 * (rate1 - err))
c1_hi = floor(pop1 * (rate1 + err))
for c1 in range(c1_lo, c1_hi+1):
c2 = cases - c1
if (pct(c1 / pop1), pct(c2 / pop2)) == target:
yield c1, c2, pop1, pop2
all_sols = list(solutions(7088, 422, 0.059, 0.060, 0.0005))
which is where I got my count of 11645 above from.
not sure what to suggest with this, but you could maybe do a bootstrap to see how much your statistic varies with different solutions. another option would be to do a Bayesian analysis which would let you put priors over the population sizes and hence cut this down a lot.

How to decrease the output time of covariance function

I have written a function for covariance matrix and the output I am getting is correct but the problem with the code is, it's taking too much time for high dimension dataset.
Could you please help me to modify the code below to take less time for output?
def cov_variance(norm_data,mean_of_mat):
col = len(norm_data)
row = len(norm_data[0])
out =[]
i = 0
sum_of_covar = 0
freezrow = 0
flag = 1
while flag<=len(mean_of_mat):
for r in range(row):
for c in range(col):
sum_of_covar+=(((norm_data[c][freezrow])-mean_of_mat[freezrow])*\
((norm_data[c][r])-mean_of_mat[i]))
freezrow=freezrow
out.append(sum_of_covar)
i+=1
sum_of_covar=0
freezrow=freezrow
flag+=1
freezrow+=1
i=0
out1 = map(lambda x : x/col-1,out)
cov_variance_output = reshape(out1,row)
return cov_variance_output
Like doctorlove already said, don't implement your own. It will almost certainly be slower and/or less versatile (speaking from my own experience).
I tried commenting with this info but my rep is too low. You can find information on calculating covariance matrices with numpy here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html

difficulty in matlab to python code conversion

I am converting matlab code to python code
function Xn = ReSampleCurve(X,N)
[n,T] = size(X);
del(1) = 0;
for r = 2:T
del(r) = norm(X(:,r) - X(:,r-1));
end
cumdel = cumsum(del)/sum(del);
newdel = [0:N-1]/(N-1);
for j=1:n
Xn(j,:) = interp1(cumdel,X(j,1:T),newdel,'linear');
end
I want to convert this into python code
The input values are :
X = [[-9.035250067710876, 7.453250169754028, 33.34074878692627], [-6.63700008392334, 5.132999956607819, 31.66075038909912],[-5.1272499561309814, 8.251499891281128, 30.925999641418457], [-5.1272499561309814, 8.251499891281128, 30.925999641418457]]
N = 200
can anyone explains me what these lines do?
del(1) = 0;
for r = 2:T
del(r) = norm(X(:,r) - X(:,r-1));
For what it is worth, here is the vectorized way to get del(2:end) in Matlab, perhaps this makes more sense to you:
sqrt(sum(diff(M,1,2).^2))
del is an array in the MATLAB code. So del(1) = 0 is equivalent to del_list = [0] (MATLAB arrays a 1-indexed, del is a reserved word in python).
In the for loop, this is equivalent to:
for r in range(1,T):
del_list.append(norm(X[:,r] - X[:,r-1]))
The above won't work in pure python (array subtraction won't work). You'd have to add in numpy or numeric) - but hopefully you get the idea.

Categories