Skip to content

Commit

Permalink
added code to generate perfectly decomposable tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonzhang929 committed Apr 30, 2019
1 parent 5e648e5 commit 38f1e22
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
79 changes: 61 additions & 18 deletions SGD/gradient1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import ctf, time, random
import ctf, time, random, sys
import numpy as np
from functools import reduce
import numpy.linalg as la
from ctf import random as crandom

glob_comm = ctf.comm()
import gzip
import shutil
import os

INDEX_STRING = "ijklmnopq"

Expand All @@ -22,8 +25,6 @@ def sparse_update(T, factors, Lambda, sizes, rank, stepSize, sample_rate, times)
R.i(indexes) << T.i(indexes) - ctf.TTTP(omega, factors).i(indexes)
times[3] += time.time() - starting_time
starting_time = time.time()
if ctf.comm().rank() == 0:
print("check")
H = ctf.tensor(tuple((sizes[:i] + sizes[i + 1:] + [rank])))
times[4] += time.time() - starting_time
starting_time = time.time()
Expand Down Expand Up @@ -72,10 +73,11 @@ def sparse_SGD(T, U, V, W, Lambda, omega, I, J, K, r, stepSize, sample_rate):
next_err_norm = diff_norm + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda
#print(curr_err_norm, next_err_norm, diff_norm)
if ctf.comm().rank() == 0:
print(diff_norm, time.time() - start_time, total_count)
print(times)
x = time.time() - start_time
print(diff_norm, x, total_count, x/total_count)
# print(times)

if abs(curr_err_norm - next_err_norm) < .001 or iteration_count > work_cycle * 50:
if abs(curr_err_norm - next_err_norm) < .001 or iteration_count > work_cycle * 0:
break

curr_err_norm = next_err_norm
Expand All @@ -91,33 +93,74 @@ def getOmega(T):
Omega.write(inds,data)
return Omega


modify = False


def read_from_frostt(file_name, I, J, K):
unzipped_file_name = file_name + '.tns'
exists = os.path.isfile(unzipped_file_name)

if not exists:
if glob_comm.rank() == 0:
print('Creating ' + unzipped_file_name)
with gzip.open(file_name + '.tns.gz', 'r') as f_in:
with open(unzipped_file_name, 'w') as f_out:
shutil.copyfileobj(f_in, f_out)

T_start = ctf.tensor((I + 1, J + 1, K + 1), sp=True)
if glob_comm.rank() == 0:
print('T_start initialized')
T_start.read_from_file(unzipped_file_name)
if glob_comm.rank() == 0:
print('T_start read in')
T = ctf.tensor((I, J, K), sp=True)
if glob_comm.rank() == 0:
print('T initialized')
T[:, :, :] = T_start[1:, 1:, 1:]
if glob_comm.rank() == 0:
print('T filled')

if modify:
T.write_to_file(unzipped_file_name)

return T

def creat_perfect_tensor(I, J, K, r, sparsity):
U = ctf.random.random((I, r))
V = ctf.random.random((J, r))
W = ctf.random.random((K, r))
T = ctf.tensor((I, J, K), sp=True)
T.i("ijk") << U.i("ir") * V.i("jr") * W.i("kr")
T.sample(sparsity)
return T

def main():
s = 2000
sparsity = 100
r = 16
regParam = 0.00001
stepSize = 0.01
sample_rate = 0.01

I = s
J = s
K = s
T = ctf.tensor((I, J, K), sp=True)
T.fill_sp_random(0., 1., 1. / sparsity)
file_name = sys.argv[1]
I = int(sys.argv[2])
J = int(sys.argv[3])
K = int(sys.argv[4])
stepSize = int(sys.argv[5])
r = int(sys.argv[6])
sample_rate = float(sys.argv[7])

T = read_from_frostt(file_name, I, J, K)
# T.read_from_file("T.txt")
Omega = getOmega(T)

s = T.sum()
os = Omega.sum()
if ctf.comm().rank() == 0:
print(s, os)
print (sys.argv)
#T = function_tensor(I, J, K, sparsity)
U = ctf.random.random((I, r))
V = ctf.random.random((J, r))
W = ctf.random.random((K, r))

#T.write_to_file("T.txt")
sparse_SGD(T, ctf.tensor(copy=U), ctf.tensor(copy=V), ctf.tensor(copy=W), regParam, Omega, I, J, K, r, stepSize, sample_rate)
sparse_SGD(T, U, V, W, regParam, Omega, I, J, K, r, stepSize, sample_rate)

main()

12 changes: 12 additions & 0 deletions data/function_tensor/perfect_tensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ctf

glob_comm = ctf.comm()

def creat_perfect_tensor(I, J, K, r, sparsity):
U = ctf.random.random((I, r))
V = ctf.random.random((J, r))
W = ctf.random.random((K, r))
T = ctf.tensor((I, J, K), sp=True)
T.i("ijk") << U.i("ir") * V.i("jr") * W.i("kr")
T.sample(sparsity)
return T

0 comments on commit 38f1e22

Please sign in to comment.