In Pytorch, when values are divided by zero, replace the result value with 0, as it will output NaN. Here is an example,
a = th.from_numpy(np.array([ [1, 0], [0, 1], [1, 1]]))
b = th.zeros_like(a)
b[0, :] = 2
a = a / b
How can I do that?
You can replace NaN values obtained after division with 0 using the following method -
Create a ByteTensor indicating the positions of NaN
a != a
>> tensor([[False, False],
[ True, False],
[False, False]])
Replace NaN values indicated by above Tensor with 0
a = a / b
>> tensor([[0.5000, 0.0000],
[ nan, inf],
[ inf, inf]])
a[a != a] = 0
>> tensor([[0.5000, 0.0000],
[0.0000, inf],
[ inf, inf]])
Note this will also replace any NaN values introduced before division.
Related
I have two lists.
a_num = [1, 3, 2, 4]
b_num = [1, 2, 3, 4]
I want to find a permutation matrix to convert a to b. Mathematically, a permutation matrix is a square matrix, whose elements are either 1 or 0. It can change the sequence of elements in a vector, by multiplying it.
In this particular example, the permutation matrix is:
p = [[1,0,0,0],
[0,0,1,0],
[0,1,0,0],
[0,0,0,1]]
# check whether p is correct.
b_num == np.dot(np.array(p), np.array(a_num).reshape(4,1))
Could you please show me how to make that matrix p? In my real application, there can be tens of elements in the lists with arbitrary sequence. And the two lists always contain str instead of int.
And how to make p when the a and b are lists of str?
a_str = ['c1', 'c2', 's1', 's2']
b_str = ['c1', 's1', 'c2', 's2']
In pure Python you can do:
a_str = ['c1', 'c2', 's1', 's2']
b_str = ['c1', 's1', 'c2', 's2']
from collections import defaultdict
dd = defaultdict(lambda: [0, []])
for i, x in enumerate(b_str): # collect indexes of target chars
dd[x][1].append(i)
matrix = [[0]*len(a_str) for x in b_str]
for i, a in enumerate(a_str):
# set cell at row (src index) and col (next tgt index) to 1
matrix[i][dd[a][1][dd[a][0]]] = 1
# increment index for looking up next tgt index
dd[a][0] += 1
matrix
# [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
This assumes that a_str and b_str are in fact respective permutations.
Edit: works only with numbers.
For arrays with the same length using NumPy:
import numpy as np
a_num = [1, 3, 2, 4]
b_num = [4, 2, 3, 1]
a = np.array(a_num)
b = np.array(b_num)
len_v = len(a) # = len(b)
A = np.zeros((len_v, len_v))
A[np.argsort(a), np.arange(len_v)] = 1
B = np.zeros((len_v, len_v))
B[np.argsort(b), np.arange(len_v)] = 1
np.dot(np.linalg.inv(B), np.dot(A, a)) # array([4., 2., 3., 1.])
Please consider the following code,
x = tf.constant([[[1, np.nan, np.nan], [4, 3, -1]], [[10, np.nan, 3], [20,5,-7]], [[5, np.nan, 3], [np.nan,15,-17]]])
x_max = tf.reduce_max(x, reduction_indices=[0])
with tf.Session() as sess:
print (np.shape(sess.run(x)))
print (sess.run(x))
print (sess.run(x_max))
The output is as following:
(3, 2, 3)
[[[ 1. nan nan]
[ 4. 3. -1.]]
[[ 10. nan 3.]
[ 20. 5. -7.]]
[[ 5. nan 3.]
[ nan 15. -17.]]]
[[ 10. -inf 3.]
[ 20. 15. -1.]]
Now my question is how tensorflow deals with np.nan, like numpy.nanmax or similar?
Quoting this link (credit goes to Yaroslav Bulatov):
Different parts of TensorFlow treat them differently:
* Float computations (usually?) propagate them.
* Int conversion treats them as 0.
* Int computations fail with Python parts of TensorFlow often raise an error on "NaN", ie, trying to add a NaN summary to histogram will fail with Python
exception.
Here is an example for some float operations:
a = tf.constant([1.0, np.nan])
b = tf.constant(np.nan)
r = tf.reduce_min(a)
m = a * b
with tf.Session() as sess:
print(sess.run(r)) # prints 1.0
print(sess.run(m)) # array([nan, nan], dtype=float32)
I want to compare two array(4 floating point)and print mismatched items.
I used this code:
>>> from numpy.testing import assert_allclose as np_assert_allclose
>>> x=np.array([1,2,3])
>>> y=np.array([1,0,3])
>>> np_assert_allclose(x,y, rtol=1e-4)
AssertionError:
Not equal to tolerance rtol=0.0001, atol=0
(mismatch 33.33333333333333%)
x: array([1, 2, 3])
y: array([1, 0, 3])
the problem by this code is with big array:
(mismatch 0.0015104228617559556%)
x: array([ 0.440088, 0.35994 , 0.308225, ..., 0.199546, 0.226758, 0.2312 ])
y: array([ 0.44009, 0.35994, 0.30822, ..., 0.19955, 0.22676, 0.2312 ])
I can not find what values are mismatched. how can see them ?
Just use
~np.isclose(x, y, rtol=1e-4) # array([False, True, False], dtype=bool)
e.g.
d = ~np.isclose(x, y, rtol=1e-4)
print(x[d]) # [2]
print(y[d]) # [0]
or, to get the indices
np.where(d) # (array([1]),)
I have got index of 0 elements of Numpy Matrix (M) using:
index_array = numpy.argwhere(M == 0)
Now, I want to make these index elements (index present in index_array) as 0 in other matrix B. Is there any numpy way to do this?
For eg : index_array contains
[[2 1]
[4 4]]
, so make element present at (2,1) and (4,4) in Matrix B as 0.
You should have used np.where which returns a tuple of row and col index and thus can be used as indexing directly, instead of argwhere, so long as the index is not out of bound for B, you can do:
B[np.where(M == 0)] = 0
Example:
M = np.array([[1,2],[3,0],[0,1]])
M
#array([[1, 2],
# [3, 0],
# [0, 1]])
B = np.array([[1,2,3],[4,5,6],[7,8,9]])
B
#array([[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]])
B[np.where(M == 0)] = 0
B
#array([[1, 2, 3],
# [4, 0, 6],
# [0, 8, 9]])
If you want to stick to np.argwhere, you can get the row index and col index respectively and then do the assignment:
index_array = np.argwhere(M == 0)
B[index_array[:,0], index_array[:,1]] = 0
All, I've looked at the following answer and it all looks good.
Matrix multiplication, solve Ax = b solve for x
I must be doing something wrong though if I work out the problem by hand I'm getting a different solution.
Method 1:
A = [[0,1,0],
[0,0,1],
[.5,.5,0]]
b = [1,1,1]
x = numpy.linalg.lstsq(A,b)
x
yields
(array([ 1., 1., 1.]),
array([], dtype=float64),
3,
array([ 1.14412281, 1. , 0.43701602]))
Method 2 (As suggested by Kevin below I transposed the matrix):
A = [[0,0,.5],[1,0,.5],[0,1,0]]
b = [1,1,1]
x = numpy.linalg.lstsq(A,b)
x
yields
(array([ 0., 1., 2.]),
array([], dtype=float64),
3,
array([ 1.14412281, 1. , 0.43701602]))
If I work out Ax=b by hand I get x = 1/5*[1,2,2]. Note that I am working example 11.19 from the link below:
https://www.probabilitycourse.com/chapter11/11_3_2_stationary_and_limiting_distributions.php
What am I missing?
If I follow the example on this link: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html
3x+y=9
x+2y=8
[x,y]=[2,3]
I do get the right solution if I do it with python or by hand. Any pointers? I must be missing something very simple here
I think when you're doing it by hand you're mixing up rows and columns.
A = [0, 1, 0]
[0, 0, 1]
[.5, .5, 0]
yields x = [1, 1, 1]. However,
A = [0, 0, .5]
[1, 0, .5]
[0, 1, 0]
yields x = 1/2[1, 2, 2] as you got when you did it by hand.