-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphBase.jl
185 lines (143 loc) · 4 KB
/
GraphBase.jl
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
include("MathBase.jl")
include("Config.jl")
mutable struct Node
nn::Array{FeedForward}
name::String
type::String
encoding
edges # ::Array{Edge}
collected
Node(nn, name, type, encoding, edges=[]) = new(
nn,
name,
type,
encoding,
edges,
zeros(1, message_size)
)
end
mutable struct Edge
nn::Array{FeedForward}
name::String
encoding
node_from::Node
node_to::Node
Edge(nn, name, encoding, node_from, node_to) = new(
nn,
name,
encoding,
node_from,
node_to,
)
end
mutable struct Graph
nodes::Array{Node}
node_names::Dict
edge_names::Dict
node_types::Dict
node_nns::Dict
edge_nns::Dict
node_predictor::Array{FeedForward}
edge_predictor::Array{FeedForward}
label_predictor::Array{FeedForward}
Graph(;node_names=Dict(), edge_names=Dict(), node_types=Dict(), node_nns=Dict(), edge_nns=Dict()) = new(
[],
node_names,
edge_names,
node_types,
node_nns,
edge_nns,
[],
[],
[],
)
end
get_node(graph, node_name::String) =
for node in graph.nodes
if node.name == node_name
return node
end
end
get_edge(graph, node_from::Node, node_to::Node) =
for edge in node_from.edges
if edge.node_to == node_to
return edge
end
end
neighbors_of(node) = [edge.node_to for edge in node.edges]
all_edges(graph) =
begin
edges = []
for node_from in graph.nodes
for node_to in graph.nodes
node_from != node_to && (edge = get_edge(graph,node_from,node_to)) != nothing ? push!(edges,edge) : ()
end
end
for node in graph.nodes
(edge = get_edge(graph,node,node)) != nothing ? push!(edges,edge) : ()
end
edges
end
update_node_wrt_neighbors!(node) =
begin
edges = [edge for edge in node.edges if edge.encoding != nothing && edge.node_to.encoding != nothing]
if length(edges) > 0
if (no_encoding = node.encoding == nothing)
for edge in node.edges
if edge.node_to.encoding != nothing
node.encoding = zeros(size(edge.node_to.encoding))
break
end
end
end
incomings = vcat([prop(edge.nn, hcat(edge.node_to.collected, edge.node_to.encoding, node.encoding)) for edge in edges]...)
attentions = softmax(vcat([prop(node.nn, hcat(edge.encoding, edge.node_to.encoding, node.encoding); act2=nothing) for edge in edges]...))
node.collected = sum(incomings .* attentions, dims=1)
no_encoding ? node.encoding = nothing : ()
end
end
update_node_wrt_depths(node) =
begin
tree = [[node]]
for _ in 1:propogation_depth-1
level = []
for node in tree[end]
for neighbor in [edge.node_to for edge in node.edges if edge.encoding != nothing && edge.node_to.encoding != nothing]
neighbor in level ? () : push!(level, neighbor)
end
end
push!(tree, level)
end
for level in reverse(tree)
for node in level
update_node_wrt_neighbors!(node)
end
end
root_node_collected = node.collected
for level in tree
for node in level
node.collected = zeros(1, message_size)
end
end
root_node_collected
end
predict_edge(graph, node_from::Node, node_to::Node) =
begin
edge = get_edge(graph, node_from, node_to)
if edge != nothing
old_encoding = edge.encoding
edge.encoding = nothing
end
node_from_collected = update_node_wrt_depths(node_from)
node_to_collected = update_node_wrt_depths(node_to)
edge != nothing ? edge.encoding = old_encoding : ()
softmax(prop(graph.edge_predictor, hcat(node_from_collected, node_to_collected); act2=nothing))
end
predict_node(graph, node::Node) =
begin
old_encoding = node.encoding
node.encoding = nothing
node_collected = update_node_wrt_depths(node)
node.encoding = old_encoding
softmax(prop(graph.node_predictor, node_collected; act2=nothing))
end