Code Monkey home page Code Monkey logo

Comments (9)

giannisnik avatar giannisnik commented on June 7, 2024

Dear @sbonner0 ,

Thank you for your interest in GraKeL.

The graph_from_networkx() function transforms an iterable of NetworkX objects to an iterable of GraKeL's internal graph representations. Therefore, instead of:
graph = grakel.graph_from_networkx(G)
you should use the following:
graph = grakel.graph_from_networkx([G])

Note also that GraKeL contains implementations of kernels between graphs (i.e., kernels that compare graphs to each other). Therefore, feeding a single graph to the fit_transform() function will just produce a single number (i.e., the kernel value between the input graph and itself). Furthermore, the shortest path kernel expects the input graphs to contain node labels. If they do not, you should set the parameter with_labels=False.

from grakel.

sbonner0 avatar sbonner0 commented on June 7, 2024

Great thanks, this works now! It might be worth updating your documentation on your webpage to make this a bit more clear for everyone.

As an aside, how many graphs and at what scale would you expect the kernels to be able to work on? For example, I have a set of 50,000 graphs of 100,000 vertices each, would you expect that I would be able to use GraKel to process this?

from grakel.

giannisnik avatar giannisnik commented on June 7, 2024

Dear @sbonner0 ,

Your graphs are very large. Most benchmark datasets contain graphs with up to thousands of nodes (i.e., two orders of magnitude smaller than your graphs). If your graphs are unlabeled, then you can use the graphlet kernel (but you need to make sure that you set the number of samples to some large value). If your graphs are also sparse, then I guess that the Weisfeiler-Lehman subtree kernel and the shortest path kernel can also handle your dataset. You can give it a try.

With regards to the number of graphs, 50,000 is quite a large number. This would correspond to a 50,000 X 50,000 gram matrix which may not fit in your main memory. Therefore, I would suggest that you use the Nystrom method and generate a low-dimensional representation for each graph (see the corresponding section here: https://ysig.github.io/GraKeL/dev/user_manual/longer_introduction.html).

from grakel.

sbonner0 avatar sbonner0 commented on June 7, 2024

This is great, thanks for all your help and work on GraKel.

from grakel.

safreita1 avatar safreita1 commented on June 7, 2024

Hi @giannisnik ,

I have a similar setup--many undirected large graphs with no attributes--and had a question regarding the use of the Weisfeiler-Lehman subtree and the shortest path kernel setup. I have a list of networkx graphs that I'm sending to:

graphs = graph_from_networkx(nx_graphs)

then initializing the kernel as:

gk = GraphKernel(kernel=[{"name": "subtree_wl"}, {"name": "shortest_path", "normalize": True, "with_labels": False}])

and then finally calling:

gk.fit_transform(x_train)

However, I'm running into an error "AttributeError: 'NoneType' object has no attribute 'values'" in line 110, in parse_input
for (label, frequency) in iteritems(Counter(itervalues(L))):

Any ideas what I'm doing wrong here?

from grakel.

giannisnik avatar giannisnik commented on June 7, 2024

Hi @safreita1 ,

The problem is that WL expects the nodes of the graphs to be annotated with discrete labels. To run the kernel, you can just assign the same discrete label to all the vertices. See for instance the following example:

G1 = nx.cycle_graph(3)
nx.set_node_attributes(G1, {0:'a', 1:'a', 2:'a'}, 'label')

G2 = nx.cycle_graph(4)
nx.set_node_attributes(G2, {0:'a', 1:'a', 2:'a', 3:'a'}, 'label')

G3 = nx.cycle_graph(5)
nx.set_node_attributes(G3, {0:'a', 1:'a', 2:'a', 3:'a', 4:'a'}, 'label')

nx_graphs = [G1, G2, G3]

graphs = graph_from_networkx(nx_graphs, node_labels_tag='label')

gk = GraphKernel(kernel=[{"name": "weisfeiler_lehman", "n_iter": 5}, {"name": "shortest_path"}], normalize=True)
K = gk.fit_transform(graphs)

The above code computes the WL shortest path kernel. It is not clear to me if you want to compute this kernel or if you want to compute the WL subtree kernel and separately, the shortest path kernel. Note that the latter can be computed even if the nodes of the graphs are unlabeled.

from grakel.

safreita1 avatar safreita1 commented on June 7, 2024

Thanks for the great example @giannisnik!

My primary goal is a kernel that can work with many large graphs (e.g., 50k nodes). Now that I'm re-reading your post a few messages above, I realize that you were probably saying you can use either the Weisfeiler-Lehman subtree kernel or the shortest path kernel to handle a dataset this large.

For the graphlet kernel, would the default parameters suffice or do you recommend a specific setup?

from grakel.

giannisnik avatar giannisnik commented on June 7, 2024

Dear @safreita1 ,

These graphs are indeed very large. These kernels have mainly been applied to much smaller graphs. But if the graphs are sparse, I guess that some kernels can handle them. See below how you can compute the two kernels: the shortest path kernel and the WL subtree kernel.

graphs = graph_from_networkx(nx_graphs)

sp_kernel = GraphKernel(kernel=[{"name": "shortest_path", "with_labels": False}], normalize=True)
K = sp_kernel.fit_transform(graphs)
print(K)

graphs = graph_from_networkx(nx_graphs, node_labels_tag='label')

wl_subtree_kernel = GraphKernel(kernel=[{"name": "weisfeiler_lehman", "n_iter": 5}, {"name": "subtree_wl"}], normalize=True)
K = wl_subtree_kernel.fit_transform(graphs)
print(K)

With regards to the graphlet kernel, since the graphs are very large, it would be infeasible to extract all graphlets. Thus, you need to perform sampling as follows.

graphs = graph_from_networkx(nx_graphs)

graphlet_kernel = GraphKernel(kernel=[{"name": "graphlet_sampling", "sampling": {"n_samples":100}}], normalize=True)
K = graphlet_kernel.fit_transform(graphs)
print(K)

Note that the above code samples 100 graphlets of size from 3 to 5 from each graph. In your case, since the graphs are very large, 100 samples is not enough. You should sample a much larger number of graphlets. Note also that as you increase the number of samples, the running time of the kernel also increases.

from grakel.

safreita1 avatar safreita1 commented on June 7, 2024

Thank you for all your assistance, it's been incredibly helpful. Great job on this library!

from grakel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.