Odd non-zero number problem
we can't access "net.params[layer][index]" directly, it is neccesary to use iter or either a loop
How to solve this problem ? thank you.
The problem arises when there is an odd "total non-zero number":
(1020 - 1) / 2 + 1 # ind_stream size 510 np.arange(0, 1020, 2).shape (510,) np.arange(1, 1020, 2).shape (510,) (3073 - 1) / 2 + 1 # ind_stream size 1537 np.arange(0, 3073, 2).shape (1537,) np.arange(1, 3073, 2).shape (1536,)
A possible solution may be:
def binary_to_net(weights, spm_stream, ind_stream, codebook, num_nz):
bits = np.log2(codebook.size)
if bits == 4:
slots = 2
elif bits == 8:
slots = 1
else:
print "Not impemented,", bits
sys.exit()
code = np.zeros(weights.size, np.uint8)
# Recover from binary stream
spm = np.zeros(num_nz, np.uint8)
ind = np.zeros(num_nz if num_nz % 2 == 0 else num_nz + 1, np.uint8)
if slots == 2:
spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
else:
spm = spm_stream
ind[np.arange(0, num_nz, 2)] = ind_stream % (2**4)
ind[np.arange(1, num_nz if num_nz % 2 == 0 else num_nz + 1, 2)] = ind_stream / (2**4)
# Recover the matrix
ind = np.cumsum(ind+1)-1
if num_nz % 2 == 1:
ind = ind[:-1]
code[ind] = spm
data = np.reshape(codebook[code], weights.shape)
np.copyto(weights, data)
maybe change line 99 to:
if num_nz%2!=0: ind[np.arange(1, num_nz, 2)] = (ind_stream/ (2**4))[:-1] else: ind[np.arange(1, num_nz, 2)] = (ind_stream/ (2**4))
Yes, yours is the correct solution. And it is also needed on line 95 "spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)". Thankyou
Hello again. In the first solution I just needed it to run, but I did not take care about actual values. Now I really need the values, I realized it was not a good solution. Here is the good one:
if slots == 2:
spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
if num_nz % 2 == 0:
spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
else:
spm[np.arange(1, num_nz, 2)] = (spm_stream / (2**4))[:-1]
else:
spm = spm_stream
ind[np.arange(0, num_nz, 2)] = ind_stream % (2**4)
if num_nz % 2 == 0:
ind[np.arange(1, num_nz, 2)] = ind_stream / (2**4)
else:
ind[np.arange(1, num_nz, 2)] = (ind_stream / (2**4))[:-1]
Just change that and leave the rest as it was in the original one (the size of ind and spm should alwais be num_nz, what we really need to do is to ignore the last odd value generated when num_nz is odd)