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shape of (7, 4, 5).
I want the c=np.dot(a,b) be in the size of (7,5,15) as below:
c = np.zeros((7,15,5))
for i in range(7):
c[i,:,:] = np.dot(a[i,:,:].T , b[i,:,:])
But I am looking for a solution without a for-loop. something like:
c = np.tensordot(a.reshape(4,7,5),b.reshape(7,4,15),axes=([1,0],[0,1]))
but this one doesn't work as expected.
I also tried this:
newaxes_a=[2,0,1]
newaxes_b=[1,0,2]
newshape_a=(-1,28)
newshape_b=(28,-1)
a_t = a.transpose(newaxes_a).reshape(newshape_a)
b_t = b.transpose(newaxes_b).reshape(newshape_b)
c = np.dot(a_t, b_t)
which didn't work as expected.
Any ideas?

You can use np.einsum -
#to match the given example
c2 = np.einsum('ijk,ijl->ikl',a,b)
print np.allclose(c, c2)
Another one using broadcasting -
c = (a[:,:,None,:]*b[...,None]).sum(1)

Related

Multi-agent grid world Q-learning observation parameter

I am trying to train agents in a multi-agent grid world using q-learning. I am using the existing environment 'collection-game': https://reposhub.com/python/deep-learning/ArnaudFickinger-gym-multigrid.html#articleHeader2. My problem is the observation/state-parameter that the environment algorithm returns. I want to use this parameter to choose the best policy given the state the agent is currently in. An example of this is shown below. Here the state parameter returns an integer that can be used to index the q-table.
Example of using the state to pick policy
The environment that I am using however returns an array of arrays that shows the observations for all agents in the environment:
[array([[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[10, 3, 0, 0, 3, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[10, 2, 0, 0, 2, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 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, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[10, 3, 0, 0, 3, 1]],
[[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0],
[ 2, 5, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0],
[ 6, 0, 0, 0, 0, 0],
[ 1, 0, 0, 0, 0, 0]]], dtype=uint8)]
My question is: how to interpret this variable, and how to use it to pick the best policy for each agent.

Updating a list in json file and keeping the same indent using Python [duplicate]

This question already has answers here:
JSON dumps custom formatting
(3 answers)
Closed 1 year ago.
I'm currently making a level editor for a game I made using Pygame. I save the world data in a json file, which contains the different blocks for each column in every row. Trough my level editor, I update the json file and it works, but the indent is messed up so it's unreadable for humans. The data for every level consists out of one list containing many other lists and the line is split after every list. Thanks for helping me!
Before dumping (the way I want it to look after updating)
{
"level_1_data": [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
2,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
2,
2,
2,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
2,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
2,
2,
2,
2,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1
],
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1
]
]
}
This is the code I have so far:
with open("./world_data.json", "r") as f:
data = json.load(f)
new_data = data[world.level_name] #Loading level_1_data or level_2_data
new_data[button[1][3][1]][button[1][3][0]] = 0 #Finding the right index in the 2 layered list
data[world.level_name] = new_data
data.update(data)
with open("./world_data.json", "w") as f:
json.dump(data, f, indent=4)
Try using pprint, it will get you alot closer to what you want.
import pprint
with open("world_data.json", "w") as f:
pprint.pprint(data, stream = f)

python/numpy combine subarrays 4 rows at a time

I have a numpy array that is split by each row:
splitArray:
[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
I was hoping to merge said splitArray every 4 rows, and the last subarray not necessarily having to be 4, but just the remainder of what's left.
Below is the array I hope to have:
joinedArray:
[[0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0]]
Using a list-comp:
[a[i:i+4] for i in range(0, len(a), 4)]
#[array([[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]])]
As a pure Numpythonic approach you can find all the desired indexes for splitting your array by creating a range from chunking number to number of rows with the chunking number as thestep arg of the range. Then use np.split() to split your array:
In [24]: def chunk_array(arr, ch):
...: x = arr.shape[0]
...: return np.split(a, np.arange(ch, x, ch))
...:
...:
Demo:
In [25]: chunk_array(a, 4)
Out[25]:
[array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])]
In [26]: chunk_array(a, 3)
Out[26]:
[array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])]
If you want the chunked arrays to be concatenated you can use #jpp's answer with np.concatenate() and map or slightly different in a list comprehension.
In [75]: def chunk_array(arr, ch):
...: x = arr.shape[0]
...: return [np.concatenate(subs) for subs in np.split(arr, np.arange(ch, x, ch))]
That can be done using the infamous grouper recipe.
>>> from itertools import zip_longest
>>> import numpy as np
>>>
>>> data = [7 * [0] for i in range(14)]
>>> i=iter(data); list(map(np.concatenate, zip_longest(*4*(i,), fillvalue=[])))
[array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])]
You can use np.concatenate with np.split. If required, you can adjust the below example to output a list of lists instead of a list of arrays.
As mentioned, a single jagged numpy array is not a good idea.
A = np.zeros((14, 3))
res = list(map(np.concatenate, np.split(A, np.arange(4, A.shape[0], 4))))
print(res)
[array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([ 0., 0., 0., 0., 0., 0.])]

Replacing part of Numpy 2D Array using list in both rows and columns

Let's import numpy first,
import numpy as np
For example, I have a matrix A as,
A = np.identity(10)
I have two other matrices as,
B = np.random.sample((4, 4))
C = np.random.sample((6, 6))
In addition, I have two lists of indices as,
idx_1 = [1, 2, 4, 7]
idx_2 = [0, 3, 5, 6, 8, 9]
Now I want to replace idx_1 rows and columns of A by B and idx_2 rows and columns of A by C. The final A matrix will be a block diagonal matrix.
What is the efficient way to achieve this?
I tried as follows but I did not change A, I don't know why, but I did not get any error as well.
A[idx_1][:,idx_1] = B
In [99]: A = np.identity(10).astype(int)
In [100]: A
Out[100]:
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
In [101]: idx_1 = [1, 2, 4, 7]
I can select a set of diagonal values with:
In [102]: A[idx_1, idx_1]
Out[102]: array([1, 1, 1, 1])
In [103]: A[idx_1, idx_1] = [10,20,30,40]
In [104]: A
Out[104]:
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 10, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 20, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 30, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 40, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
But it looks like you want to replace a (4,4) block of values. We have to construct a pair on indices that together broadcast to the shape, that is (4,1) array with a (1,4) array.
In [105]: np.ix_(idx_1, idx_1)
Out[105]:
(array([[1],
[2],
[4],
[7]]), array([[1, 2, 4, 7]]))
In [106]: A[np.ix_(idx_1, idx_1)]
Out[106]:
array([[10, 0, 0, 0],
[ 0, 20, 0, 0],
[ 0, 0, 30, 0],
[ 0, 0, 0, 40]])
In [107]: A[np.ix_(idx_1, idx_1)] += 1
In [108]: A
Out[108]:
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 11, 1, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 21, 0, 1, 0, 0, 1, 0, 0],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[ 0, 1, 1, 0, 31, 0, 0, 1, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, 1, 1, 0, 1, 0, 0, 41, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
The equivalent indexing with nested lists is:
In [109]: A[[[1],[2],[4],[7]],[[1,2,4,7]]]
Out[109]:
array([[11, 1, 1, 1],
[ 1, 21, 1, 1],
[ 1, 1, 31, 1],
[ 1, 1, 1, 41]])
Regarding your indexing attempt:
In [110]: A[idx_1] # A[idx_1,:]
Out[110]:
array([[ 0, 11, 1, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 21, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 1, 0, 31, 0, 0, 1, 0, 0],
[ 0, 1, 1, 0, 1, 0, 0, 41, 0, 0]])
In [111]: A[idx_1][:,idx_1]
Out[111]:
array([[11, 1, 1, 1],
[ 1, 21, 1, 1],
[ 1, 1, 31, 1],
[ 1, 1, 1, 41]])
In[111] is evaluated in 2 steps; first rows are selected, and then columns.
In
A[idx_1][:,idx_1] = B
the values of B will replace columns in Out[110]. But that's a copy of values from A, not a view. So a good grasp of the difference between view and copy, and between basic and advanced indexing is important when working with numpy.

Neural network not producing results

Here is my project. It consists of: m = 24 where m is the number of training examples; 3 hidden layers and the input layer; 3 sets of weights connecting each layer; the data is 1x38 with a response of y (1x1).
import numpy as np
x = np.array([
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0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0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0]])
y = np.array([
[1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1,0]]).T
w = np.random.random((38, 39))
w2 = np.random.random((39, 39))
w3 = np.random.random((39, 1))
for j in xrange(100000):
a2 = 1/(1 + np.exp(-(np.dot(x, w) + 1)))
a3 = 1/(1 + np.exp(-(np.dot(a2, w2) + 1)))
a4 = 1/(1 + np.exp(-(np.dot(a3, w3) + 1)))
a4delta = (y - a4) * (1 * (1 - a4))
a3delta = a4delta.dot(w3.T) * (1 * (1 - a3))
a2delta = a3delta.dot(w2.T) * (1 * (1 - a2))
w3 += a3.T.dot(a4delta)
w2 += a2.T.dot(a3delta)
w += x.T.dot(a2delta)
print(a4)
Here are the results:
[[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]
[ 1.]]
Can anyone see if i have gone wrong? Does my network need to be changed? I have tried experimenting with the hyperparameters by adding more hidden layers and more memory
You have some mistakes and some things I think are mistakes, but maybe just a different implementation.
You are adding your gradients to your weights, when you should be subtracting your gradient multiplied by a step size. This is why your weights shoot up to 1.0 in only a single iteration.
These:
w3 += a3.T.dot(a4delta)
Should be something like this:
w3 -= addBias(a3).T.dot(a4delta) * step
Also, I don't think you have the correct formulation for the partial derivative of the sigmoid function. I think these:
a3delta = a4delta.dot(w3.T) * (1 * (1 - a3))
Should be:
a3delta = a4delta.dot(w3.T) * (a3 * (1 - a3))
You should also initialize your weight around zero with something like:
ep = 0.12
w = np.random.random((39, 39)) * 2 * ep - ep
Most implementations add a bias node to each layer, you're not doing that. It complicates things a little, but I think it will make it converge faster.
For me, this converges on a confident answer in 200 iterations:
# Weights have different shapes to account for bias node
w = np.random.random((39, 39)) * 2 * ep - ep
w2 = np.random.random((40, 39))* 2 * ep - ep
w3 = np.random.random((40, 1)) * 2 * ep - ep
ep = 0.12
w = np.random.random((39, 39)) * 2 * ep - ep
w2 = np.random.random((40, 39))* 2 * ep - ep
w3 = np.random.random((40, 1)) * 2 * ep - ep
def addBias(mat):
return np.hstack((np.ones((mat.shape[0], 1)), mat))
step = -.1
for j in range(200):
# Forward prop
a2 = 1/(1 + np.exp(- addBias(x).dot(w)))
a3 = 1/(1 + np.exp(- addBias(a2).dot(w2)))
a4 = 1/(1 + np.exp(- addBias(a3).dot(w3)))
# Back prop
a4delta = (y - a4)
# need to remove bias nodes here
a3delta = a4delta.dot(w3[1:,:].T) * (a3 * (1 - a3))
a2delta = a3delta.dot(w2[1:,:].T) * (a2 * (1 - a2))
# Gradient Descent
# Multiply gradient by step then subtract
w3 -= addBias(a3).T.dot(a4delta) * step
w2 -= addBias(a2).T.dot(a3delta) * step
w -= addBias(x).T.dot(a2delta) * step
print(np.rint(a4))

Categories