I am new to programming, especially programming with tensorflow. I'm making toy problems to understand using it.
In that case I want to build a function like softmax, where the denominator is not the sum of all classes, but a sum of some sampled classes.
In python using numpy would be like:
def my_softmax(X,W, num_of_samples):
K = 4
S = np.zeros(((np.dot(X,np.transpose(W))).shape))
for line in range(X.shape[0]):
XW = np.dot(X[line],np.transpose(W))
m = np.max(XW)
samples_sum = 0
for s in range(num_of_samples):
r = (randint(0,K-1))
samples_sum += np.exp(XW[r]- m)
S[line] = (np.exp(XW-m))/(samples_sum)
return S
How could this be implemented in tensorflow?
More generally, is there a possible way to create new "custom" functions like that?
You can wrap Python/numpy functions as tensorflow operators. See tf.py_func
https://www.tensorflow.org/versions/r0.9/api_docs/python/script_ops.html
However, it is better to not use it in production setting as performance will be (significantly) impacted. For most of np.* functions you will find corresponding tf.* functions that you can use. Try to represent all your computation in terms of matrix/vector instead of for loop.
Also see
https://www.tensorflow.org/versions/r0.11/api_docs/python/constant_op.html
Related
I would like to create a NumPy function that computes the Jacobian of a function at a certain point - with the Jacobian hard coded into the function.
Say I have a vector containing two arbitrary scalars X = np.array([[x],[y]]), and a function f(X) = np.array([[2xy],[3xy]]).
This function has Jacobian J = np.array([[2y, 2x],[3y, 3x]])
How can I write a function that takes in the array X and returns the Jacobian? Of course, I could do this using array indices (e.g. x = X[0,0]), but am wondering if there is a way to do this directly without accessing the individual elements of X.
I am looking for something that works like this:
def foo(x,y):
return np.array([[2*y, 2*x],[3*y, 3*x]])
X = np.array([[3],[7]])
J = foo(X)
Given that this is possible on 1-dimensional arrays, e.g. the following works:
def foo(x):
return np.array([x,x,x])
X = np.array([1,2,3,4])
J = foo(X)
You want the jacobian, which is the differential of the function. Is that correct? I'm afraid numpy is not the right tool for that.
Numpy works with fixed numbers not with variables. That is given some number you can calculate the value of a function. The differential is a different function, that has a special relationship to the original function but is not the same. You cannot just calculate the differential but must deduce it from the functional form of the original function using differentiating rules. Numpy cannot do that.
As far as I know you have three options:
use a numeric library to calculate the differential at a specific point. However you only will get the jacobian at a specific point (x,y) and no formula for it.
take a look at a pythen CAS library like e.g. sympy. There you can define expressions in terms of variables and compute the differential with respect to that variables.
Use a library that perform automatic differentiation. Maschine learning toolkits like pytorch or tensorflow have excellent support for automatic differentiation and good integration of numpy arrays. They essentially calculate the differential, by knowing the differential for all basic operation like multiplication or addition. For composed functions, the chain rule is applied and the difderential can be calculated for arbitray complex functions.
I am trying to create several functions for Linear Algebra and was completely stuck on matrix multiplication. Below is my working solution, but is there possibly a cleaner solution?
def matrix_multiplication(a, b):
# Transpose matrix b
bT = list(zip(*b))
# Multiply the two
return [[sum([a[ai][j] * bT[bTi][j] for j in range(len(a[ai]))]) for bTi in range(len(bT))] for ai in range(len(a))]
Firstof all, I would only recommend creating a linear algebra library from scratch, if you want to use it for learning purposes. Otherwise you should use numpy.linalg or something similar.
Assuming, you want to do this from scratch, I reccommend to go with object oriented programming approach. This would mean creating your own matrix class.
You can try something similar to this blog: https://towardsdatascience.com/how-to-build-a-matrix-module-from-scratch-a4f35ec28b56
If you are trying to come up with your own way of computing matrices, it may be that your loop works, but more standard "cleaner" way would be to use "numpy" module.
import numpy as np
A = np.array([[1,2,3],[2,3,4],[3,4,5]])
b = np.array([[1,-1,1]])
#Transposing
b = np.transpose(b)
c = np.matmult(A,b)
I am using a scipy.minimize function, where I'd like to have one parameter only searching for options with two decimals.
def cost(parameters,input,target):
from sklearn.metrics import mean_squared_error
output = self.model(parameters = parameters,input = input)
cost = mean_squared_error(target.flatten(), output.flatten())
return cost
parameters = [1, 1] # initial parameters
res = minimize(fun=cost, x0=parameters,args=(input,target)
model_parameters = res.x
Here self.model is a function that performs some matrix manipulation based on the parameters. Input and target are two matrices. The function works the way I want to, except I would like to have parameter[1] to have a constraint. Ideally I'd just like to give an numpy array, like np.arange(0,10,0.01). Is this possible?
In general this is very hard to do as smoothness is one of the core-assumptions of those optimizers.
Problems where some variables are discrete and some are not are hard and usually tackled either by mixed-integer optimization (working good for MI-linear-programming, quite okay for MI-convex-programming although there are less good solvers) or global-optimization (usually derivative-free).
Depending on your task-details, i recommend decomposing the problem:
outer-loop for np.arange(0,10,0.01)-like fixing of variable
inner-loop for optimizing, where this variable is fixed
return the model with the best objective (with status=success)
This will effect in N inner-optimizations, where N=state-space of your to fix-var.
Depending on your task/data, it might be a good idea to traverse the fixing-space monotonically (like using np's arange) and use the solution of iteration i as initial-point for the problem i+1 (potentially less iterations needed if guess is good). But this is probably not relevant here, see next part.
If you really got 2 parameters, like indicated, this decomposition leads to an inner-problem with only 1 variable. Then, don't use minimize, use minimize_scalar (faster and more robust; does not need an initial-point).
I have read this blog which shows how an algorithm had a 250x speed-up by using numpy. I have tried to improve the following code by using numpy but I couldn't make it work:
for i in nodes[1:]:
for lb in range(2, diameter+1):
not_valid_colors = set()
valid_colors = set()
for j in nodes:
if j == i:
break
if distances[i-1, j-1] >= lb:
not_valid_colors.add(c[j, lb])
else:
valid_colors.add(c[j, lb])
c[i, lb] = choose_color(not_valid_colors, valid_colors)
return c
Explanation
The code above is part of an algorithm used to calculate the self similar dimension of a graph. It works basically by constructing dual graphs G' where a node is connected to each other node if the distance between them is greater or equals to a given value (Lb) and then compute the graph coloring on those dual networks.
The algorithm description is the following:
Assign a unique id from 1 to N to all network nodes, without assigning any colors yet.
For all Lb values, assign a color value 0 to the node with id=1, i.e. C_1l = 0.
Set the id value i = 2. Repeat the following until i = N.
a) Calculate the distance l_ij from i to all the nodes in the network with id j less than i.
b) Set Lb = 1
c) Select one of the unused colors C[ j][l_ij] from all nodes j < i for which l_ij ≥ Lb . This is the color C[i][Lb] of node i for the given Lb value.
d) Increase Lb by one and repeat (c) until Lb = Lb_max.
e) Increase i by 1.
I wrote it in python but it takes more than a minute when try to use it with small networks which have 100 nodes and p=0.9.
As I'm still new to python and numpy I did not find the way to improve its efficiency.
Is it possible to remove the loops by using the numpy.where to find where the paths are longer than the given Lb? I tried to implement it but didn't work...
Vectorized operations with numpy arrays are fast since actual calculations are done with underlying libraries such as BLAS and LAPACK without Python overheads. With loop-intensive operations, you will not see those benefits.
You usually have to figure out a way to vectorize operations (usually possible with a smart use of array slicing). Some operations are inherently loop-intensive, however, and sometimes it is not easy to vectorize them (which seems to be the case for your code).
In those cases, you can first try Numba, which generates optimized machine code from a Python function without any modifications. (You just annotate the function and it will automatically do it for you). I do not have a lot of experience with it, and have not tried using this for complicated functions.
If this does not work, then you can use Cython, which converts Python-like code (with typed variables) into efficient C code automatically and generates a Python extension module that you can import and use in Python. That will usually give you at least an order of magnitude (usually two orders of magnitude) speedup for loop-intensive operations. I generally find Cython easy to use since unlike pure C, one can access your numpy arrays directly in Cython code.
I recommend using Anaconda Python distribution, since you will be able to install these packages easily. I'm sorry I don't have a specific answer for your code.
if you want to go to numpy, you can just change the lists into arrays,
for example distances[i-1][j-1] becomes distances[i-1, j-1] after you declare distances as a numpy array. same with c[i][lb]. About valid_colors and not_valid_colors you should think a bit more because with numpy arrays you cannot append things: the array have fixed length, so you should fix a maximum size before. Another idea is that after you have everything in numpy, you can cythonize your code http://docs.cython.org/src/tutorial/cython_tutorial.html it means that all your loops will become very fast. In any case, if you don't want cython and you look at the blog, you see that distances is declared as an array in the main()
I'm programming a scientific application in Python, and the performance of my algorithm so far is terrible. I'm trying to find an efficient way to code what I'm doing. Basically, I have to multiply
def get_thing(self, chi, n):
return np.sum(self.an[n][j] * pow(chi, -j) for j in xrange(1, self.j))
where self.an[i][j] is a previously generated array. Then I'll have to do this:
pot = np.sum(self.coeffs[n] * self.get_thing(chi, n) for n in xrange(0, self.n))
where chi changes and cannot be cached, as it's a point that is being generated outside this class. Of course, this is extremely slow and not very bright. How can I improve this?
Thanks!
Within get_things you could certainly simplify things as something like:
def get_thing(self, chi, n):
return np.sum(self.an[n,1:self.j] * np.power(chi,-np.arange(1,self.j)))
Note, that you don't want to index numpy arrays using [i][j] notation; instead use [i,j].
You may be able to make further improvements using higher level broadcasting as #eat suggested.
Edit:
Made a couple of changes to the above code to try to get the indexing to match the OP and changed a sign error in my code.
Simply, try to do the computations in higher level of abstraction, i.e. try to avoid python level looping.
Study carefully how to do element-wise operations and how broadcasting operates, and last but not least don't forget the power of linear algebra!