質問編集履歴
1
コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,4 +3,108 @@
|
|
3
3
|
あるモデルを作って学習させた後、そのモデルの一部を変更して再度学習させたい時に、どのようなプログラムを書けば最初に学習させた時の重みを使いながら再度学習することができるでしょうか。
|
4
4
|
現在、CVAEのプログラムを作ろうとしているところで、止まってしまっています。
|
5
5
|
プログラムの例などがあると、幸いです。
|
6
|
-
よろしくお願いします。
|
6
|
+
よろしくお願いします。
|
7
|
+
|
8
|
+
```python
|
9
|
+
|
10
|
+
from __future__ import print_function
|
11
|
+
|
12
|
+
import numpy as np
|
13
|
+
import matplotlib.pyplot as plt
|
14
|
+
from scipy.stats import norm
|
15
|
+
import time
|
16
|
+
from collections import Counter
|
17
|
+
|
18
|
+
from keras.layers import Input, Dense, Lambda, Concatenate
|
19
|
+
from keras.models import Model
|
20
|
+
from keras import backend as K
|
21
|
+
from keras import metrics
|
22
|
+
from keras.datasets import mnist
|
23
|
+
from keras import utils
|
24
|
+
|
25
|
+
batch_size = 100
|
26
|
+
original_dim = 784
|
27
|
+
latent_dim = 10
|
28
|
+
intermediate_dim = 256
|
29
|
+
epochs = 25
|
30
|
+
cat_dim = 10
|
31
|
+
epsilon_std = 1.0
|
32
|
+
|
33
|
+
x = Input(shape=(original_dim,))
|
34
|
+
h = Dense(intermediate_dim, activation='relu')(x)
|
35
|
+
z_mean = Dense(latent_dim)(h)
|
36
|
+
z_log_var = Dense(latent_dim)(h)
|
37
|
+
|
38
|
+
def sampling(args):
|
39
|
+
z_mean, z_log_var = args
|
40
|
+
epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0.,
|
41
|
+
stddev=epsilon_std)
|
42
|
+
return z_mean + K.exp(z_log_var / 2) * epsilon
|
43
|
+
|
44
|
+
def vae_loss(x, x_decoded_mean):
|
45
|
+
x = K.flatten(x)
|
46
|
+
x_decoded_mean = K.flatten(x_decoded_mean)
|
47
|
+
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
|
48
|
+
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
|
49
|
+
return K.mean(xent_loss + kl_loss)
|
50
|
+
|
51
|
+
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
|
52
|
+
|
53
|
+
# ラベルありデータのラベルを入力
|
54
|
+
ly = Input(shape=(cat_dim,))
|
55
|
+
merge = Concatenate()([z, ly])
|
56
|
+
|
57
|
+
decoder_h = Dense(intermediate_dim, activation='relu')
|
58
|
+
decoder_mean = Dense(original_dim, activation='sigmoid')
|
59
|
+
h_decoded = decoder_h(merge)
|
60
|
+
x_decoded_mean = decoder_mean(h_decoded)
|
61
|
+
|
62
|
+
# ラベルありの時のモデル
|
63
|
+
labeled_M2 = Model([x,ly], x_decoded_mean)
|
64
|
+
labeled_M2.compile(optimizer='rmsprop', loss=vae_loss)
|
65
|
+
|
66
|
+
|
67
|
+
# データ整形
|
68
|
+
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
69
|
+
|
70
|
+
x_train = x_train.astype('float32') / 255.
|
71
|
+
x_test = x_test.astype('float32') / 255.
|
72
|
+
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
|
73
|
+
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
|
74
|
+
|
75
|
+
|
76
|
+
# トレーニングデータを100個選択
|
77
|
+
|
78
|
+
random_Num = np.random.randint(0,10000)
|
79
|
+
np.random.seed(random_Num)
|
80
|
+
np.random.shuffle(x_test)
|
81
|
+
np.random.seed(random_Num)
|
82
|
+
np.random.shuffle(y_test)
|
83
|
+
x_realtest = x_test[100:] # テストデータ
|
84
|
+
y_realtest = y_test[100:] # テストデータのラベル
|
85
|
+
x_test = np.delete(x_test, range(100,10000), axis=0)
|
86
|
+
y_test = np.delete(y_test, range(100,10000), axis=0)
|
87
|
+
|
88
|
+
# 教師なしデータ,教師ありデータ,テスト用データのラベルを,それぞれone-hot表現にする
|
89
|
+
y_train_cat = utils.to_categorical(y_train)
|
90
|
+
y_test_cat = utils.to_categorical(y_test)
|
91
|
+
y_realtest_cat = utils.to_categorical(y_realtest)
|
92
|
+
|
93
|
+
labeled_M2.fit([x_train, y_train_cat],
|
94
|
+
shuffle=True,
|
95
|
+
epochs=epochs,
|
96
|
+
batch_size=batch_size)
|
97
|
+
|
98
|
+
|
99
|
+
# ラベルなしの時のモデル
|
100
|
+
uy = Dense(intermediate_dim, activation='relu')(x)
|
101
|
+
merge = Concatenate()([z, uy])
|
102
|
+
h_decoded = decoder_h(merge)
|
103
|
+
x_decoded_mean = decoder_mean(h_decoded)
|
104
|
+
|
105
|
+
unlabeled_M2 = Model([x,uy],x_decoded_mean)
|
106
|
+
unlabeled_M2.compile(optimizer='rmsprop', loss=vae_loss)
|
107
|
+
|
108
|
+
labeled_M2.summary()
|
109
|
+
unlabeled_M2.summary()
|
110
|
+
```
|