Adding and Accessing Node and Edge features to the Homogeneous Graphs in DGL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video let's see how can we add node and edge features to the homogeneous graph so within dgl we have two interfaces to do this task we have something called n data which refers to node data and we have e data which refers to the e data so before looking at these interfaces let's try to understand the example graph so i constructed a simple graph with four different edges ranging from 0 to 3 and this is a directed graph with 5 different edges so unlike the previous graph along with the nodes and edges i also added some information at nodes and edges okay so along with the nodes and edges we also have a three-dimensional vector at every node and a scalar value at every edge so now now let's try to build this graph using digital functions okay so first i'll create the i'll create this directed graph using the source node ids and the destination node ids okay so we have already seen this in the previous videos so we use dgl.graph method which accepts the tuple of tensors okay it accepts a tuple of tensors where the first tensor refers to the source node ids and the second tensor refers to the destination node ids so we already know what are the source node ids and destination node ids from this graph so we just need to pass these two tensors as an input to the dgl graph method so once we pass this so we'll get a graph and if you observe this graph object we can see that the number of nodes are equal to 4 which is ranging from 0 to 3 and the number of edges are 5 okay even in the graph that we have here also it contains 5 different edges and if you observe here we have 2 more things something called n data schemes and e data schemes okay so right now we can see that both the end data schemes and e data schemes are null because we haven't added any node features and edge features okay so now using the n data and e data interfaces let's see how can we add these node vectors and some weights on the edges okay and there is one thing to note here so whatever values that we add for the nodes and edges should be of numerical types that is you can either define the values to be of float double or int okay only the numerical type values are allowed and again like i mentioned earlier we can either have the scalars or we can have the vectors or we can even have the multi-dimensional tensors for every node and for every edge okay it's up to us we can define whatever dimensions we want for the adjacent nodes so now let's just try to replicate these vectors for every nodes okay so it's very simple so we have graph dot n data graph graph.ndata interface first i'll just print this graph.ndata and let's see as you can see it's empty it's empty for now because we didn't add any data at a node level so now i wanted to add these vectors at every node okay so i'll just name this as node vectors okay i'll just name this as node vectors and this can be any name it's up to you it's a user defined name okay so i'm creating a new end data with node vectors and within this now i wanted to assign a node vector for every node so how many nodes do we have we have total of four different nodes right we can even print that and see let's print graph dot nodes okay you can see the nodes are ranging from zero to three so for every node id we wanted to add a three dimensional vector okay a 3d vector so let's see how can we do that so i'll use a multi-dimensional tensor to do this task okay so first i'll define torch.tensor and i wanted to add a three values i mean a three valued vector for every node right and how many nodes we have we have four different nodes so i'm going to create a tensor of tensor something like an array of errors so okay first for node 0 i'll just create a vector with three values similarly i'll create a vector with three values for node one i'm randomly just defining these numbers just for the case of demonstrating this so again like 3 5 6 and again 7 8 9 okay so this is the tensor that i have defined as you can see how many rows do we have we have four different rows because we have four different nodes in the graph and how many columns do we have we have three different columns because each of the node vector is of three dimensions okay so we created something like four cross three matrix okay even we can uh uh see the shape of this tensor we'll see that so first i'll add this tensor to the graph okay so now i'll print the graph again okay i'll print the graphing graph again so now when i print the graph again you can see now the end data schemes is not empty okay so here we have added some scheme with the name node vectors okay so now how can we access this and this is very similar to dictionary we can just access this with the key value that is the scheme key value so what is the key value here it's node vectors i'll just print this okay so you can see it it returned a tensor that we have created that we have passed as an input to this node vectors okay uh let's try to see the shape okay so we can see this is a torch tensor of size four cross three because we have four nodes where each node is having a three dimensional array or a three dimensional vector okay so it's a four cross three matrix okay that's what that's how we can create or we can add the nodes level data using these end data interfaces okay and similarly let's see how can we add edge features okay so again uh here i just wanted to add the scalar set edges for now i don't wanted to add any vectors i just wanted to add some scalar values for every edge okay and how many edges do we have we have five different edges okay let's just try to print the edges okay so let's try graph dot edges okay so this will return us you know the source node ids and the destination node ids so i'll just put form is equal to all okay so this will return a triple you can observe the third tensor in this triple which is ranging from zero to four which says we have five different edges whose edge whose edge ids are ranging from zero to 4 okay so now we need to add some edge value for each of these edges and similar to the previous approach we can use graph dot e data in place of graph dot and data first i'll print graph.t data let's see so this is empty because we haven't defined any schemes for the e data so now i wanted to define something on the edges i wanted to call them as edge weights okay because these are not really vectors i just wanted to add a single value at every edge okay and we can again use the torch dot tensors so here as it is a scalar we don't need a multi-dimensional tensor i am just considering a simple list kind of a list a simple 1d tensor and how many values we can have here the number of values in this tensor should be equivalent to the number of edges in the graph we have five different edges so i'll define five different weights let's say hundred gamma two hundred comma three hundred gamma four hundred comma five hundred okay so now i'll just run this okay so now i have added the edge weights that is the e data so now i'll print the e data first i'll print the graph so now when you print the graph you can see now the e data schemes is also not empty it has a new scheme with the name edge weights okay and again we can access similar to dictionaries graph dot e data of key name okay edge weights okay and you can see this is a tensor of weights okay so this is how we can add the node features and edge features we can use dgl and data interfaces to add the data at node level and we can use dcl edata interfaces to add any data at edge level okay thanks
Info
Channel: SriharshAI
Views: 731
Rating: undefined out of 5
Keywords: iMovie
Id: pOgUVyyEFQs
Channel Id: undefined
Length: 9min 9sec (549 seconds)
Published: Mon Sep 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.