Python: define a log likelihood function - function

Let's say I have some data z=[1,2,3,4]
I am trying to fit this data to a model which is known, so the exercise is simply to find the value of an unknown parameter D
My log likelihood function looks like this
l(D|z)= \sum(\sqrt(z^2 + D^2))
I am trying to define this log likelihood function, z is the data which is a list and theta is the parameter vector which in this case is 1 dimensional
import scipy.optimize as op
import numpy as np
D_true = some given value
def f(z,theta):
D=theta
z2=[x**2 for x in z]
return np.sqrt(np.sum(z2 + D**2))
result = op.minimize(f, D_true,args=(z))
print result.x
But I am getting the error message unsupported operand type(s) for ** or pow(): 'list' and 'int'
and pointing towards return np.sqrt(np.sum(z2 + D**2))
Can anyone help me solve this issue?

as they say, "we have eyes, if we only but see." What I mean is, it's telling you where to look
"But I am getting the error message unsupported operand type(s) for ** or pow(): 'list' and 'int'
and pointing towards return np.sqrt(np.sum(z2 + D**2))"
and it's even telling you the problem: you cannot add a list object to an int object. By the way you wrote it, I think you are assuming Python will broadcast, but it will not do that unless at least one of the objects is a numpy (ndarray) object.
You probably wanted the value D ** 2 to be added to each entry of z2. One option is to write
return np.sqrt(np.sum(z2 + D **2 * np.ones_like(z2)))
another option is
return np.sqrt(np.sum(np.array(z2) + D ** 2))
It's awkward to me to see lists being used with numpy functions, you may consider working exclusively with numpy arrays. For example, you wouldn't have run into this problem if you opted to do that from the start.
If you had a numpy array, you can use ufuncs instead of list comprehensions.
z = np.arange(1,5)
z2 = z ** 2
is the same as
z2 = [x ** 2 for x in z]
but returns a numpy array instead of a list.

I think you should replace D = theta by D, = theta since theta is a list. This way we unpack it on the fly.

Related

Using the embedding layer as the input for an encoder

I want to use the embedding layer as the input for encoder, however I got an error as follow. My input y is a time series data with shape of 1*84. Could you please help me with that?
import numpy
import torch.nn as nn
r_input = torch.nn.Embedding(84, 10)
activation = nn.functional.relu
mu_r = nn.Linear(10, 6)
log_var_r = nn.Linear(10, 6)
y = np.random.rand(1,84)
def encode_r(y):
y = torch.reshape(y, (-1, 1, 84)) # torch.Size([batch_size, 1, 84])
hidden = torch.flatten(activation(r_input(y)), start_dim = 1)
z_mu = mu_r(hidden)
z_log_var = log_var_r(hidden)
return z_mu, z_log_var```
Error: RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got torch.cuda.FloatTensor instead (while checking arguments for embedding)
According to this thread: https://discuss.pytorch.org/t/expected-tensor-for-argument-1-indices-to-have-scalar-type-long-but-got-cpufloattensor-instead-while-checking-arguments-for-embedding/32441/4, it seems that one possible solution would be to ensure embeddings have integer values and not float values in them (by embeddings we mean the lookup table not an actual embedding vector).

matrix operation not returning correctly

R = [cos(pi/3) sin(pi/3); -sin(pi/3) cos(pi/3)]
[i,j]=round([1 1] * R)
returns
i =
-0 1
error: element number 2 undefined in return list
While I want i=0 and j=1
Is there a way to work around that? Or just Octave being stupid?
Octave is not being stupid; it's just that you expect the syntax [a,b] = [c,d] to result in 'destructuring', but that's not how octave/matlab works. Instead, you are assigning a 'single' output (a matrix) to two variables. Since you are not generating multiple outputs, there is no output to assign to the second variable you specify (i.e. j) so this is ignored.
Long story short, if you're after a 'destructuring' effect, you can convert your matrix to a cell, and then perform cell expansion to generate two outputs:
[i,j] = num2cell( round( [1 1] * R ) ){:}
Or, obviously, you can collect the output into a single object, and then assign to i, and j separately via that object:
[IJ] = round( [1 1] * R ) )
i = IJ(1)
j = IJ(2)
but presumably that's what you're trying to avoid.
Explanation:
The reason [a,b] = bla bla doesn't work, is because syntactically speaking, the [a,b] here isn't a normal matrix; it represents a list of variables you expect to assign return values to. If you have a function or operation that returns multiple outputs, then each output will be assigned to each of those variables in turn.
However, if you only pass a single output, and you specified multiple return variables, Octave will assign that single output to the first return variable, and ignore the rest. And since a matrix is a single object, it assigns this to i, and ignores j.
Converting the whole thing to a cell allows you to then index it via {:}, which returns all cells as a comma separated list (this can be used to pass multiple arguments into functions, for instance). You can see this if you just index without capturing - this results in 'two' answers, printed one after another:
num2cell( round( [1 1] * R ) ){:}
% ans = 0
% ans = 1
Note that many functions in matlab/octave behave differently, based on whether you call them with 1 or 2 output arguments. In other words, think of the number of output arguments with which you call a function to be part of its signature! E.g., have a look at the ind2sub function:
[r] = ind2sub([3, 3], [2,8]) % returns 1D indices
% r = 2 8
[r, ~] = ind2sub([3, 3], [2,8]) % returns 2D indices
% r = 2 2
If destructuring worked the way you assumed on normal matrices, it would be impossible to know if one is attempting to call a function in "two-outputs" mode, or simply trying to call it in "one-output" mode and then destructure the output.

multipling matrix elements creating a function and using repetition structures

I have the following exercise:
Make a function that takes two parameters, a NumPy matrix and a constant, and uses repetition structures to multiply each element of the matrix and returns the multiplied matrix.
I did it just using repetition structures:
np.random.seed(0)
matriz = np.random.randint (1,30, (3,4))
constante = 4
for i in matrix:
print (i * constante)
Can anyone help me to solve it right?
thanks
The question (which is a terrible exercise, you'll see why in a minute) is trying to help you get used to for loops. The array you're working with has two dimensions, so you need to loop over row indices i and column indices j to access each of the entries in the matrix. You can get the number of rows and columns of the array with the .shape attribute, so you don't have to know the shape of the array beforehand.
Here's one solution that modifies the matrix in-place, meaning the original matrix gets overwritten.
>>> def scale_matrix(A, c):
... for i in range(A.shape[0]): # Loop over row indices.
... for j in range(A.shape[1]): # Loop over column indices.
... A[i,j] = c * A[i,j] # Multiply the entry and store the result in the same spot.
... return A
BUT
This is a futile exercise, because when you multiply a NumPy array by a constant, it multiplies each entry of the array. No need for looping. This way is also faster and much more readable.
>>> import numpy as np
>>> A = np.array([[1, 2],
... [3, 4]])
>>> A * 2
array([[2, 4],
[6, 8]])

How can a function return if a number is a factor of another?

I'm writing a function for Python 3.7.3 that tests if a number is a factor of another number.
I tried researching on the internet to find some idea about how the write a function that tests the validity of factoring two unknown real numbers. I ended up stumbling upon the difference between factoring and divisibility, which intrigued me somewhat.
def is_factor(f, n):
"""This function returns if f, a real number, is a factor of another
real number n."""
while f * f <= n:
if f % n == 0:
f /= n
#return True?
else: f += 1 #return False?
print(is_factor(1, 15))
The function appears to work, because Python returns None, and that's all. I expect the function to return a True or False solution. There must be some logical error in the code. Any feed back is appreciated.
If you are dealing with integers, use:
def is_factor(f, n):
return n%f==0
If you are dealing with real numbers, the above code works but is very sensitive to floating point imprecision. Instead, you can divide n by f and see if you get back n after rounding to the nearest integer:
def is_factor(f, n, e):
return abs(round(n/f)*f-n)<e

mxnet failed to infer type

My mxnet code - which consists of a series of complex connections and slicing raises the following error:
Error in operator concat0: [03:03:51] src/operator/./concat-inl.h:211: Not enough information to infer type in Concat.
Im not sure how to interpret that or what information to provide to help debug it. Concat0 is part of the operation:
# Define take_column function as transpose(take(transpose(x), i))
for i in range(47):
y_hat_lt = take_column(y_hat,
mx.sym.concat(mx.sym.slice(some_indices, begin=i, end=i+1), self.label_dim + mx.sym.slice(some_indices, begin=i, end=i+1), dim=0))
here some_indices is a variable which I fix to be a list. Do let me know!
It looks like MXNet is not able to infer the shape of output. Did you specify the shape for variable some_indices?
e.g. some_indices = mx.sym.var('indices', shape=(1,1))
It would be nice if you can paste a minimum reproducible code :)
Instead of taking transpose, swapping among the axis resolved the issue.
def ttake( x, i ):
""" Take from axis 1 instead of 0.
"""
a = mx.sym.swapaxes(x, dim1=0, dim2=1)
return mx.sym.flatten( mx.sym.transpose( mx.sym.take( a , i ) ) )