Expand dim of integer


I am trying to access values of an array and to concatenate them afterwards. I have tried to use np.expand_dims(np.array(my_value)) and then concatenate them but it is then perceived as trying to return multiple ciphertexts :

import numpy as np
import concrete.numpy as cnp
import time

cfg = cnp.Configuration(show_graph=True, enable_unsafe_features=True)

@cnp.compiler({"x": "encrypted"})
def f(x):
    y = np.array(x[0])
    y = np.expand_dims(y,axis=0)
    for i in range(1,x.shape[0]):
        y = np.concatenate((y,np.expand_dims(np.array(x[i]),axis=0)),axis=0 )
    return y

inputset = np.random.randint(0,2,(5,3))
inpt = np.random.randint(0,2,(3))


t = time.time()
circuit = f.compile(inputset, configuration=cfg)
print('compiled in ', time.time()-t)
t = time.time()

enc = circuit.encrypt(inpt)
res_enc = circuit.run(enc)
dec = circuit.decrypt(res_enc)

which gives the following output when trying to compute print(f(inpt)):

ValueError: Value cannot represent array([<concrete.numpy.tracing.tracer.Tracer object at 0x7f08d0ba8be0>,
       <concrete.numpy.tracing.tracer.Tracer object at 0x7f08d0b3a160>,
       <concrete.numpy.tracing.tracer.Tracer object at 0x7f0858db11c0>],

I would like to know if there is a workaround ?

I am using concrete-numpy=0.11.0


Hello @tricycl3,

The problem is that you’re trying to create a numpy array of an abstract object. During the initial step of tracing, x[0] is basically a special object to let us track what you’re doing with it. So when you do np.array(x[0]), you create a numpy array of dtype=object. It’s like you did np.array({"example": "yes"}).

The solution is very easy though, you can replace usages of np.array(value) with cnp.array([value]) and it’ll work :slight_smile:

As a note, it seems this code is transforming [1, 2, 3] to [[1], [2], [3]] which you can do with np.reshape(x, (-1, 1)) in your code.

Hope this helps!

Thanks a lot, I did not know the existence of the cnp.array method !

@umutsahin knows everything!