RuntimeError: ffi_prep_cif failed - ctypes

I am working on a project where I have to use ctypes to call a few C functions from a C file in a Python script. I have created a shared object from the C file and used it inside my Python script.
I have defined the C struct in ctypes, but I am getting a RuntimeError while running my Python script. The error is:
ffi_prep_cif failed
I have included ffi.h header in my C file and again created the shared object, but I'm still getting the same error. Any suggestions?
The code which I have written for the wrapper in Python is as follows:
if __name__ == "__main__":
lib_fib = ctypes.CDLL('./lib6.so')
class model_t(ctypes.Structure):
_fields__ = [('weight', ctypes.POINTER(ctypes.c_float)),
('threshold', ctypes.c_float)]
data = [1.0, 2.0, 3.0]
f1 = 2.5
l = len(data)
m = model_t()
m.weight = (ctypes.c_float * l)(*data)
m.threshold = (ctypes.c_float * 1)(l)
chr_p = ctypes.POINTER(ctypes.c_char)
c = 'abcd'
l1 = len(c)
chr_p = (ctypes.c_char * l1)(*c)
out = lib_fib.new_classify(m, chr_p)
print out`
I am trying to replace Python function called self.abc_prob() with C new_classify(). This is a part of my C function, which I want to call using ctypes:
extern double
new_classify(model_t *model_ptr, char *s)
{
char bigram[3]; /* Used only when debugging */
float *model_weights = model_ptr->weights;
float threshold = model_ptr->threshold;
/* ... */

Related

Substituting undefined function in symbolic expression

In an expression depending on an unknown function f of r**2, I would like to replace the function f by some actual function and display the result but I cannot find a way to do it. Here's an example:
r = symbols('r', real= True)
phi = r * Function('phi')(r**2)
dphi = phi.diff(r)
print(dphi)
At this stage I get:
2*r**2*Subs(Derivative(phi(_xi_1), _xi_1), _xi_1, r**2) + phi(r**2)
Now let's assume I would like to evaluate dphi when phi(y) = y.
This should give me:
2*r**2 + r**2 = 3*r**3
How do I make the actual substitution of phi in dphi to obtain the desired result ?
#Davide_sd This is an example that works as I expect (but for a function of r alone):
r = symbols('r', real= True)
phi = Function('phi')(r)
om = r * phi
dom = om.diff(r)
dom.subs(phi, sin(r)).doit()
output: r * cos(r) +sin(r)
But I would like to have for example (does not work):
r = symbols('r', real= True)
phi = Function('phi')(r**2)
om = r * phi
dom = om.diff(r)
dom.subs(phi, sin(r)).doit()
output: 2*r**2*Subs(Derivative(phi(_xi_1), _xi_1), _xi_1, r**2) + phi(r**2)
Instead I would like to get:
2*r**2*cos(r**2) + sin(2*r**2)
Thanks in advance for any help,
Regards,
Bernard.
If you want phi(y) = y this is the Id function; replacement can be done as:
>>> from sympy import Id
>>> dphi.subs(Function('phi'), Id).doit() # where dphi is as given at the start
3*r**2
In your example for dom it is not clear what function mapping you want since you use phi for the expression Function('phi')(r**2) and for the function Function('phi'). If you mean the same as in the first example -- something like phi(r) = sin(r) then you would just replace Function('phi') with sin.
Another way to approach this is by using replace to find occurrences of the function of interest and replace them with desired value, regardless of argument.
>>> p = Function('phi')
>>> (1+p(x)).replace(lambda x:x.func==p, lambda x:x.args[0]**3)
x**3 + 1
Based on the above answer and another usage of replace that I discovered here (Why does substitution into a sympy derivative only partly work), here is a summary of two solutions that do exactly what I need:
>>> r = symbols('r', real= True)
>>> phi = Function('phi')
>>> om = r * phi(r**2)
>>> dom = om.diff(r)
# Solution 1
>>> dom.replace(lambda x: x.func == phi, lambda x: x.args[0]**3).doit()
7*r**6
# Solution 2
>>> y = Wild("y")
>>> dom.replace(phi(y), y**3).doit()
7*r**6

CNTK Providing final LSTM state as input to another LSTM

I'm trying to build the following architecture:
Embeddings -> Forward LSTM -> Backward LSTM -> Concat Final States -> Concat external embedding -> LSTM
My code looks like this:
i = Input(features)
e = Embedding(25)(i)
# Forward and backward LSTM
h_f = Recurrence(LSTM(25), go_backwards=False)(e)
h_b = Recurrence(LSTM(25), go_backwards=True)(e)
# Get the final states and splice
f = sequence.last(h_f)
b = sequence.first(h_b)
e1 = splice(f, b)
# Get the other embedding and concat
i2 = Input(100)
e2 = Embedding(100)(i2)
e2 = sequence.first(word_embedding)
e3 = splice(e1, e2)
# Input concatenated embedding to new LSTM
r = Recurrence(LSTM(50))(e3)
When I do this I get the following error:
Input operand 'Output('Block1994_Output_0', [#], [50])' with #dynamic axes != 2 (1 sequence axis and 1 batch axis) is not supported.
If I do not get the final state of my first bidirectional LSTM then it works fine, but that's not what I want.
I can also reproduce the error with this simple example:
i = Input(features)
e = Embedding(25)(i)
h_f = Fold(LSTM(25), go_backwards=False)(e)
s = Recurrence(LSTM(25))(h_f)
check this link about returning output sequences and states of an LSTM. Then, you can bind the output to the input of another layer.

Error: 'multiclass.roc.default' is not an exported object from 'namespace:pROC'

I'm using mlr R package (v2.3), trying to use multiclass auc as one of the measures when using CV in a 3-class classification problem. But, I'm getting the following error:
Error: 'multiclass.roc.default' is not an exported object from 'namespace:pROC'
I believe I have all the dependencies installed already (and don't believe that is the actual issue). The error is produced when trying to run the 'resample' function, as follows:
tsk <- makeClassifTask(data = dat, target = "best.model")
lrn <- makeLearner("classif.rpart", predict.type = "prob")
rdesc <- makeResampleDesc("CV", iters=10)
r <- resample(learner = lrn, task = tsk, resampling = rdesc, show.info = T, measures = list(acc, multiclass.auc))
Any suggestion?
This works fine with the latest version of mlr (2.4).

PyMC3 how to implement latent dirichlet allocation?

I am trying to implement lda using PyMC3.
However, when defining the last part of the model in which words are sampled based on their topics, I keep getting the error: TypeError: list indices must be integers, not TensorVariable
How to tackle the problem?
The code is as follows:
## Data Preparation
K = 2 # number of topics
N = 4 # number of words
D = 3 # number of documents
import numpy as np
data = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]])
Wd = [len(doc) for doc in data] # length of each document
## Model Specification
from pymc3 import Model, Normal, HalfNormal, Dirichlet, Categorical, constant
lda_model = Model()
with lda_model:
# Priors for unknown model parameters
alpha = HalfNormal('alpha', sd=1)
eta = HalfNormal('eta', sd=1)
a1 = eta*np.ones(shape=N)
a2 = alpha*np.ones(shape=K)
beta = [Dirichlet('beta_%i' % i, a1, shape=N) for i in range(K)]
theta = [Dirichlet('theta_%s' % i, a2, shape=K) for i in range(D)]
z = [Categorical('z_%i' % d, p = theta[d], shape=Wd[d]) for d in range(D)]
# That's when you get the error. It is caused by: beta[z[d][w]]
w = [Categorical('w_%i_%i' % (d, w), p = beta[z[d][w]], observed = data[i,j]) for d in range(D) for w in range(Wd[d])]
Any help would be much appreciated!
beta[z[d][w]] is naturally incorrect because z[d][w] is a variable stored by PyMC instead of being an fixed index.
In pymc2 it is solved by lambda function
p=pm.Lambda("phi_z_%s_%s" % (d,i),
lambda z=z[d][w], beta=beta: beta[z])
In pymc3 it is suppose to be solved by
#theano.compile.ops.as_op
def your_function
But there is a problem here that it seems like Theano doesn't allow sending a python list of pymc variable. t.lvector baisically don't work.
More discussion is in this question:
Unable to create lambda function in hierarchical pymc3 model
The following code was adapted from what has been referenced by #Hanan. I've somehow made it work with pymc3.
import numpy as np
import pymc3 as pm
def get_word_dict(collection):
vocab_list = list({word for doc in collection for word in doc})
idx_list = [i for i in range(len(vocab_list))]
return dict(zip(vocab_list,idx_list))
def word_to_idx(dict_vocab_idx, collection):
return [[dict_vocab_idx[word] for word in doc] for doc in collection]
docs = [["sepak","bola","sepak","bola","bola","bola","sepak"],
["uang","ekonomi","uang","uang","uang","ekonomi","ekonomi"],
["sepak","bola","sepak","bola","sepak","sepak"],
["ekonomi","ekonomi","uang","uang"],
["sepak","uang","ekonomi"],
["komputer","komputer","teknologi","teknologi","komputer","teknologi"],
["teknologi","komputer","teknologi"]]
dict_vocab_idx = get_word_dict(docs)
idxed_collection = word_to_idx(dict_vocab_idx, docs)
n_topics = 3
n_vocab = len(dict_vocab_idx)
n_docs = len(idxed_collection)
length_docs = [len(doc) for doc in idxed_collection]
alpha = np.ones([n_docs, n_topics])
beta = np.ones([n_topics, n_vocab])
with pm.Model() as model:
theta = pm.distributions.Dirichlet('theta', a=alpha, shape=(n_docs, n_topics))
phi = pm.distributions.Dirichlet('phi', a=beta, shape=(n_topics, n_vocab))
zs = [pm.Categorical("z_d{}".format(d), p=theta[d], shape=length_docs[d]) for d in range(n_docs)]
ws = [pm.Categorical("w_{}_{}".format(d,i), p=phi[zs[d][i]], observed=idxed_collection[d][i])
for d in range(n_docs) for i in range(length_docs[d])]
trace = pm.sample(2000)
for d in range(n_docs):
value_z=trace.get_values("z_d{}".format(d))
print(value_z[1999])
check out this blog post. I haven't tested it.
import numpy as np
import pymc as pc
def wordDict(collection):
word_id = {}
idCounter = 0
for d in collection:
for w in d:
if (w not in word_id):
word_id[w] = idCounter
idCounter+=1
return word_id
def toNpArray(word_id, collection):
ds = []
for d in collection:
ws = []
for w in d:
ws.append(word_id.get(w,0))
ds.append(ws)
return np.array(ds)
###################################################
#doc1, doc2, ..., doc7
docs = [["sepak","bola","sepak","bola","bola","bola","sepak"],
["uang","ekonomi","uang","uang","uang","ekonomi","ekonomi"],
["sepak","bola","sepak","bola","sepak","sepak"],
["ekonomi","ekonomi","uang","uang"],
["sepak","uang","ekonomi"],
["komputer","komputer","teknologi","teknologi","komputer","teknologi"],
["teknologi","komputer","teknologi"]]
word_dict = wordDict(docs)
collection = toNpArray(word_dict,docs)
#number of topics
K = 3
#number of words (vocab)
V = len(word_dict)
#number of documents
D = len(collection)
#array([1, 1, 1, ..., 1]) K times
alpha = np.ones(K)
#array([1, 1, 1, ..., 1]) V times
beta = np.ones(V)
#array containing the information about doc length in our collection
Nd = [len(doc) for doc in collection]
######################## LDA model ##################################
#topic distribution per-document
theta = pc.Container([pc.CompletedDirichlet("theta_%s" % i,
pc.Dirichlet("ptheta_%s"%i, theta=alpha))
for i in range(D)])
#word distribution per-topic
phi = pc.Container([pc.CompletedDirichlet("phi_%s" % j,
pc.Dirichlet("pphi_%s" % j, theta=beta))
for j in range(K)])
#Please note that this is the tricky part :)
z = pc.Container([pc.Categorical("z_%i" % d,
p = theta[d],
size = Nd[d],
value = np.random.randint(K, size=Nd[d]))
for d in range(D)])
#word generated from phi, given a topic z
w = pc.Container([pc.Categorical("w_%i_%i" % (d,i),
p = pc.Lambda("phi_z_%i_%i" % (d,i),
lambda z=z[d][i], phi=phi : phi[z]),
value=collection[d][i],
observed=True)
for d in range(D) for i in range(Nd[d])])
####################################################################
model = pc.Model([theta, phi, z, w])
mcmc = pc.MCMC(model)
mcmc.sample(iter=5000, burn=1000)
#show the topic assignment for each word, using the last trace
for d in range(D):
print(mcmc.trace('z_%i'%d)[3999])

"NameError" when supplying variable name to input() prompt

I have this simple bit of code I can't get to work any advice?
a = 2
b = 4
c = input()
d = 0
d = c + 5
print(d)
Say I input a, so 2, I should get 7. But I don't. This Python 3. Using Wing IDE 101 (ver. 5) here. I get this as my error output.
Traceback (most recent call last):
File "", line 1, in
builtins.NameError: name 'a' is not defined
Are you sure you are using Python 3? In python 2.x you can do it by explictly evaluating an string expression with the eval() function:
c = eval(raw_input()) # Python 2.7
c = eval(input()) # Python 3.x
In Python 3.x input() will convert the input in a string and won't raise that error (NameError). It will raise a TypeError instead, because you cannot concatenate str and int that way.
you can just try c = raw_input()