Skip to content
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
28 changes: 28 additions & 0 deletions Lecture_TUKR/tanaka/animal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import os


def load_data(retlabel_animal=True, retlabel_feature=False):
datastore_name = 'datastore/animal'
file_name = 'features.txt'

directory_path = os.path.join(os.path.dirname(__file__), datastore_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
42 changes: 42 additions & 0 deletions Lecture_TUKR/tanaka/data_scratch_tanaka.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

def load_kura_tsom(xsamples, ysamples, missing_rate=None,retz=False):
# z1 = np.random.rand(xsamples)/2
z1 = np.linspace(-1,1,xsamples)
# z2 = np.random.rand(ysamples)/2
z2 = np.linspace(-1,1,ysamples)

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

x = np.concatenate((x1[:, :, np.newaxis], x2[:, :, np.newaxis], x3[:, :, np.newaxis]), axis=2)
truez = np.concatenate((z1_repeated[:, :, np.newaxis], z2_repeated[:, :, np.newaxis]), axis=2)
# print(x.shape)

if missing_rate == 0 or missing_rate == None:
if retz:
return x, truez, z1, z2
else:
return x

if __name__ == '__main__':
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

xsamples = 10
ysamples = 10

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

fig = plt.figure(figsize=[5, 5])
ax_x = fig.add_subplot(projection='3d')
ax_x.scatter(x[:, :, 0].flatten(), x[:, :, 1].flatten(), x[:, :, 2].flatten(), c=x[:, :, 0].flatten())
ax_x.set_title('Generated three-dimensional data')
plt.show()

17 changes: 17 additions & 0 deletions Lecture_TUKR/tanaka/datastore/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 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 1.0 1.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.3 0.0 0.0 1.0 1.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 1.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.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 1.0 1.0 1.0 0.0 0.0 0.0 0.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 1.0 0.0 0.5 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.3 0.7 1.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 1.0
0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0
1.0 0.0 0.0 0.5 0.0 1.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.0 1.0 1.0
0.0 0.0 1.0 0.5 0.0 1.0 1.0 0.0 0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0
0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0
0.0 0.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 1.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 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/tanaka/datastore/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/tanaka/datastore/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
Binary file added Lecture_TUKR/tanaka/tmp.mp4
Binary file not shown.
175 changes: 175 additions & 0 deletions Lecture_TUKR/tanaka/tukr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import numpy as np
import jax,jaxlib
import jax.numpy as jnp
import tensorflow as tf
from tqdm import tqdm #プログレスバーを表示させてくれる
from sklearn.datasets import load_iris



class TUKR:
def __init__(self, X, nb_samples1, nb_samples2, latent_dim1, latent_dim2, sigma1, sigma2, prior='random', Uinit=None, Vinit=None):
#--------初期値を設定する.---------
self.X = X
#ここから下は書き換えてね

if X.ndim == 3:
self.nb_samples1, self.nb_samples2, self.ob_dim = self.X.shape
else:
self.nb_samples1, self.nb_samples2 = self.X.shape
self.ob_dim = 1
self.X = X[:,:,None]

self.sigma1 = sigma1
self.sigma2 = sigma2
self.latent_dim1 = latent_dim1
self.latent_dim2 = latent_dim2
self.alpha = alpha
self.norm = norm

if Uinit is None:
if prior == 'random': #一様事前分布のとき
self.U = np.random.uniform(-0.1, 0.1, size=(self.nb_samples1, self.latent_dim1))
#(平均,標準偏差,配列のサイズ)
else: #ガウス事前分布のとき
self.U = np.random.uniform(0, 0.1 * self.sigma1, (self.nb_samples1, self.latent_dim1))
else: #Zの初期値が与えられた時
self.U = Uinit

self.history = {}

if Vinit is None:
if prior == 'random': # 一様事前分布のとき
self.V = np.random.normal(-0.1, 0.1 , size=(self.nb_samples2, self.latent_dim2))
# (平均,標準偏差,配列のサイズ)
else: #ガウス事前分布のとき
self.V = np.random.normal(0, 0.1 * self.sigma2, (self.nb_samples2, self.latent_dim2))
else: # Zの初期値が与えられた時
self.V = Vinit

self.history = {}

def f(self, U, V): #写像の計算
DistU = jnp.sum((U[:, None, :] - U[None, :, :]) ** 2, axis=2)
DistV = jnp.sum((V[:, None, :] - V[None, :, :]) ** 2, axis=2)
HU = jnp.exp((-1 * DistU) / (2 * (self.sigma1) ** 2))
HV = jnp.exp((-1 * DistV) / (2 * (self.sigma2) ** 2))
# GU = jnp.sum(HU, axis=1)[:, None]
# GV = jnp.sum(HV, axis=1)[:, None]
# RU = HU / GU
# RV = HV / GV
f = jnp.einsum('li,kj,ijd->lkd', HU, HV, self.X)
f1 = jnp.einsum('li,kj->lk', HU, HV)
f2 = f1[:, :, None]
return f / f2

def ff(self, U, V, epoch): #写像の計算
DistU = jnp.sum((U[:, None, :] - self.history['u'][epoch][None, :, :]) ** 2, axis=2)
DistV = jnp.sum((V[:, None, :] - self.history['v'][epoch][None, :, :]) ** 2, axis=2)
HU = jnp.exp((-1 * DistU) / (2 * (self.sigma1) ** 2))
HV = jnp.exp((-1 * DistV) / (2 * (self.sigma2) ** 2))
# GU = jnp.sum(HU, axis=1)[:, None]
# GV = jnp.sum(HV, axis=1)[:, None]
# RU = HU / GU
# RV = HV / GV
f = jnp.einsum('li,kj,ijd->lkd', HU, HV, self.X)
f1 = jnp.einsum('li,kj->lk', HU, HV)
f2 = f1[:, :, None]
return f / f2

def E(self,U,V,X,alpha,norm):#目的関数の計算
Y = self.f(U,V)
e = jnp.sum((X - Y) ** 2)
r = alpha*(jnp.sum(U**norm)+jnp.sum(V**norm))
e = e/(self.nb_samples1*self.nb_samples2)
r = r/(self.nb_samples1*self.nb_samples2)
return e + r

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

for epoch in tqdm(range(nb_epoch)):

# U,Vの更新
dEdu = jax.grad(self.E, argnums=0)(self.U, self.V, self.X, alpha, norm) / self.nb_samples1
self.U = self.U - eta * dEdu

dEdv = jax.grad(self.E, argnums=1)(self.U, self.V, self.X, alpha, norm) / self.nb_samples2
self.V = self.V - eta * dEdv

# 学習過程記録用
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_f(self, resolution): #fのメッシュ描画用,resolution:一辺の代表点の数
nb_epoch = self.history['u'].shape[0]
self.history['y'] = np.zeros((nb_epoch,self.nb_samples1,self.nb_samples2, self.ob_dim))
for epoch in tqdm(range(nb_epoch)):
Uzeta = self.create_Uzeta(self.history['u'][epoch],resolution)
Vzeta = self.create_Vzeta(self.history['v'][epoch],resolution)

y = self.ff(Uzeta,Vzeta,epoch)
self.history['y'][epoch] = y

def create_Uzeta(self, U, resolution): #fのメッシュの描画用に潜在空間に代表点zetaを作る.
Uzeta = np.linspace(np.min(U), np.max(U),self.nb_samples1).reshape(-1,1)

return Uzeta

def create_Vzeta(self, V, resolution): # fのメッシュの描画用に潜在空間に代表点zetaを作る.
Vzeta = np.linspace(np.min(V), np.max(V),self.nb_samples2).reshape(-1,1)

return Vzeta


if __name__ == '__main__':
# from Lecture_TUKR.tanaka.animal import load_data
from Lecture_TUKR.tanaka.data_scratch_tanaka import load_kura_tsom
# from Lecture_TUKR.tanaka.data_scratch_tanaka import create_rasen
# from Lecture_TUKR.tanaka.data_scratch_tanaka import create_2d_sin_curve
from visualizer import visualize_history
# from visualizer_animal import visualize_history

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



#入力データ(詳しくはdata.pyを除いてみると良い)
nb_samples1 = 10 #データ数
nb_samples2 = 20 #データ数
# data = load_data(retlabel_animal=True, retlabel_feature=True)
# X = load_iris()
X = load_kura_tsom(nb_samples1,nb_samples2) #鞍型データ ob_dim=3, 真のL=2
# X = create_rasen(nb_samples) #らせん型データ ob_dim=3, 真のL=1
# X = create_2d_sin_curve(nb_samples) #sin型データ ob_dim=2, 真のL=1

# X = data[0]
# animal_label = data[1]
# feature_label = data[2]
X, truez, z1, z2 = load_kura_tsom(nb_samples1, nb_samples2, retz=True)
allZ =[truez,z1,z2]
tukr = TUKR(X, nb_samples1, nb_samples2, latent_dim1, latent_dim2, sigma1, sigma2, prior='random')
tukr.fit(epoch, eta, alpha, norm)
# visualize_history(X, tukr.history['f'], tukr.history['u'],tukr.history['v'], tukr.history['error'], save_gif=False, filename="tmp")

#----------描画部分が実装されたらコメントアウト外す----------
tukr.calc_approximate_f(resolution=10)
# visualize_history(X, tukr.history['y'], tukr.history['u'],tukr.history['v'], tukr.history['error'],animal_label, feature_label, save_gif=False, filename="tmp")
visualize_history(X, tukr.history['y'], tukr.history['u'],tukr.history['v'], tukr.history['error'], save_gif=False, filename="tmp")
Loading