I'm working on FEM analysis. I just wanted to evaluate a simple matrix multiplication and see the numeric result. How can I see the elements of the sparse matrix?
the code that I have used for is:
U_h= 0.5 * np.dot(np.dot(U[np.newaxis], K), U[np.newaxis].T)
Since U is a 1x3 matrix, K is 3x3 matrix and U.T is 3x1 matrix, I expect a 1x1 matrix with a single number in it. However, the result is "[[<3x3 sparse matrix of type 'class 'numpy.float64' with 3 stored elements in Compressed Sparse Row format>]]"
In [260]: M = sparse.random(5,5,.2, format='csr')
What you got was the repr format of the matrix:
In [261]: M
Out[261]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [262]: repr(M)
Out[262]: "<5x5 sparse matrix of type '<class 'numpy.float64'>'\n\twith 5 stored elements in Compressed Sparse Row format>"
The str format used print is:
In [263]: print(M)
(1, 0) 0.7152749140462651
(1, 1) 0.4298096228326874
(1, 3) 0.8148327301300698
(4, 0) 0.23366934073409018
(4, 3) 0.6117499168861333
In [264]: str(M)
Out[264]: ' (1, 0)\t0.7152749140462651\n (1, 1)\t0.4298096228326874\n (1, 3)\t0.8148327301300698\n (4, 0)\t0.23366934073409018\n (4, 3)\t0.6117499168861333'
If the matrix isn't big, displaying it as a dense array is nice. M.toarray() does that, or for short:
In [265]: M.A
Out[265]:
array([[0. , 0. , 0. , 0. , 0. ],
[0.71527491, 0.42980962, 0. , 0.81483273, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0.23366934, 0. , 0. , 0.61174992, 0. ]])
for a graphical inspection use plt.spy()
see an applied example here
see the reference manual here
Related
I finally have fist SSD_mobilnet_v2 model started.
[https://tfhub.dev/iree/lite-model/ssd_mobilenet_v2_100/fp32/default/1
But compare with other models of SSD_mobilnet_v1, the outoput is not decoded and without NMS. As described in the web,
raw_outputs/box_encodings: an fp32 array of shape [1, M, 4] containing
decoded detection boxes without Non-Max suppression. M is the number
of raw detections.
raw_outputs/class_predictions: a fp32 array of
shape [1, M, 91] and contains class logits for raw detection boxes.
Score conversion layer not included. M is the number of raw
detections.
could anyone help me understand more how should I do next step to transfer the raw outout bounding box relative coords and raw detections to final bounding box and scores?
below is what I have for relative coords
((1, 8004, 4), array([[[ 1.0489864 , 0.2852646 , -7.456381 ,
-4.7595205 ],
[-0.35197747, 1.0335344 , -2.378837 , -0.6666372 ],
[ 0.8332888 , 0.8247721 , -0.3024159 , -3.290514 ],
...,
[ 0.1398478 , -0.25943977, 0.29718816, -1.410246 ],
[-0.1175375 , -0.08291922, -1.461216 , 0.22000816],
[ 0.13272771, -0.51625276, -0.5618129 , -1.0699694 ]]],
dtype=float32))
what is the meaning of the 4 channels for each prediction bounding box? why some values are negative?
Regarding raw predictions, for each bounding box we have predictions of 8004 bounding box, and total 91 classes. but why we see negative probabilities?
The probability should be in the range from 0-1?
((1, 8004, 91), array([[[-9.669849 , -4.4239364, -4.8566256, ...,
-5.8348265,
-5.1578894, -4.801747 ],
[-9.669044 , -5.7234015, -6.342394 , ..., -7.4027104,
-6.728588 , -6.4829254],
[-9.590075 , -5.214742 , -6.874845 , ..., -6.9183044,
-6.844805 , -6.4774513],
...,
[-4.8303924, -5.1854134, -4.871473 , ..., -4.9025354,
-4.829895 , -4.7791467],
[-4.830332 , -4.9423876, -4.8391323, ..., -4.9813066,
-4.8254986, -4.7414174],
[-4.832156 , -4.925433 , -4.9521995, ..., -5.1177106,
-4.7640305, -4.6455407]]], dtype=float32))
I sense I need to understand more about basic definitions of SSD mobilenet models. Could anyone be kind and give me some guide?
some reference links or a small pieces of code would be very helpful for me to understand.
thanks in advance!
regards
Cliff
I would like to obtain a tensordot of two arrays with the same shape with index-dependent weight applied, without use of explicit loop. For example,
import numpy as np
A=np.array([1,2,3])
B=np.array([-2,6,9])
C=np.zeros((3,3))
for i in range(3):
for j in range(3):
C[i,j]=A[i]*B[j]*(np.exp(i-j)if i>j else 0)
Can an array similar to C be obtained with a built-in tool (e.g., with some options for tensordot)?
Here's a vectorized solution:
N = 3
C = np.tril(A[:, None] * B * np.exp(np.arange(N)[:, None] - np.arange(N)), k=-1)
Output:
>>> C
array([[ -2. , 0. , 0. ],
[-10.87312731, 12. , 0. ],
[-44.33433659, 48.92907291, 27. ]])
With np.einsum inconsistently slightly faster for some larger inputs than broadcasting, slower for others.
import numpy as np
A=np.array([1,2,3])
B=np.array([-2,6,9])
np.einsum('ij,i,j->ij', np.tril(np.exp(np.subtract.outer(A,A)), -1), A, B)
Output
array([[ 0. , 0. , 0. ],
[-10.87312731, 0. , 0. ],
[-44.33433659, 48.92907291, 0. ]])
Question
What numpy function to use for mathematical dot product in the case below?
Backpropagation for a Linear Layer
Define sample (2,3) array:
In [299]: dldx = np.arange(6).reshape(2,3)
In [300]: w
Out[300]:
array([[0.1, 0.2, 0.3],
[0. , 0. , 0. ]])
Element wise multiplication:
In [301]: dldx*w
Out[301]:
array([[0. , 0.2, 0.6],
[0. , 0. , 0. ]])
and summing on the last axis (size 3) produces a 2 element array:
In [302]: (dldx*w).sum(axis=1)
Out[302]: array([0.8, 0. ])
Your (6) is the first term, dropping the 0. One might argue that the use of a dot/inner in (5) is a bit sloppy.
np.einsum borrows ideas from physics, where dimensions may be higher. This case can be expressed as
In [303]: np.einsum('ij,ik->i',dldx,w)
Out[303]: array([1.8, 0. ])
inner and dot do more calculations that we want. We just want the diagonal:
In [304]: np.dot(dldx,w.T)
Out[304]:
array([[0.8, 0. ],
[2.6, 0. ]])
In [305]: np.inner(dldx,w)
Out[305]:
array([[0.8, 0. ],
[2.6, 0. ]])
In matmul/# terms, the size 2 dimension is a 'batch' one, so we have to add dimensions:
In [306]: dldx[:,None,:]#w[:,:,None]
Out[306]:
array([[[0.8]],
[[0. ]]])
This is (2,1,1), so we need to squeeze out the 1s.
I did this using a for loop and feel like there is a faster way to achieve it but it eludes me.
datal=[[-9.8839112e-05, -0.001128727317, -0.000197679149],
[-0.0009201639200000001, 0.0005601014289999999, 0.000496686232],
[-0.000184700668, 9.414391600000001e-05, 0.000409526574]]
bigtranfo=[array([[ 0.89442732, 0. , 0.44721334],
[ 0.44721334, 0. , -0.89442732],
[-0. , 1. , 0. ]]),
array([[ 0.27639329, 0.85065091, 0.44721334],
[ 0.13819655, 0.42532516, -0.89442732],
[-0.9510565 , 0.30901705, 0. ]]),
array([[-0.72360684, 0.52573128, 0.44721334],
[-0.36180316, 0.26286545, -0.89442732],
[-0.58778535, -0.80901692, 0. ]])]
vectorfield=[]
for i in range(0,3):
x=list(bigtransfo[i].dot(datal[['tx','ty','tz']].iloc[i]))
vectorfield.append(x)
vf=pd.DataFrame(vectorfield,columns=['tx','ty','tz'])
output:
[[-0.00017680915486414586, 0.00013260746120237444, -0.001128727317],
[0.00044424836491567196, -0.0003331879850180065, 0.0010482087675674915],
[0.000366290815094829, -0.00027471928608663234, 3.240032593749042e-05]]
bigtransfo is an object containing 800 3x3 arrays, transformations. datal is just a chunk of a data frame that has 800 rows and three columns. The idea is to multiply the three components of each row, the selected vector, by the corresponding transformation.
Any ideas are welcome. Thanks in advance.
update: Added working example.
I am new to matplotlib, and have get stuck in colormaps.
In matplotlib how do I get the whole array of RGB colors for a specific colormap, let's say for "hot". For example if I was in MATLAB I would have just done this:
# in matlab
c = hot(256);
disp(c)
Any ideas?
You can look up the values by calling the colormap as a function, and it accepts numpy arrays to query many values at once:
In [12]: from matplotlib import cm
In [13]: cm.hot(range(256))
Out[13]:
array([[ 0.0416 , 0. , 0. , 1. ],
[ 0.05189484, 0. , 0. , 1. ],
[ 0.06218969, 0. , 0. , 1. ],
...,
[ 1. , 1. , 0.96911762, 1. ],
[ 1. , 1. , 0.98455881, 1. ],
[ 1. , 1. , 1. , 1. ]])
Got it! So you just go in the command window of your Matlab and type
cmap = colormap(nameOfTheColormapYouWant)
Possible colormap in Matlab are: parula, jet, hsv, hot, cool, spring, summer,autumn,winter, gray, bone, copper, pink, lines, colorcube, prism, flag.
You get a matrix where each row is the color code used for the colormap.