-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_network.py
More file actions
139 lines (108 loc) · 3.49 KB
/
Copy pathagent_network.py
File metadata and controls
139 lines (108 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from asyncio import constants
import torch
import torch.nn as nn
import torch.nn.functional as F
# from sklearn.preprocessing import LabelEncoder
# from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
import numpy as np
from copy import deepcopy
from scipy.stats import truncnorm
import os
from communication import Communication
import constants
'''
Both of the agent will learn their own mapping because each agent
is a separate neural network
'''
'''
Neural network architecture:
Input Layer: 28 [one_hot encoding]
Hidden Layer1: 10
Output Layer : size of vocabulary = 28 [one_hot encoding]
'''
class MapNet(nn.Module):
def __init__(self, input_size, output_size) -> None:
super(MapNet,self).__init__()
self.L1 = nn.Linear(input_size,10)
self.L2 = nn.Linear(10,output_size)
def forward(self, input):
# print(input)
x = self.L1(input)
# print(x)
x = F.relu(x)
x = self.L2(x)
# print(x)
x = F.softmax(x, dim=0)
return x
def print_loss(losses, learning_rate = 0.01):
plt.plot(losses)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.title("Learning rate %f"%(learning_rate))
plt.show()
def train_agent(net, X,Y, learning_rate = 0.01, loss_fn = nn.MSELoss(), epochs = 1000):
optimiser = torch.optim.Adam(net.parameters(), lr=learning_rate)
losses = []
input_count=[]
for epoch in range(epochs):
# t = torch.FloatTensor(1).uniform_(-1, 1) #number of concepts = 28
t2 = truncnorm.rvs(-10, 10, size=1)
index = np.random.choice(28)
# input_count.append(index)
X_ = X[index] +\
torch.tensor(t2, dtype=torch.float)
Y_ = Y[index]
# print(f"X_ = {X_}")
# print(f"Y_ = {Y_}")
pred_y = net(X_)
# print(f"pred = {pred_y}")
# print(f"pred sum = {pred_y.sum()}")
loss = loss_fn(pred_y, Y_)
losses.append(loss.item())
net.zero_grad()
loss.backward()
optimiser.step()
# print_loss(losses)
# print()
def one_hot_encoded(data):
dim = len(data)
temp = np.eye(dim)
return temp
# if __name__ == '__main__':
def initialise():
# Getting the mappings
vocab_map = Communication.generate_vocabulary(constants.n_octants,constants.n_segments)
# vocab_map = np.array(vocab_map)
X_ = [i[0] for i in vocab_map]
Y_ = [i[1] for i in vocab_map]
# Creating one hot encoding of each of the vector
X = torch.tensor(one_hot_encoded(X_), dtype=torch.float)
Y = torch.tensor(one_hot_encoded(Y_), dtype=torch.float)
# t = torch.FloatTensor(28).uniform_(-1, 1)
# VocabNet tries to learn the mapping from concept to vocab
# input size = output size = 8+20 = constants.n_octants+ constants.n_segments
vocabNet = MapNet(28,28)
# print(vocabNet)
train_agent(vocabNet,X,Y, epochs=10000)
# print(X[2])
'''
for i in range(11):
pred = vocabNet(X[i])
print(torch.sum(pred))
print(pred)
print(torch.argmax(pred))
print(torch.argmax(Y[i]))
print("*"*50)
# os.system('clear')
# print(torch.sum(pred, 1))
# print(torch.argmax(Y,1))
# print(torch.argmax(pred, 1))
# print(vocabNet(X[:3]))
# train_agent(vocabNet,)
'''
# Vocab to concepts
# this will be used by listener agent
conceptNet = MapNet(28, 28)
train_agent(conceptNet, X,Y, epochs=10000)
return X_, Y_, conceptNet, vocabNet