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

TUKR実装-福永 #15

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
187 changes: 187 additions & 0 deletions Lecture_TUKR/TUKR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import numpy as np

from tqdm import tqdm #プログレスバーを表示させてくれる
import jax,jaxlib
import jax.numpy as jnp


class UKR:
def __init__(self, X, latent_dim, sigma1, sigma2, prior='random', Uinit=None, Vinit=None):
#--------初期値を設定する.---------
self.X = X
#ここから下は書き換えてね
self.xsamples = X.shape[0]
self.ysamples = X.shape[1]
self.ob_dim = X.shape[2]
self.sigma1 = sigma1
self.sigma2 = sigma2
self.latent_dim = latent_dim


if Uinit is None:
if prior == 'random': #一様事前分布のとき
self.U = np.random.uniform(low=-0.001, high=0.001, size=(self.xsamples, self.latent_dim))


else: #ガウス事前分布のとき
self.U = np.random.normal(0, self.sigma1, (self.xsamples, self.latent_dim))

else: #Zの初期値が与えられた時
self.U = Uinit
self.history = {}
if Vinit is None:
if prior == 'random': #一様事前分布のとき
self.V = np.random.uniform(low=-0.001, high=0.001, size=(self.ysamples, self.latent_dim))

else: #ガウス事前分布のとき
self.V = np.random.normal(self.ysamples*self.latent_dim).reshape(self.ysamples, self.latent_dim)
else: #Zの初期値が与えられた時
self.V = Vinit

self.history = {}
def f(self, U, V):
Ud = np.sum((U[:, None, :] - self.U[None, :, :])**2, axis=2)
UH = -1*(Ud/(2*self.sigma1**2))
Uh = jnp.exp(UH)

Vd = np.sum((V[:, None, :] - self.V[None, :, :]) ** 2, axis=2)
VH = -1 * (Vd / (2 * self.sigma2 ** 2))
Vh = jnp.exp(VH)

bunshi = jnp.einsum('KI,MJ,IJD -> KMD', Uh, Vh, self.X)
# aaa = jnp.einsum('MJ,IJD -> IMD', Vh, self.X)
# bunshi = jnp.einsum('KI,IMD -> KMD', Uh, aaa)

bunbo = jnp.einsum('KI,MJ -> KM', Uh, Vh)
newbunbo = bunbo[:, :, None]



f = bunshi/newbunbo
# print(U.shape)
#写像の計算
return f
def ff(self, U, V, epoch):
Ud = np.sum((U[:, None, :] - self.history['u'][epoch][None, :, :]) ** 2, axis=2)
UH = -1 * (Ud / (2 * self.sigma1 ** 2))
Uh = jnp.exp(UH)

Vd = np.sum((V[:, None, :] - self.history['v'][epoch][None, :, :]) ** 2, axis=2)
VH = -1 * (Vd / (2 * self.sigma2 ** 2))
Vh = jnp.exp(VH)

bunshi = jnp.einsum('KI,MJ,IJD -> KMD', Uh, Vh, self.X)
# aaa = jnp.einsum('MJ,IJD -> IMD', Vh, self.X)
# bunshi = jnp.einsum('KI,IMD -> KMD', Uh, aaa)

bunbo = jnp.einsum('KI,MJ -> KM', Uh, Vh)
newbunbo = bunbo[:, :, None]

return bunshi/newbunbo

def E(self, U, V, X, alpha, norm): #目的関数の計算
E = jnp.sum((X - self.f(U, V))**2)
UR = alpha * jnp.sum(jnp.abs(U ** norm))
VR = alpha * jnp.sum(jnp.abs(V ** norm))
E = E/self.xsamples/self.ysamples + UR/self.xsamples + VR/self.ysamples

return E



def fit(self, nb_epoch: int, eta: float, alpha: float, norm: float):
# 学習過程記録用
self.history['u'] = np.zeros((nb_epoch, self.xsamples, self.latent_dim))
self.history['v'] = np.zeros((nb_epoch, self.ysamples, self.latent_dim))
self.history['f'] = np.zeros((nb_epoch, self.xsamples, self.ysamples, self.ob_dim))
self.history['error'] = np.zeros(nb_epoch)

for epoch in tqdm(np.arange(nb_epoch)):
dEdu = jax.grad(self.E, argnums=0)(self.U, self.V, self.X, alpha, norm)
self.U = self.U - eta * dEdu
dEdv = jax.grad(self.E, argnums=1)(self.U, self.V, self.X, alpha, norm)
self.V = self.V - eta * dEdv

# U,Vの更新




# 学習過程記録用
self.history['u'][epoch] = self.U
self.history['v'][epoch] = self.V
self.history['f'][epoch] = self.f(self.U, self.V)
self.history['error'][epoch] = self.E(self.U, self.V, self.X, alpha, norm)

#--------------以下描画用(上の部分が実装できたら実装してね)---------------------
def calc_approximate_fu(self, resolution): #fのメッシュ描画用,resolution:一辺の代表点の数
nb_epoch = self.history['u'].shape[0]

self.history['y'] = np.zeros((nb_epoch, self.xsamples, self.ysamples, self.ob_dim))
for epoch in range(nb_epoch):
uzeta = self.create_uzeta(self.history['u'][epoch])
vzeta = self.create_vzeta(self.history['v'][epoch])
Y = self.ff(uzeta, vzeta, epoch)
# print(self.history['y'][epoch])
self.history['y'][epoch] = Y



def create_uzeta(self, Z): #fのメッシュの描画用に潜在空間に代表点zetaを作る.

u_y = np.linspace(np.amin(Z), np.amax(Z), self.xsamples).reshape(-1, 1)
# UYY = np.meshgrid(u_y)
# A = np.amin(Z)
# print(self.U.shape)

# zeta = np.concatenate([uxx[:, None], uyy[:, None]], axis=1)
return u_y

def create_vzeta(self, Z): #fのメッシュの描画用に潜在空間に代表点zetaを作る.

v_y = np.linspace(np.min(Z), np.max(Z), self.ysamples).reshape(-1, 1)
# UYY = np.meshgrid(u_y)


# zeta = np.concatenate([uxx[:, None], uyy[:, None]], axis=1)
return v_y



if __name__ == '__main__':
from Lecture_UKR.data import create_kura
from Lecture_UKR.data import create_rasen
# from Lecture_UKR.data import create_2d_sin_curve
from Lecture_TUKR.visualizer import visualize_history
from Lecture_TUKR.data_scratch import load_kura_tsom
from Lecture_TUKR.animal import load_date

#各種パラメータ変えて遊んでみてね.
epoch = 200 #学習回数
sigma1 = 0.1 #カーネルの幅
sigma2 = 0.1
eta = 8 #学習率
latent_dim = 2 #潜在空間の次元
alpha = 0.01
norm = 10
seed = 4
np.random.seed(seed)

#入力データ(詳しくはdata.pyを除いてみると良い)
xsamples = 20 #データ数
ysamples = 10
# X = load_kura_tsom(xsamples, ysamples) #鞍型データ ob_dim=3, 真のL=2
X = load_date()[0][:, :, None]
animal_label = load_date(retlabel_animal=True)[1]
feature_label = load_date(retlabel_feature=True)[2]

#X = create_rasen(nb_samples) #らせん型データ ob_dim=3, 真のL=1
# X = create_2d_sin_curve(nb_samples) #sin型データ ob_dim=2, 真のL=1
ukr = UKR(X, latent_dim, sigma1, sigma2, prior='random')
ukr.fit(epoch, eta, alpha, norm)
#visualize_history(X, ukr.history['f'], ukr.history['z'], ukr.history['error'], save_gif=False, filename="tmp")

#----------描画部分が実装されたらコメントアウト外す----------
ukr.calc_approximate_fu(resolution=10)
# ukr.calc_approximate_fv(resolution=10)
visualize_history(X, ukr.history['y'], ukr.history['u'], ukr.history['v'], ukr.history['error'], save_gif=True, filename="/Users/furukawashuushi/Desktop/3ヶ月コースGIF/TUKR動物", label1=animal_label, label2=feature_label)
28 changes: 28 additions & 0 deletions Lecture_TUKR/animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import os


def load_date(retlabel_animal=True, retlabel_feature=True):
datestore_name = 'datestore/animal'
file_name = 'features.txt'

directory_path = os.path.join(os.path.dirname(__file__), datestore_name)
file_path = os.path.join(directory_path, file_name)

x = np.loadtxt(file_path)

return_objects = [x]

if retlabel_animal:
label_name = 'labels_animal.txt'
label_path =os.path.join(directory_path, label_name)
label_animal =np.genfromtxt(label_path, dtype=str)
return_objects.append(label_animal)

if retlabel_feature:
label_name ='labels_feature.txt'
label_path =os.path.join(directory_path, label_name)
label_feature =np.genfromtxt(label_path, dtype=str)
return_objects.append(label_feature)

return return_objects
27 changes: 13 additions & 14 deletions Lecture_TUKR/data_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
import matplotlib.pyplot as plt

def load_kura_tsom(xsamples, ysamples, missing_rate=None,retz=False):
z1 =
z2 =
z1 = np.random.normal(0, 0.5, xsamples)
z2 = np.random.normal(0, 0.5, ysamples)

z1_repeated, z2_repeated =
x1 =
x2 =
x3 =
z1_repeated, z2_repeated = np.meshgrid(z1, z2, indexing = 'ij')
x1 = z1_repeated #+ np.random.normal(loc=0.0, scale=0.1, size=(xsamples, ysamples))
x2 = z2_repeated #+ np.random.normal(loc=0.0, scale=0.1, size=(xsamples, ysamples))
x3 = x1**2 - x2**2
#ノイズを加えたい時はここをいじる,locがガウス分布の平均、scaleが分散,size何個ノイズを作るか
#このノイズを加えることによって三次元空間のデータ点は上下に動く

x =
truez =

x = np.concatenate((x1[:, :, np.newaxis],x2[:, :, np.newaxis], x3[:, :, np.newaxis]), axis=2)
truez = np.concatenate((x1[:, :, np.newaxis],x2[:, :, np.newaxis], x3[:, :, np.newaxis]), axis=2)
if missing_rate == 0 or missing_rate == None:
if retz:
return x, truez
Expand All @@ -25,13 +24,13 @@ def load_kura_tsom(xsamples, ysamples, missing_rate=None,retz=False):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

xsamples =
ysamples =
xsamples = 10
ysamples = 5

x, truez = load_kura_tsom(xsamples, ysamples, retz=True)

x, truez = load_kura_tsom()

fig = plt.figure(figsize=[5, 5])
ax_x = fig.add_subplot(projection='3d')
ax_x.scatter()
ax_x.scatter(x[:, :, 0].flatten(), x[:, :, 1].flatten(), x[:, :, 2].flatten())
ax_x.set_title('Generated three-dimensional data')
plt.show()
17 changes: 17 additions & 0 deletions Lecture_TUKR/datestore/animal/features.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.5 0.5 0.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.5 0.5 0.0 0.0 0.0
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.5 0.5 0.0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 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0
17 changes: 17 additions & 0 deletions Lecture_TUKR/datestore/animal/labels_animal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dove
cock
duck
w_duck
owl
hawk
eagle
crow
fox
dog
wolf
cat
tiger
lion
horse
zebra
cattle
21 changes: 21 additions & 0 deletions Lecture_TUKR/datestore/animal/labels_feature.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
small
medium
large
nocturnality
two_legs
four_legs
hair
hoof
mane
wing
stripe
hunt
run
fly
swim
domestic
herbivorous
carnivore
canidae
felidae
pet
Loading