Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/angeloyeo/gongdols
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloyeo committed Oct 19, 2020
2 parents 8f425ab + 4dd95da commit 0d30cab
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 1 deletion.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified 머신러닝/restricted_boltzmann_machine/generated_testdata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion 머신러닝/restricted_boltzmann_machine/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,29 @@ def free_energy(self,v):
transforms.ToTensor()
]))

sample_data = testset.data[:32,:].view(-1, 784) # 총 32개 데이터를 받아옴.
sample_data = testset.test_data[:32,:].view(-1, 784) # 총 32개 데이터를 받아옴.
sample_data = sample_data.type(torch.FloatTensor)/255.

v, v1 = rbm(sample_data)
show_adn_save("real_testdata",make_grid(v.view(32,1,28,28).data))
show_adn_save("generated_testdata",make_grid(v1.view(32,1,28,28).data))


#%% 적절한 hidden layer의 sample을 하나 가지고 visible layer의 sample을 만들어보자.

random_idx = np.random.permutation(testset.test_data.shape[0])
random_image = Variable(testset.test_data[random_idx[0], :, :].view(-1, 784))/255.
sample_data = random_image.bernoulli()

pr_h, h = rbm.v_to_h(sample_data)

new_data = torch.empty(100, 1, 28, 28)
for i in range(100):
pr_v, v = rbm.h_to_v(h)
new_data[i, :, :, :] = v.view(28, 28)
# plt.imshow(v.view(28, 28).detach().numpy(), cmap = 'gray')


plt.imshow(
np.transpose(make_grid(new_data, nrow = 10).detach().numpy(),(1,2,0))
)
71 changes: 71 additions & 0 deletions 선형대수/NMF/NMF_on_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from time import time
import logging
import pylab as pl

from sklearn.decomposition import PCA, NMF
from sklearn import datasets


# Display progress logs on stdout
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')

digits = datasets.load_digits()

# reshape the data using the traditional (n_samples, n_features) shape
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
n_features = X.shape[1]

n_components = 16

######################################################################
# Compute a PCA (eigendigits) on the digit dataset

print("Extracting the top %d eigendigits from %d images" % (
n_components, X.shape[0]))
t0 = time()
pca = PCA(n_components=n_components, whiten=True).fit(X)
print("done in %0.3fs" % (time() - t0))

eigendigits = pca.components_

######################################################################
# Compute a NMF on the digit dataset

print("Extracting %d non-negative features from %d images" % (
n_components, X.shape[0]))
t0 = time()
nmf = NMF(n_components=n_components, init='nndsvd', tol=1e-2).fit(X)
print("done in %0.3fs" % (time() - t0))

W = nmf.fit_transform(X)
H = nmf.components_

nmfdigits = nmf.components_

######################################################################
# Plot the results

n_row, n_col = 4, 4

f1 = pl.figure(figsize=(1. * n_col, 1.13 * n_row))
f1.text(.5, .95, 'Principal components', horizontalalignment='center')
for i in range(n_row * n_col):
pl.subplot(n_row, n_col, i + 1)
pl.imshow(eigendigits[i].reshape((8, 8)), cmap=pl.cm.gray,
interpolation='nearest')
pl.xticks(())
pl.yticks(())
pl.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.)

f2 = pl.figure(figsize=(1. * n_col, 1.13 * n_row))
f2.text(.5, .95, 'Non-negative components', horizontalalignment='center')
for i in range(n_row * n_col):
pl.subplot(n_row, n_col, i + 1)
pl.imshow(nmfdigits[i].reshape((8, 8)), cmap=pl.cm.gray,
interpolation='nearest')
pl.xticks(())
pl.yticks(())
pl.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.)
pl.show()
Binary file added 선형대수/NMF/YaleB_32x32.mat
Binary file not shown.
134 changes: 134 additions & 0 deletions 선형대수/NMF/html/no01_NMF_on_face_dataset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>no01_NMF_on_face_dataset</title><meta name="generator" content="MATLAB 9.2"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2020-10-15"><meta name="DC.source" content="no01_NMF_on_face_dataset.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}

html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }

h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }

a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }

p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }

ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }

.content { font-size:1.2em; line-height:140%; padding: 20px; }

pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }

@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }

span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }

.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }

table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }





</style></head><body><div class="content"><h2>Contents</h2><div><ul><li><a href="#2">NMF &#49688;&#54665;&#54616;&#44592;</a></li><li><a href="#3">PCA &#49688;&#54665;&#54616;&#44592;</a></li></ul></div><pre class="codeinput">clear; close <span class="string">all</span>; clc;

load(<span class="string">'YaleB_32x32.mat'</span>); <span class="comment">% gnd&#45716; &#49324;&#46988; &#48264;&#54840;&#51064;&#46319;.</span>
<span class="comment">% &#52636;&#52376;: http://www.cad.zju.edu.cn/home/dengcai/Data/FaceData.html</span>
<span class="comment">% &#49324;&#50857;&#54620; &#45936;&#51060;&#53552;&#49483;&#51032; &#51060;&#47492;&#51008; Extended Yale Face Database B&#51076;.</span>

figure(<span class="string">'position'</span>,[556, 237, 947, 699]);
<span class="keyword">for</span> i= 1:25
subplot(5,5,i)
imagesc(reshape(fea(i,:), 32, 32)); colormap(<span class="string">'gray'</span>)
<span class="keyword">end</span>
</pre><img vspace="5" hspace="5" src="no01_NMF_on_face_dataset_01.png" alt=""> <h2 id="2">NMF &#49688;&#54665;&#54616;&#44592;</h2><pre class="codeinput">n_features = 25;
[W, H] = nnmf(fea, n_features);

figure(<span class="string">'position'</span>,[556, 237, 947, 699]);
<span class="keyword">for</span> i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(H(i_features,:), 32, 32)); colormap(<span class="string">'gray'</span>);
<span class="keyword">end</span>

figure; imagesc(reshape(randn(1, 25) * H, 32, 32)); colormap(<span class="string">'gray'</span>)
</pre><img vspace="5" hspace="5" src="no01_NMF_on_face_dataset_02.png" alt=""> <img vspace="5" hspace="5" src="no01_NMF_on_face_dataset_03.png" alt=""> <h2 id="3">PCA &#49688;&#54665;&#54616;&#44592;</h2><pre class="codeinput">[coeff, score, latent] = pca(fea);

figure(<span class="string">'position'</span>,[556, 237, 947, 699]);
<span class="keyword">for</span> i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(coeff(:, i_features), 32, 32)); colormap(<span class="string">'gray'</span>);
<span class="keyword">end</span>
</pre><img vspace="5" hspace="5" src="no01_NMF_on_face_dataset_04.png" alt=""> <p class="footer"><br><a href="http://www.mathworks.com/products/matlab/">Published with MATLAB&reg; R2017a</a><br></p></div><!--
##### SOURCE BEGIN #####
clear; close all; clc;
load('YaleB_32x32.mat'); % gnd는 사람 번호인듯.
% 출처: http://www.cad.zju.edu.cn/home/dengcai/Data/FaceData.html
% 사용한 데이터셋의 이름은 Extended Yale Face Database B임.
figure('position',[556, 237, 947, 699]);
for i= 1:25
subplot(5,5,i)
imagesc(reshape(fea(i,:), 32, 32)); colormap('gray')
end
%% NMF 수행하기
n_features = 25;
[W, H] = nnmf(fea, n_features);
figure('position',[556, 237, 947, 699]);
for i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(H(i_features,:), 32, 32)); colormap('gray');
end
figure; imagesc(reshape(randn(1, 25) * H, 32, 32)); colormap('gray')
%% PCA 수행하기
[coeff, score, latent] = pca(fea);
figure('position',[556, 237, 947, 699]);
for i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(coeff(:, i_features), 32, 32)); colormap('gray');
end
##### SOURCE END #####
--></body></html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 선형대수/NMF/lena_std.tif
Binary file not shown.
33 changes: 33 additions & 0 deletions 선형대수/NMF/no01_NMF_on_face_dataset.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
clear; close all; clc;

load('YaleB_32x32.mat'); % gnd는 사람 번호인듯.
% 출처: http://www.cad.zju.edu.cn/home/dengcai/Data/FaceData.html
% 사용한 데이터셋의 이름은 Extended Yale Face Database B임.

figure('position',[556, 237, 947, 699]);
for i= 1:25
subplot(5,5,i)
imagesc(reshape(fea(i,:), 32, 32)); colormap('gray')
end

%% NMF 수행하기
n_features = 25;
[W, H] = nnmf(fea, n_features);

figure('position',[556, 237, 947, 699]);
for i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(H(i_features,:), 32, 32)); colormap('gray');
end

% figure; imagesc(reshape(randn(1, 25) * H, 32, 32)); colormap('gray')

%% PCA 수행하기

[coeff, score, latent] = pca(fea);

figure('position',[556, 237, 947, 699]);
for i_features = 1:n_features
subplot(5,5,i_features)
imagesc(reshape(coeff(:, i_features), 32, 32)); colormap('gray');
end

0 comments on commit 0d30cab

Please sign in to comment.