def test_3output_dimensions(self):
     nn=NN((2,2,3),verbose=0)
     inputs=([1,0],[0,1],[1,1],[0,0])
     for i in inputs:
         nn.forward(i)
         self.assertEqual(nn.outputs[0].shape,(3,1))
         self.assertEqual(nn.outputs[1].shape,(3,1))
         self.assertEqual(nn.outputs[2].shape,(3,1))
         self.assertEqual(nn.get_output().shape,(3,))
Beispiel #2
0
 def test_3output_dimensions(self):
     nn = NN((2, 2, 3), verbose=0)
     inputs = ([1, 0], [0, 1], [1, 1], [0, 0])
     for i in inputs:
         nn.forward(i)
         self.assertEqual(nn.outputs[0].shape, (3, 1))
         self.assertEqual(nn.outputs[1].shape, (3, 1))
         self.assertEqual(nn.outputs[2].shape, (3, 1))
         self.assertEqual(nn.get_output().shape, (3, ))
 def test_backward(self):
     X=[[0,0],[0,1],[1,0],[1,1]]
     Y=[[0],[1],[1],[0]]
     nn=NN([2,2,1],verbose=0)
     for i in range(4):
         nn.forward(X[i])
         nn.backward(Y[i])
         self.assertEqual(nn.outputs[0].shape,(3,1))
         self.assertEqual(nn.outputs[1].shape,(3,1))
         self.assertEqual(nn.outputs[2].shape,(1,1))
         self.assertEqual(nn.get_output().shape,(1,))
Beispiel #4
0
 def test_backward(self):
     X = [[0, 0], [0, 1], [1, 0], [1, 1]]
     Y = [[0], [1], [1], [0]]
     nn = NN([2, 2, 1], verbose=0)
     for i in range(4):
         nn.forward(X[i])
         nn.backward(Y[i])
         self.assertEqual(nn.outputs[0].shape, (3, 1))
         self.assertEqual(nn.outputs[1].shape, (3, 1))
         self.assertEqual(nn.outputs[2].shape, (1, 1))
         self.assertEqual(nn.get_output().shape, (1, ))
Beispiel #5
0
    def test_neural_net_back_forward(self):
        n_in, n_out = 3, 2

        weights = np.array([[0, -1, 2], [-3, 4, -5]])
        bias = np.arange(n_out)[:, np.newaxis]

        nn = NeuralNet(MeanSquaredError(),
                       1e-3,
                       layers=[Linear(n_in, 2, weights, bias),
                               ReLU()])
        x = np.array([[[0], [1], [2]]])
        y = np.array([[[2], [3]]])
        assert y.shape[1] == n_out
        # |0 -1  2| |0|   |0|   | 3|   |0|   | 3|    |3|
        # |-3 4 -5| |1| + |1| = |-6| + |1| = |-5| -> |0|
        #           |2|

        pred = nn.forward(x)
        assert np.array_equal(pred, [[[3], [0]]])

        nn.compute_loss(pred, y)
        dL_dx = nn.backward()

        # |0 -1  2| |0 + dx1|   | 3 + 0    -  dx2 + 2dx3|   | 3 + ...|    |3 - dx2 + 2dx3|
        # |-3 4 -5| |1 + dx2| = |-6 - 3dx1 + 4dx2 - 5dx3| = |-5 + ...| -> |0|
        #           |2 + dx3| The second component is ReLU'ed away
        # MSE loss results in 2( ... ) so dL = -2dx2 + 4dx3, dL/dx = |0, -2, 4|

        assert np.array_equal(dL_dx, [[[0], [-2], [4]]])
Beispiel #6
0
    def test_neural_net_tends_to_correct(self):
        n_in, n_out = 4, 2

        np.random.seed(12)
        weights = np.random.normal(size=(n_out, n_in))
        bias = np.zeros(n_out)[:, np.newaxis]

        nn = NeuralNet(MeanSquaredError(),
                       1e-2,
                       layers=[Linear(n_in, 2, weights, bias)])

        x = np.array([[[-1], [0.5], [-0.33], [0.75]]])
        y = np.array([[[-0.5], [0.2]]])

        for _ in range(1000):
            pred = nn.forward(x)
            loss = nn.compute_loss(pred, y)
            nn.backward()

        assert np.isclose(loss, 0)
Beispiel #7
0
    def test_neural_net_works_with_batches(self):
        n_in, n_out = 2, 2

        np.random.seed(12)
        weights = np.random.normal(size=(n_out, n_in))
        bias = np.zeros(n_out)[:, np.newaxis]

        nn = NeuralNet(MeanSquaredError(),
                       1e-2,
                       layers=[Linear(n_in, 2, weights, bias)])

        # batch of 3
        x = np.array([[[-1], [0.5]], [[1], [-0.2]], [[-0.33], [0.75]]])
        y = x

        # Why does this take so much longer to converge than the previous one?
        for _ in range(10000):
            pred = nn.forward(x)
            loss = nn.compute_loss(pred, y)
            nn.backward()

        assert np.isclose(loss, 0)
        assert np.all(
            np.isclose(nn.layers[0].weights, [[1, 0], [0, 1]], atol=1e-3))
import torch
from neural_net import NeuralNet
from load_policy import *
import gym
import torch.nn as nn

env = gym.make('CartPole-v1')
init = env.reset()
network = NeuralNet(4, 128, 64, 2)
#network.load_state_dict(torch.load('mymodel.pt'))
_, y = torch.max(network.forward(torch.Tensor(init).view(1, 4)), 0)[-1]
done = False
for i in range(1000):
    observation, reward, done, info = env.step(y.item())
    print(reward)
    if done == True:
        y = env.reset()
    observation = torch.Tensor(observation).view(1, 4)
    env.render()
    _, y = torch.max(network.forward(torch.Tensor(observation).view(1, 4)),
                     0)[-1]