theano.function - how does it work? - python

I've read the official documenation and read the comments here https://github.com/Theano/theano/blob/ddfd7d239a1e656cee850cdbc548da63f349c37d/theano/compile/function.py#L74-L324, and one man told me that it tells Theano to compile the symbolic computation graph into an actual program that you can run.
However, I still cannot figure out how does it know, for example in this code:
self.update_fun = theano.function(
inputs=[self.input_mat, self.output_mat],
outputs=self.cost,
updates=updates,
allow_input_downcast=True)
how to compute all that, if it has no body? I mean all those things are computed in some code above these pasted lines, but... is theano.function actually looking to source code to find out how to compute those things? I'm just guessing and would really like to know how it works.
Maybe the problem I have in the explanation that "it tells Theano to compile the symbolic computation graph into an actual program" is that I have no clue what symbolic computation graph is, so that would be another question very related to the previous.
Explanation would be appreciated.

I'm no expert but here's my take at explaining it:
Yes the 'body' is defined in the code above. But theano doesn't 'interpret' that code directly like the python interpreter would. The code in question is just creating theano objects that will allow theano to compile the desired function. Let's take a simple example: how you would create a function f(x) = 2x + x**3.
You first create a symbolic input variable x. Then you define the 'body' of the function by building the symbolic expression of f(x):
y = 2 * x + x**3 # defines a new symbolic variable which depends on x
This y object is equivalent to a graph representing the formula. Something like Plus(Times(2,x), Power(x,3)).
You finally call theano.function with input=x and output=y. Then theano does its magic and compiles the actual function f(x) = y = 2 * x + x**3 from the information (the graph) 'contained' in y.
Does it make things clearer?

Related

How do I use newtons method on python to solve a system of equations?

I have an assignment where I need to make a single defined function that runs newtons method and then be able plug in other defined functions to it and it will solve them all. I wrote one that works for equations that have 1 variable, and I only need to solve for one variable from the system but I don't know how to do that in code without solving for all four of them.
the function I wrote to run newtons method is this:
def fnewton(function, dx, x, n):
#defined the functions that need to be evaluated so that this code can be applied to any function I call
def f(x):
f=eval(function)
return f
#eval is used to evaluate whatever I put in the function place when I recall fnewton
#this won't work without eval to run the functions
def df(x):
df=eval(dx)
return df
for intercept in range(1,n):
i= x-(f(x)/df(x))
x= i
#this is literally just newtons method
#to find an intercept you can literally input intercept in a for loop and it'll do it for you
#I just found this out
#putting n in the range makes it count iterations
print ('root is at')
print (x)
print ('after this many iterations:')
print (n)
my current system of equations function looks like this:
def c(x):
T=x[0]
y=x[1]
nl=x[2]
nv=x[3]
RLN=.63*Antoine(An,Bn,Cn,T)-y*760
RLH=(1-.63)*Antoine(Ah,Bh,Ch,T)-(1-y)*760
N=.63*nl+y*nv-50
H=(1-.63)*nl+(1-y)*nv-50
return[RLN,RLH,N,H]
To use my function to solve this I've entered multiple variations of:
fnewton("c(x)","dcdx(x)", (2,2,2,2), 10)
Do I need to change the system of equations into 1 equation somehow or something I don't know how to manipulate my code to make this work and also work for equations with only 1 variable.
To perform Newton's method in multiple dimensions, you must replace the simple derivative by a Jacobian matrix, that is, a matrix that has the derivatives of all components of your multidimensional function with respect to all given variables. This is decribed here: https://en.wikipedia.org/wiki/Newton%27s_method#Systems_of_equations,
(or, perhaps more helpful, here: https://web.mit.edu/18.06/www/Spring17/Multidimensional-Newton.pdf in Sec. 1.4)
Instead of f(x)/f'(x), you need to work with the inverse of the Jacobian matrix times the vector function f. So the formula is actually quite similar!

Using an array as an input for a multivariable function

If I have a multivariable function such as
F= lambda x,y: x**2+y**2
and if I need to use the input x0=np.array([1,1])
May I know how I should use x0 to get the value from F?
I understand that I could use something like F(x0[0],x0[1])
But I would like to know whether there is a way that I can directly use x0 rather than calling each cordinate manually
Appreciate your help
Python lets you do this by doing F(*x0), which expands the array into the parameters. In other languages this is sometimes called "splatting".

Matlab mynorm function

I am trying to convert a code from Matlab to Python in which I came across a function named mynorm(x,y) in Matlab which I want to convert to Python. I searched equivalent for this function in Python but was not successful. So tried to find the implementation of this function on the net and found a small mynorm.m file which contains the function with just one-liner code which is as follows:
function L = mynorm(x,y)
%length of the vector [x,y]
L = sqrt(x^2 + y^2);
%note - if the input was a vector v, a better way to do this would be
%L = sqrt(dot(v,v))
%see help dot for the dot product. This would work for vectors of any size.
But when I looked into the call which is made to this function in matlab file, it is as follows:
feaNorm = mynorm(fea2, 1)
feaNorm = mynorm(iris(:,1:4),2);
which doesn't really look like length function as in the above implementation.
Thus, I am skeptical to use sqrt function in Python for this function call.
Can someone redirect me to the correct implementation or the equivalent python code?
I suggest you use the hypot trigonometric function available in the math module. You will find a description of the math module here.

What is wrong with my cost function in numpy?

I was trying to implement a cost function for a Programming assignment in Andrew Ng Deep Learning course which requires my own, original work. I am also not allowed to reproduce the assignment code without permission, but am doing so anyway in this question.
The expected result for the cost = 6.000064773192205, But with this code, my result for cost = 4.50006477319. Does anyone have any idea what I did wrong in this code?
removed code
There is an error in your sigmoid function. You are supposed to calculate negative of np.dot(np.transpose(w), X) + b).
Here is the one I have used
A = 1 / (1 + np.exp(-(np.dot(np.transpose(w), X) + b)))
np.sum(np.multiply(Y, np.log(A)) + np.multiply((1-Y), np.log(1-A))) /m
Just in case you find it useful (and as I'm doing the same source), you could also have called the sigmoid() function you defined in the previous step from within propagate() by using this instead:
A = sigmoid(np.dot(w.T,X) + b)
It's not necessary as evidenced by your work, but it's a bit cleaner.

What is fitfunc and errfunc intuitively in python?

I just wanted to ask you all about what is fitfunc, errfunc followed by scipy.optimize.leastsq is intuitively. I am not really used to python but I would like to understand this. Here is the code that I am trying to understand.
def optimize_parameters2(p0,mz):
fitfunc = lambda p,p0,mz: calculate_sp2(p, p0, mz)
errfunc = lambda p,p0,mz: exp-fitfunc(p,p0,mz)
return scipy.optimize.leastsq(errfunc, p0, args=(p0,mz))
Can someone please explain what this code is saying narratively word by word?
Sorry for being so specific but I really do have trouble understanding what it's saying.
This particular code snippet is implementing nonlinear least-squares regression to find the parameters of a curve function (this is the fitfunc, here) that best fit a set of data (exp, probably an abbreviation for "experimental data"). leastsq() is a somewhat more general routine for doing nonlinear least-squares optimization, not just curve-fitting. It requires a function (named errfunc, here) that is given a vector of parameters (p) and returns an array. It will attempt to find the parameter vector that minimizes the square of the returned array. In order to implement "fitting a curve to data" with leastsq, you have to provide an errfunc that evaluates the curve (fitfunc) at the given trial parameter vector and then subtracts it from the data (i.e. calculate the "error" or sometimes called the "residuals").
Just to be clear, none of these names are important. I'm just using them to refer to specific parts of the code snippet you provided. You will find other code that uses leastsq() for curve-fitting that names and organizes the code a little bit differently, but now that you know the general scheme, you should be able to follow along.
Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called lambda. In your example, fitfunc and errfunc are two such lambda functions.
I believe calculate_sp2 and exp_fitfunc are simply two functions which are in the code but you didn't provide their code in the example. So, in short fitfunc actually calls the calculate_sp2 function with 3 parameters (p, p0, mz) and returns the value which is returned by calculate_sp2. errfunc also works in the same manner.
As mentioned in official documentation of scipy.optimize.leastsq, leastsq() minimizes the sum of squares of a set of equations. You can learn about the parameters of leastsq() from the official documentation.
I am giving a simple example to illustrate how lambda function works.
def add(x,y):
return x + y
def subtract(x,y):
return x-y if x > y else y-x
def main(x,y):
addition = lambda x,y: add(x,y)
subtraction = lambda x,y: subtract(x,y)
return addition(x,y) * subtraction(x,y)
print(main(7,4)) # prints 33 which is equal to (7+4)*(7-4)

Categories