<jupyter_start><jupyter_text>Training 1<jupyter_code>import torch.utils.data
dataset = torch.utils.data.TensorDataset(train_xy[1:1000,:,:], train_xyz[1:1000,:,:])
loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Assume that we are on a CUDA machine, then this should print a CUDA device:
print(device)
model.to(device)

lr, epochs, batch_size = 1e-1 , 50 , 10
optimizer = torch.optim.SGD(model.parameters(), lr = lr )
train_accum=[]
i = 0
t = Variable(torch.randn(1,1), requires_grad = True).to(device)
print(t)
maxk=10
gamma=2
mu=1
L=2
kappa = np.random.random()*0.5
epsilon = np.random.random()*0.1
epochaccum = []
for k in range(epochs):
    localaccum = []
    for xy, xyz in iter(loader):
        i =  i+1
        local_xy = Variable(xy)
        local_xyz = Variable(xyz)
        local_xy = local_xy.to(device)
        outputs = model.forward(local_xy)
        
        output_sda = outputs[0]
        output_sda = output_sda.to(device)

        loss_sda = criterion(output_sda, torch.Tensor.new_zeros(output_sda, output_sda.size()))

        #print(local_xy, local_xyz)
        #if (i%500 == 0): print("network loss -> ", loss)
        
        output_sym = outputs[1]
        output_sym = output_sym.to(device)
        
        model.zero_grad() 
        loss_sda.backward()
        
        optimizer.step()
        localaccum.append(loss_sda.data[0])
        
        #print("GRAD msda ", grads['msda'])
        #print("GRAD msda ", grads['sym'])
        
        z_msda = outputs[2].to(device)
        z_sym = outputs[3].to(device)
        G = outputs[4]
        n,d,l = local_xy.size()
        
        xy,conn = torch.Tensor.split(local_xy,[vertices,l-vertices],dim=2)
        
        g_msda = grads['msda'].to(device)
        g_sym = grads['sym'].to(device)

        args = (t,z_msda,z_sym,g_msda,g_sym,xy[:,0,:],xy[:,1,:],G,gamma)
        fun_min = model.f_gamma(*args)
 
        loss_f_star = criterion(fun_min, torch.Tensor.new_zeros(fun_min, fun_min.size()))
        optimizer.step()
        
        z_k = model.get_next_z(z_msda,mu=2,L=1)
        #print("z_k -> ", z_k)
        for j in range(maxk):
            args1 = (t,z_msda,z_sym,g_msda,g_sym,xy[:,0,:],xy[:,1,:],G,mu)
            args2 = (t,z_msda,z_sym,g_msda,g_sym,xy[:,0,:],xy[:,1,:],G,L)
            if (model.f_star(z_k,*args1) >= (1-kappa)*model.f_star(z_k,*args2)):
                #print(model.f_star(z_k,*args1))
                #print((1-kappa)*model.f_star(z_k,*args2))
                z_kj=z_k
                #print("z_kj=z_k")
                break
            z_kj=model.f_star(zk,L,*args)
            if(f_star(zkj,L,*args) < epsilon):
                #print("z_kj < e")
                break           
            t = find_root(zkj,t)
        #print("z_kj ->", z_kj) 
        sym_val = model.f_sym(xy[:,0,:],xy[:,1,:],z_kj).to(device)
        
        #print(output_sym, sym_val)
        
        loss_sym = criterion(output_sym, sym_val.data)
        loss_sym.backward()
    
    train_accum.append(np.mean(localaccum))
    epochaccum.append(localaccum)
    
plt.plot(train_accum);

plt.plot(epochaccum[9]);
plt.plot(train_accum[0:5]);
train_accum<jupyter_output><empty_output><jupyter_text>TEST BY RENDERING<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt


    
def test_reconstruct(N):
    print(N)
    print("******************************")
    fig = plt.figure()
    ax = Axes3D(fig)

    test_obj=test_xyz[N,:,:]
    d,t = test_obj.size()
    obj,conn = torch.Tensor.split(test_obj,[8,t-8],dim=1)
    #print(obj)
    #print(conn)
    fullobj = np.concatenate((obj,conn), axis=1)
    computed_z = model.forward(torch.Tensor([fullobj]).to(device), extract_z=True)
    orig_z = obj[2]


    print("_________ORIG________")

    x=obj[0]
    y=obj[1]
    z=obj[2]


    x=[(i-min(x))/(max(x)-min(x)) for i in x]
    y=[(i-min(y))/(max(y)-min(y)) for i in y]
    z=[(i-min(z))/(max(z)-min(z)) for i in z]

    verts = [list(zip(x, y,z))]
    #ax.add_collection3d(Poly3DCollection(verts))
    ax.scatter(x, y, z, c='r', marker='o')
    
    
    xbar = sum(x)/len(x)
    ybar = sum(y)/len(y)
    zbar = sum(z)/len(z)

    ax.scatter(xbar, ybar, zbar, c='b', marker='x')
    
    C = np.array((xbar ,ybar, zbar))
    P = [np.array((x[i] ,y[i], z[i])) for i in range(len(x))]
    #print(C,P)
    D = [np.linalg.norm(a-C) for a in P]
    print(D)
    M = statistics.median(D)
    print(M)
    #SD = statistics.stdev(D)
    SD = np.std(D)
    print(SD)
    circularity = SD/M
    print("circularity: ",circularity)

    plt.show()    



    print("_________RECON________")

    fig = plt.figure()
    ax = Axes3D(fig)
    x=obj[0]
    y=obj[1]
    z=computed_z.to('cpu').detach().numpy()[0]
    #print("Test  obj's   z: ", obj[2])
    #print("Model computed z:", z)

    x=[(i-min(x))/(max(x)-min(x)) for i in x]
    y=[(i-min(y))/(max(y)-min(y)) for i in y]
    z=[(i-min(z))/(max(z)-min(z)) for i in z]

    verts = [list(zip(x, y,z))]
    #ax.add_collection3d(Poly3DCollection(verts))
    ax.scatter(x, y, z, c='r', marker='o')
    
    xbar = sum(x)/len(x)
    ybar = sum(y)/len(y)
    zbar = sum(z)/len(z)

    ax.scatter(xbar, ybar, zbar, c='b', marker='x')
    
    C = np.array((xbar ,ybar, zbar))
    P = [np.array((x[i] ,y[i], z[i])) for i in range(len(x))]
    #print(C,P)
    D = [np.linalg.norm(a-C) for a in P]
    print(D)
    M = statistics.median(D)
    print(M)
    #SD = statistics.stdev(D)
    SD = np.std(D)
    print(SD)
    circularity = SD/M
    print("circularity: ",circularity)

    plt.show()    


N=[15,]
#for i in range(50,60):
#    test_reconstruct(i)
orig_obj = [[  0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265],
        [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344 ],
        [ 0.6691,  0.9135, -0.6691, -0.5000,  0.6691,  0.9135, -0.6691,
         -0.5000 ]]
obj = [[[ 0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265,  1.0000,  1.0000,  1.0000,  0.0000,  1.0000,  1.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  1.0000,  1.0000,  1.0000,
          1.0000,  1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          0.0000],
        [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344,  1.0000,  1.0000,  1.0000,  0.0000,  1.0000,  1.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  1.0000,  1.0000,  1.0000,
          1.0000,  1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          0.0000],
        [ 0.6691,  0.9135, -0.6691, -0.5000,  0.6691,  0.9135, -0.6691,
         -0.5000,  1.0000,  1.0000,  1.0000,  0.0000,  1.0000,  1.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          1.0000,  0.0000,  0.0000,  0.0000,  1.0000,  1.0000,  1.0000,
          1.0000,  1.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
          0.0000]]]

model.forward(torch.Tensor(obj).to(device), extract_x=True)

model.forward(torch.Tensor(obj).to(device), extract_y=True)

model.forward(torch.Tensor(obj).to(device), extract_z=True)

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import statistics

fig = plt.figure()
ax = Axes3D(fig)

x = [ 0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265 ]
y =  [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344 ]
z =  [ 0.6691,  0.9135, -0.6691, -0.5000,  0.6691,  0.9135, -0.6691,
        -0.5000 ]

x=[(i-min(x))/(max(x)-min(x)) for i in x]

y=[(i-min(y))/(max(y)-min(y)) for i in y]

z=[(i-min(z))/(max(z)-min(z)) for i in z]

verts = [list(zip(x, y,z))]
#ax.add_collection3d(Poly3DCollection(verts))
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('Y axis')
ax.set_ylabel('X axis')
ax.set_zlabel('Z axis')
ax.set_zlim(0, 1)

xbar = sum(x)/len(x)
ybar = sum(y)/len(y)
zbar = sum(z)/len(z)

ax.scatter(xbar, ybar, zbar, c='b', marker='x')

C = np.array((xbar ,ybar, zbar))
P = [np.array((x[i] ,y[i], z[i])) for i in range(len(x))]
#print(C,P)
D = [np.linalg.norm(a-C) for a in P]
#print(D)
M = statistics.median(D)
#print(M)
SD = statistics.stdev(D)
#print(SD)
circularity = M/SD
print("circularity: ",circularity)

plt.show()
# for the report - delete later

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)

x = [ 0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265 ]
y =  [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344 ]
z =  [ 0.0,  0.0, 0.0, 0.0,  0.0,  0.0, 0.0001,
        0.0 ]
#z = [-1,-1,-1,-1,-1,-1,-1,-1]

x=[(i-min(x))/(max(x)-min(x)) for i in x]

y=[(i-min(y))/(max(y)-min(y)) for i in y]

z=[(i-min(z))/(1) for i in z]

verts = [list(zip(x, y, z))]
#ax.add_collection3d(Poly3DCollection(verts))
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('Y axis')
ax.set_ylabel('X axis')
ax.set_zlabel('Z axis')
ax.set_zlim(0, 1)

xbar = sum(x)/len(x)
ybar = sum(y)/len(y)
zbar = sum(z)/len(z)

ax.scatter(xbar, ybar, zbar, c='b', marker='x')

plt.show()
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [ 0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265 ]
y =  [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344 ]
z = [ 1.0587,  1.2730,  0.7061,  0.8346,  1.1649,  1.4251,  0.9184,
          0.9816]
x=[(i-min(x))/(max(x)-min(x)) for i in x]
y=[(i-min(y))/(max(y)-min(y)) for i in y]
z=[(i-min(z))/(max(z)-min(z)) for i in z]

verts = [list(zip(x, y,z))]
#ax.add_collection3d(Poly3DCollection(verts))
ax.scatter(x, y, z, c='r', marker='o')

xbar = sum(x)/len(x)
ybar = sum(y)/len(y)
zbar = sum(z)/len(z)

ax.scatter(xbar, ybar, zbar, c='b', marker='x')

xdiff = [xi-xbar for xi in x]
ydiff = [yi-ybar for yi in y]
zdiff = [zi-zbar for zi in z]

points = zip(x,y,x)

#xsd = np.std()

plt.show()<jupyter_output><empty_output><jupyter_text>Trial 2<jupyter_code>model.forward(torch.Tensor(obj).to(device), extract_z=True)
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [ 0.7319, -0.4006, -0.7319,  0.8529,  0.9055, -0.2269, -0.5582,
          1.0265 ]
y =  [ 0.1290, -0.0706, -0.1290,  0.1504, -0.8558, -1.0554, -1.1139,
         -0.8344 ]
z = [ 0.6459,  0.4539,  0.7028,  0.7240,  0.7935,  0.6045,  0.8322,
          0.8384]
x=[(i-min(x))/(max(x)-min(x)) for i in x]
y=[(i-min(y))/(max(y)-min(y)) for i in y]
z=[(i-min(z))/(max(z)-min(z)) for i in z]

verts = [list(zip(x, y,z))]
#ax.add_collection3d(Poly3DCollection(verts))
ax.scatter(x, y, z, c='r', marker='o')
plt.show()<jupyter_output><empty_output><jupyter_text>Test Performance<jupyter_code>import torch.utils.data
dataset = torch.utils.data.TensorDataset(test_xy[:,:,:], test_xyz[:,:,:])
loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Assume that we are on a CUDA machine, then this should print a CUDA device:
print(device)
model.to(device)
criterion = nn.L1Loss()
lr, epochs, batch_size = 1e-1 , 1 , 1
optimizer = torch.optim.SGD(model.parameters(), lr = lr )
accum=[]
i = 0
for k in range(epochs):
    localaccum = []
    for xy, xyz in iter(loader):
        i =  i+1
        local_xy = Variable(xy)
        #print(local_xy)
        local_xyz = Variable(xyz)
        local_xy = local_xy.to(device)
        local_xyz = local_xyz.to(device)
        outputs = model.forward(local_xy) #[val_msda,val_sym,z_star_msda,z_star_sym,G]
        output = outputs[0].to(device)
        #print("output:",output)
        #print("actual:", local_xyz[:,2,0:8])
        #loss = criterion(output, local_xyz[:,2,0:8])
        loss = criterion(output, torch.Tensor.new_zeros(output, output.size()))
        
        #print(local_xy, local_xyz)
        #if (i%100 == 0): print("network loss -> ", loss)
        #model.zero_grad()
        #loss.backward()
        #optimizer.step()
        localaccum.append(loss.data[0])
    accum.append(np.mean(localaccum))
plt.plot(localaccum);                                           
plt.ylabel('SDA')
plt.xlabel('Objects')
plt.ylim((0.06,0.08))
plt.plot(localaccum[0:1000])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19726455646737007, 'fraction_numerical': 0.10082063305978899, 'mean_word_length': 2.71329077190946, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '256896', 'license': 'MIT', 'n_tokens_mistral': 6407, 'n_tokens_neox': 5716, 'n_words': 1001, 'path': 'archive/Network Arch V6 - IE Project.ipynb', 'repo_name': 'palmishr/3dvisnet', 'size': '12795'}