Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging recomm_sys #72

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TM Graph Devcontainer",
"name": "TM Graph Recomm",
"dockerComposeFile": "docker-compose.yml",
"service": "tm-graph-development",
"service": "tm-graph-recomm",
"workspaceFolder": "/app",
"forwardPorts": [],
"postCreateCommand": "echo 'Devcontainer is ready'",
Expand Down
5 changes: 3 additions & 2 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
tm-graph-development:
tm-graph-recomm:
build:
context: ../
dockerfile: .devcontainer/Dockerfile
Expand All @@ -9,4 +9,5 @@ services:
devices:
- driver: nvidia
capabilities: [gpu]
count: 1 # Assign number of GPUs or use 'all' to assign all available GPUs
# count: 1 # Assign number of GPUs or use 'all' to assign all available GPUs
device_ids: ["6"]
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
build/
GraphTsetlinMachine.egg-info/
/dist/

.envrc

# Byte-compiled / optimized / DLL files
Expand Down
13 changes: 0 additions & 13 deletions examples/MNISTConvolutionDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,13 @@ def default_args(**kwargs):
hypervector_bits=args.hypervector_bits,
double_hashing = args.double_hashing
)

for graph_id in range(X_train.shape[0]):
graphs_train.set_number_of_graph_nodes(graph_id, number_of_nodes)

graphs_train.prepare_node_configuration()

for graph_id in range(X_train.shape[0]):
for node_id in range(graphs_train.number_of_graph_nodes[graph_id]):
graphs_train.add_graph_node(graph_id, node_id, 0)

graphs_train.prepare_edge_configuration()

for graph_id in range(X_train.shape[0]):
if graph_id % 1000 == 0:
print(graph_id, X_train.shape[0])
Expand All @@ -88,23 +83,17 @@ def default_args(**kwargs):

graphs_train.add_graph_node_property(graph_id, node_id, "C:%d" % (q))
graphs_train.add_graph_node_property(graph_id, node_id, "R:%d" % (r))

graphs_train.encode()

print("Training data produced")

graphs_test = Graphs(X_test.shape[0], init_with=graphs_train)
for graph_id in range(X_test.shape[0]):
graphs_test.set_number_of_graph_nodes(graph_id, number_of_nodes)

graphs_test.prepare_node_configuration()

for graph_id in range(X_test.shape[0]):
for node_id in range(graphs_test.number_of_graph_nodes[graph_id]):
graphs_test.add_graph_node(graph_id, node_id, 0)

graphs_test.prepare_edge_configuration()

for graph_id in range(X_test.shape[0]):
if graph_id % 1000 == 0:
print(graph_id, X_test.shape[0])
Expand All @@ -120,9 +109,7 @@ def default_args(**kwargs):

graphs_test.add_graph_node_property(graph_id, node_id, "C:%d" % (q))
graphs_test.add_graph_node_property(graph_id, node_id, "R:%d" % (r))

graphs_test.encode()

print("Testing data produced")

tm = MultiClassGraphTsetlinMachine(
Expand Down
46 changes: 15 additions & 31 deletions examples/MNISTVanillaDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from GraphTsetlinMachine.tm import MultiClassGraphTsetlinMachine
from time import time
import argparse
from skimage.util import view_as_windows
from keras.datasets import mnist
from numba import jit

Expand Down Expand Up @@ -53,51 +52,36 @@ def default_args(**kwargs):
hypervector_bits=args.hypervector_bits,
double_hashing = args.double_hashing
)

for graph_id in range(X_train.shape[0]):
graphs_train.set_number_of_graph_nodes(graph_id, number_of_nodes)

graphs_train.prepare_node_configuration()

for graph_id in range(X_train.shape[0]):
number_of_outgoing_edges = 0
graphs_train.add_graph_node(graph_id, 'Image Node', number_of_outgoing_edges)

graphs_train.prepare_edge_configuration()

for graph_id in range(X_train.shape[0]):
if graph_id % 1000 == 0:
print(graph_id, X_train.shape[0])

# if graph_id % 1000 == 0:
# print(graph_id, X_train.shape[0])
for k in X_train[graph_id].nonzero()[0]:
graphs_train.add_graph_node_property(graph_id, 'Image Node', "W%d,%d" % (k // 28, k % 28))

graphs_train.encode()

print("Training data produced")

graphs_test = Graphs(X_test.shape[0], init_with=graphs_train)

for graph_id in range(X_test.shape[0]):
graphs_test.set_number_of_graph_nodes(graph_id, number_of_nodes)

graphs_test.prepare_node_configuration()

for graph_id in range(X_test.shape[0]):
number_of_outgoing_edges = 0
graphs_test.add_graph_node(graph_id, 'Image Node', number_of_outgoing_edges)

graphs_test.prepare_edge_configuration()

for graph_id in range(X_test.shape[0]):
if graph_id % 1000 == 0:
print(graph_id, X_test.shape[0])

for k in X_test[graph_id].nonzero()[0]:
graphs_test.add_graph_node_property(graph_id, 'Image Node', "W%d,%d" % (k // 28, k % 28))

graphs_test.encode()

print("Testing data produced")

tm = MultiClassGraphTsetlinMachine(
Expand Down Expand Up @@ -125,16 +109,16 @@ def default_args(**kwargs):

print("%d %.2f %.2f %.2f %.2f" % (i, result_train, result_test, stop_training-start_training, stop_testing-start_testing))

weights = tm.get_state()[1].reshape(2, -1)
for i in range(tm.number_of_clauses):
print("Clause #%d Weights:(%d %d)" % (i, weights[0,i], weights[1,i]), end=' ')
l = []
for k in range(args.hypervector_size * 2):
if tm.ta_action(0, i, k):
if k < args.hypervector_size:
l.append("x%d" % (k))
else:
l.append("NOT x%d" % (k - args.hypervector_size))
print(" AND ".join(l))

print(graphs_train.hypervectors)
# weights = tm.get_state()[1].reshape(2, -1)
# for i in range(tm.number_of_clauses):
# print("Clause #%d Weights:(%d %d)" % (i, weights[0,i], weights[1,i]), end=' ')
# l = []
# for k in range(args.hypervector_size * 2):
# if tm.ta_action(0, i, k):
# if k < args.hypervector_size:
# l.append("x%d" % (k))
# else:
# l.append("NOT x%d" % (k - args.hypervector_size))
# print(" AND ".join(l))

# print(graphs_train.hypervectors)
21 changes: 2 additions & 19 deletions examples/NoisyXORDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,24 @@ def default_args(**kwargs):
print("Creating training data")

# Create train data

graphs_train = Graphs(
args.number_of_examples,
symbols=['A', 'B'],
hypervector_size=args.hypervector_size,
hypervector_bits=args.hypervector_bits,
)

for graph_id in range(args.number_of_examples):
graphs_train.set_number_of_graph_nodes(graph_id, 2)

graphs_train.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
number_of_outgoing_edges = 1
graphs_train.add_graph_node(graph_id, 'Node 1', number_of_outgoing_edges)
graphs_train.add_graph_node(graph_id, 'Node 2', number_of_outgoing_edges)

graphs_train.prepare_edge_configuration()

graphs_train.prepar_eedge_configuration()
for graph_id in range(args.number_of_examples):
edge_type = "Plain"
graphs_train.add_graph_node_edge(graph_id, 'Node 1', 'Node 2', edge_type)
graphs_train.add_graph_node_edge(graph_id, 'Node 2', 'Node 1', edge_type)

Y_train = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
x1 = random.choice(['A', 'B'])
Expand All @@ -74,32 +67,23 @@ def default_args(**kwargs):

if np.random.rand() <= args.noise:
Y_train[graph_id] = 1 - Y_train[graph_id]

graphs_train.encode()

# Create test data

# Create test data
print("Creating testing data")

graphs_test = Graphs(args.number_of_examples, init_with=graphs_train)

for graph_id in range(args.number_of_examples):
graphs_test.set_number_of_graph_nodes(graph_id, 2)

graphs_test.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
number_of_outgoing_edges = 1
graphs_test.add_graph_node(graph_id, 'Node 1', number_of_outgoing_edges)
graphs_test.add_graph_node(graph_id, 'Node 2', number_of_outgoing_edges)

graphs_test.prepare_edge_configuration()

for graph_id in range(args.number_of_examples):
edge_type = "Plain"
graphs_test.add_graph_node_edge(graph_id, 'Node 1', 'Node 2', edge_type)
graphs_test.add_graph_node_edge(graph_id, 'Node 2', 'Node 1', edge_type)

Y_test = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
x1 = random.choice(['A', 'B'])
Expand All @@ -112,7 +96,6 @@ def default_args(**kwargs):
Y_test[graph_id] = 0
else:
Y_test[graph_id] = 1

graphs_test.encode()

tm = MultiClassGraphTsetlinMachine(
Expand Down
16 changes: 0 additions & 16 deletions examples/NoisyXORMNISTDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,18 @@ def default_args(**kwargs):
hypervector_size=args.hypervector_size,
hypervector_bits=args.hypervector_bits,
)

for graph_id in range(args.number_of_examples):
graphs_train.set_number_of_graph_nodes(graph_id, 2)

graphs_train.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
number_of_outgoing_edges = 1
graphs_train.add_graph_node(graph_id, 'Node 1', number_of_outgoing_edges)
graphs_train.add_graph_node(graph_id, 'Node 2', number_of_outgoing_edges)

graphs_train.prepare_edge_configuration()

for graph_id in range(args.number_of_examples):
edge_type = "Plain"
graphs_train.add_graph_node_edge(graph_id, 'Node 1', 'Node 2', edge_type)
graphs_train.add_graph_node_edge(graph_id, 'Node 2', 'Node 1', edge_type)

Y_train = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
x1 = random.choice([0, 1])
Expand All @@ -91,32 +85,23 @@ def default_args(**kwargs):

if np.random.rand() <= args.noise:
Y_train[graph_id] = 1 - Y_train[graph_id]

graphs_train.encode()

# Create test data

print("Creating testing data")

graphs_test = Graphs(args.number_of_examples, init_with=graphs_train)

for graph_id in range(args.number_of_examples):
graphs_test.set_number_of_graph_nodes(graph_id, 2)

graphs_test.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
number_of_outgoing_edges = 1
graphs_test.add_graph_node(graph_id, 'Node 1', number_of_outgoing_edges)
graphs_test.add_graph_node(graph_id, 'Node 2', number_of_outgoing_edges)

graphs_test.prepare_edge_configuration()

for graph_id in range(args.number_of_examples):
edge_type = "Plain"
graphs_test.add_graph_node_edge(graph_id, 'Node 1', 'Node 2', edge_type)
graphs_test.add_graph_node_edge(graph_id, 'Node 2', 'Node 1', edge_type)

Y_test = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
x1 = random.choice([0, 1])
Expand All @@ -133,7 +118,6 @@ def default_args(**kwargs):
Y_test[graph_id] = 0
else:
Y_test[graph_id] = 1

graphs_test.encode()

tm = MultiClassGraphTsetlinMachine(
Expand Down
14 changes: 0 additions & 14 deletions examples/SequenceClassificationDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,21 @@ def default_args(**kwargs):
print("Creating training data")

# Create train data

graphs_train = Graphs(
args.number_of_examples,
symbols=['A'],
hypervector_size=args.hypervector_size,
hypervector_bits=args.hypervector_bits,
double_hashing = args.double_hashing
)

for graph_id in range(args.number_of_examples):
graphs_train.set_number_of_graph_nodes(graph_id, np.random.randint(args.number_of_classes, args.max_sequence_length+1))

graphs_train.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
for node_id in range(graphs_train.number_of_graph_nodes[graph_id]):
number_of_edges = 2 if node_id > 0 and node_id < graphs_train.number_of_graph_nodes[graph_id]-1 else 1
graphs_train.add_graph_node(graph_id, node_id, number_of_edges)

graphs_train.prepare_edge_configuration()

Y_train = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
for node_id in range(graphs_train.number_of_graph_nodes[graph_id]):
Expand All @@ -76,26 +70,19 @@ def default_args(**kwargs):

if np.random.rand() <= args.noise:
Y_train[graph_id] = np.random.choice(np.setdiff1d(np.arange(args.number_of_classes), [Y_train[graph_id]]))

graphs_train.encode()

# Create test data

print("Creating testing data")

graphs_test = Graphs(args.number_of_examples, init_with=graphs_train)
for graph_id in range(args.number_of_examples):
graphs_test.set_number_of_graph_nodes(graph_id, np.random.randint(args.number_of_classes, args.max_sequence_length+1))

graphs_test.prepare_node_configuration()

for graph_id in range(args.number_of_examples):
for node_id in range(graphs_test.number_of_graph_nodes[graph_id]):
number_of_edges = 2 if node_id > 0 and node_id < graphs_test.number_of_graph_nodes[graph_id]-1 else 1
graphs_test.add_graph_node(graph_id, node_id, number_of_edges)

graphs_test.prepare_edge_configuration()

Y_test = np.empty(args.number_of_examples, dtype=np.uint32)
for graph_id in range(args.number_of_examples):
for node_id in range(graphs_test.number_of_graph_nodes[graph_id]):
Expand All @@ -113,7 +100,6 @@ def default_args(**kwargs):
node_id = np.random.randint(Y_test[graph_id], graphs_test.number_of_graph_nodes[graph_id])
for node_pos in range(Y_test[graph_id] + 1):
graphs_test.add_graph_node_property(graph_id, node_id - node_pos, 'A')

graphs_test.encode()

tm = MultiClassGraphTsetlinMachine(
Expand Down
2 changes: 2 additions & 0 deletions examples/recomm_system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd examples/recomm_system/
bash main.sh
Binary file not shown.
Loading