Related
I am using the networkx—find_cliques library for finding the maximal cliques in a graph. I want to reduce the size of this graph based on maximal cliques.
Here is the code:
from torch_geometric.utils.convert
import to_networkx from torch_geometric.data import Data
import networkx as nx
edge_list = torch.tensor([
[0, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 7, 7, 8 ], # Source Nodes
[1, 2, 3, 4, 5, 3, 9, 5, 6, 7, 8, 9, 8, 9, 9 ] # Target Nodes
], dtype=torch.long)
node_features = torch.tensor([
[-8, 1, 5, 8, 2, -3], # Features of Node 0
[-1, 0, 2, -3, 0, 1], # Features of Node 1
[1, -1, 0, -1, 2, 1], # Features of Node 2
[0, 1, 4, -2, 3, 4], # Features of Node 3
],dtype=torch.long)
data = Data(x=node_features, edge_index=edge_list, edge_attr=edge_weight)
G_directed = to_networkx(data)
G_undirected = G_directed.to_undirected()
no_cliques= nx.find_cliques(G, nodes=None)
print(No_cliques)
List of Maximal Cliques = {1,[1,2], 2,[2, 3, 4], 3,[4, 5, 6], 4,[6, 7], 5, [7, 8, 9, 10], 6, [10,3]}
In the next step, we reduce the size of the original graph in the coarsened graph as we consider one clique as one node and joint the edge based on this rule, joining two cliques if they are not disjoint. I want to remove such a clique whose node already appeared in other cliques. In the above example, the nodes of clique 6 are already assigned in cliques no 2 and 5. So in the new graph, this clique should be removed from the clique list.
For better understanding, I am posting the picture.
hierarchy of a graph as the coarsened graph at each level
I want to make this type of graph hierarchy based on maximal clique. Does anyone know about it? How can I do it?
I am trying to create a matrix m x n x 3 base on some joints with cardinal cartesian x,y,z. First, I arrange the indices of the joints in the first image skeleton image into a 2D grid 2D grid
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
What I cannot do is to add the cartesian coordinate (x,y,z) of those indices along the third dimension of my matrice A in order to get m x n x 3. The x,y,z of each joint will be similar to the Chanel R, G, B of a color image with R=x, g=y, b=z
The the example code below produces a matrix mxnx3 from matrix A, where A illustrate indexes of elements in a skeleton, resulting in a A_result like:
# Create mxnx3 matrix from matrix A, where A include index name of skeleton element in each mxn location
import numpy as np
from matplotlib import pyplot as plt
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's rearrange slightly...
A_sub=A[0][:][:]
#Take the size of the matrix...
(m_max,n_max)=A_sub.shape
#Let's create basis for result matrix...note the format where mxn is first and after them the 3 dimensions of "colors"
A_result=np.zeros((A.shape[1],A.shape[2],3))
#demonstration function for the xyz coordinates...
def tell_me_xyz_coordinate_of_element(element_number):
#...perhaps in real application there is some measurement or the like functionality...
#...which investigate the element_number and then gives back its location...
#...but here to exemplify we simple return random int values back...
x=np.random.randint(0,255)
y=np.random.randint(0,255)
z=np.random.randint(0,255)
return x,y,z
#let's create the result matrix...
for m in range(m_max):
for n in range(n_max):
#Define x,y,z -values of the element in this m,n coordinate,
#where the value in m,n coordinate tells the number of corresponding element...
element_number=A_sub[m][n]
(x,y,z)=tell_me_xyz_coordinate_of_element(element_number)
#Set the results in the matrix...
A_result[m][n][0]=x
A_result[m][n][1]=y
A_result[m][n][2]=z
#Let's investigate the resulting nympy-matrix as a image...
#...remember to change the data format to uint8 to be able to investigate as a image
plt.imshow(np.uint8(A_result),interpolation='nearest')
title_text=''.join(["Result matrix mxnx3, \nwhere m=",str(m_max+1), " n=",str(n_max+1),",\n" "with color codes 0-255 in each m and n"])
plt.title(title_text)
plt.show()
This example may help you to solve your problem, but if not, please describe your question again in a more clear format.
#Create mxnx3 arrays and tensor form specific A matrix and then let's show how to create mxnx3 np-array and the same also in tensor format
import numpy as np
import tensorflow as tf
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's convert A to a tensor:
A_in_tensorformat=tf.Variable(A)
#Let's make a numpy of size mxnx3:
m=123
n=45
B=np.ones((m,n,3))
B_in_tensorformat=tf.Variable(B)
Let us assume there are 5-time slots and at each time slot, I have 4 options to choose from, each with a known reward, for eg. rewards = [5, 2, 1, -3]. At every time step, at least 1 of the four options must be selected, with a condition that, if option 3 (with reward -3) is chosen at a time t, then for the remaining time steps, none of the options should be selected. As an example, considering the options are indexed from 0, both [2, 1, 1, 0, 3] and [2, 1, 1, 3, 99] are valid solutions with the second solution having option 3 selected in the 3rd time step and 99 is some random value representing no option was chosen.
The Z3py code I tried is here:
T = 6 #Total time slots
s = Solver()
pick = [[Bool('t%d_ch%d' %(j, i)) for i in range(4)] for j in range(T)]
# Rewards of each option
Rewards = [5, 2, 1, -3]
# Select at most one of the 4 options as True
for i in range(T):
s.add(Or(Not(Or(pick[i][0], pick[i][1], pick[i][2], pick[i][3])),
And(Xor(pick[i][0],pick[i][1]), Not(Or(pick[i][2], pick[i][3]))),
And(Xor(pick[i][2],pick[i][3]), Not(Or(pick[i][0], pick[i][1])))))
# If option 3 is picked, then none of the 4 options should be selected for the future time slots
# else, exactly one should be selected.
for i in range(len(pick)-1):
for j in range(4):
s.add(If(And(j==3,pick[i][j]),
Not(Or(pick[i+1][0], pick[i+1][1], pick[i+1][2], pick[i+1][3])),
Or(And(Xor(pick[i+1][0],pick[i+1][1]), Not(Or(pick[i+1][2], pick[i+1][3]))),
And(Xor(pick[i+1][2],pick[i+1][3]), Not(Or(pick[i+1][0], pick[i+1][1]))))))
if s.check()==False:
print("unsat")
m=s.model()
print(m)
With this implementation, I am not getting solutions such as [2, 1, 1, 3, 99]. All of them either do not have option 3 or have it in the last time slot.
I know there is an error inside the If part but I'm unable to figure it out. Is there a better way to achieve such solutions?
It's hard to decipher what you're trying to do. From a basic reading of your description, I think this might be an instance of the XY problem. See https://xyproblem.info/ for details on that, and try to cast your question in terms of what your original goal is; instead of a particular solution, you're trying to implement. (It seems to me that the solution you came up with is unnecessarily complicated.)
Having said that, you can solve your problem as stated if you get rid of the 99 requirement and simply indicate -3 as the terminator. Once you pick -3, then all the following picks should be -3. This can be coded as follows:
from z3 import *
T = 6
s = Solver()
Rewards = [5, 2, 1, -3]
picks = [Int('pick_%d' % i) for i in range(T)]
def pickReward(p):
return Or([p == r for r in Rewards])
for i in range(T):
if i == 0:
s.add(pickReward(picks[i]))
else:
s.add(If(picks[i-1] == -3, picks[i] == -3, pickReward(picks[i])))
while s.check() == sat:
m = s.model()
picked = []
for i in picks:
picked += [m[i]]
print(picked)
s.add(Or([p != v for p, v in zip(picks, picked)]))
When run, this prints:
[5, -3, -3, -3, -3, -3]
[1, 5, 5, 5, 5, 1]
[1, 2, 5, 5, 5, 1]
[2, 2, 5, 5, 5, 1]
[2, 5, 5, 5, 5, 1]
[2, 1, 5, 5, 5, 1]
[1, 1, 5, 5, 5, 1]
[2, 1, 5, 5, 5, 2]
[2, 5, 5, 5, 5, 2]
[2, 5, 5, 5, 5, 5]
[2, 5, 5, 5, 5, -3]
[2, 1, 5, 5, 5, 5]
...
I interrupted the above as it keeps enumerating all the possible picks. There are a total of 1093 of them in this particular case.
(You can get different answers depending on your version of z3.)
Hope this gets you started. Stating what your original goal is directly is usually much more helpful, should you have further questions.
I am trying to write a function to create a CNN model. I get the following error whenever I run the script:
lua:15: unexpected symbol near '['
require('torch')
require('nn')
function CeateNvidiaModel()
--The Nvidia model
--Input dimensions
local n_channels = 3
local height = 66
local width = 200
local nvidia_model = nn.Sequential();
--nvida_model:add(nn.Normalize()
--Convolutional Layers
nvidia_model:add(nn.SpatialConvolution(n_channels, 24, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(24, 36, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(36, 48, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(48, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(64, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
-- Flatten Layer
nvidia_model:add(nn.Reshape(1164))
-- FC Layers
nvida_model:add(nn.Linear(1164, 100))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(100, 50))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(50, 10))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(10, 1))
return nvida_model
end
I assume you are confusing [] and {}. In many other languages, you write array literals as [1, 2, 3], but in Lua [ and ] are only used for indexing; to declare an "array literal", you write {1, 2, 3} (because arrays in Lua are just tables).
The error message is a bit misleading; it says unexpected symbol near '[', but in reality the [ is the unexpected symbol.
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> t.word_index
{'fantastic': 6, 'like': 10, 'no': 8, 'this': 2, 'is': 3, 'there': 7, 'one': 11, 'other': 9, 'so': 5, 'world': 1, 'hello': 4}
I'd have expected t.word_index to have just the top 3 words. What am I doing wrong?
There is nothing wrong in what you are doing. word_index is computed the same way no matter how many most frequent words you will use later (as you may see here). So when you will call any transformative method - Tokenizer will use only three most common words and at the same time, it will keep the counter of all words - even when it's obvious that it will not use it later.
Just a add on Marcin's answer ("it will keep the counter of all words - even when it's obvious that it will not use it later.").
The reason it keeps counter on all words is that you can call fit_on_texts multiple times. Each time it will update the internal counters, and when transformations are called, it will use the top words based on the updated counters.
Hope it helps.
Limiting num_words to a small number (eg, 3) has no effect on fit_on_texts outputs such as word_index, word_counts, word_docs. It does have effect on texts_to_matrix. The resulting matrix will have num_words (3) columns.
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> print(t.word_index)
{'world': 1, 'this': 2, 'is': 3, 'hello': 4, 'so': 5, 'fantastic': 6, 'there': 7, 'no': 8, 'other': 9, 'like': 10, 'one': 11}
>>> t.texts_to_matrix(l, mode='count')
array([[0., 1., 1.],
[0., 1., 1.]])
Just to add a little bit to farid khafizov's answer,
words at sequence of num_words and above are removed from the results of texts_to_sequences (4 in 1st, 5 in 2nd and 6 in 3rd sentence disappeared respectively)
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
print(tf.__version__) # 2.4.1, in my case
sentences = [
'I love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words=4)
tokenizer.fit_on_texts(sentences)
word_index = tokenizer.word_index
seq = tokenizer.texts_to_sequences(sentences)
print(word_index) # {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6}
print(seq) # [[3, 1, 2], [3, 1, 2], [1, 2]]