-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_tf.py
More file actions
executable file
·46 lines (37 loc) · 1.1 KB
/
models_tf.py
File metadata and controls
executable file
·46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 25 13:20:11 2018
@author: Paris
"""
import tensorflow as tf
import numpy as np
import timeit
class PCA:
# Initialize the class
def __init__(self, X, Z_dim):
# Normalize
self.Xmean = X.mean(0)
X = X - self.Xmean
self.X = X
self.N = X.shape[0]
self.Z_dim = Z_dim
# Sample covariance matrix
self.S = np.matmul(X.T,X)/(self.N-1)
# Computes the eigendecomposition of the covariance
def fit(self):
values, vectors = np.linalg.eig(self.S)
idx = np.argsort(values)[::-1]
self.values = values[idx[:self.Z_dim]]
self.vectors = vectors[:,idx[:self.Z_dim]]
# Encodes data into latent space
def encode(self, X_star):
X_star = X_star - self.Xmean
Z = np.matmul(self.X, self.vectors)
return Z
# Decodes latent values back to physical space
def decode(self, Z):
X = np.matmul(Z, self.vectors.T)
# De-normalize
X = X + self.Xmean
return X