実現したいこと
- はじめてのディープラーニング2(著者:我妻幸長)のGANの実装を完全に実行できるものにしたい。
前提
はじめてのディープラーニング2(著者:我妻幸長)のGANの実装を参考にプログラムの修正を図りながら最低限実行できるように修正しましたが、以下のランタイムワーニングに遭遇しました。
ソースコードの修正すべき箇所がありましたら、お手数おかけしますがご指摘よろしくお願い致します。
発生している問題
terminal
1gan_.py:134: RuntimeWarning: invalid value encountered in log 2 return -np.sum(t*np.log(y+eps) + (1-t)*np.log(1-y+eps)) / len(y) 3n_learn: 0 4Error_fake: 0.0 Acc_fake: 0.0 5Error_fake: nan Acc_fake: 0.1875 6n_learn: 1000 7Error_fake: 0.0 Acc_fake: 0.0 8Error_fake: 11.512925464970229 Acc_fake: 0.0 9n_learn: 2000 10Error_fake: 0.0 Acc_fake: 0.0 11Error_fake: 11.512925464970229 Acc_fake: 0.0 12n_learn: 3000 13Error_fake: 0.0 Acc_fake: 0.0 14Error_fake: 11.512925464970229 Acc_fake: 0.0 15n_learn: 4000 16Error_fake: 0.0 Acc_fake: 0.0 17Error_fake: 11.512925464970229 Acc_fake: 0.0 18n_learn: 5000 19Error_fake: 0.0 Acc_fake: 0.0 20Error_fake: 11.512925464970229 Acc_fake: 0.0 21n_learn: 6000 22Error_fake: 0.0 Acc_fake: 0.0 23Error_fake: 11.512925464970229 Acc_fake: 0.0 24n_learn: 7000 25Error_fake: 0.0 Acc_fake: 0.0 26Error_fake: 11.512925464970229 Acc_fake: 0.0 27n_learn: 8000 28Error_fake: 0.0 Acc_fake: 0.0 29Error_fake: 11.512925464970229 Acc_fake: 0.0 30n_learn: 9000 31Error_fake: 0.0 Acc_fake: 0.0 32Error_fake: 11.512925464970229 Acc_fake: 0.0 33n_learn: 10000 34Error_fake: 0.0 Acc_fake: 0.0 35Error_fake: 11.512925464970229 Acc_fake: 0.0
該当のソースコード
python
1import numpy as np 2from sklearn import datasets 3import matplotlib.pyplot as plt 4 5 6img_size = 8 7n_noise=16 8eta = 0.001 9n_learn=10001 10interval = 1000 11batch_size = 32 12 13digits_data = datasets.load_digits() 14x_train = np.asarray(digits_data.data) 15x_train = x_train / 15*2-1 16t_train = digits_data.target 17 18class BaseLayer: 19 def update(self,eta): 20 self.w -= eta * self.grad_w 21 self.b -= eta * self.grad_b 22 23class MiddleLayer(BaseLayer): 24 def __init__(self,n_upper,n): 25 self.w = np.random.randn(n_upper,n) * np.sqrt(2/n_upper) 26 self.b = np.zeros(n) 27 28 def forward(self,x): 29 self.x = x 30 self.u = np.dot(x, self.w) + self.b 31 self.y = np.where(self.u <= 0,0,self.u) 32 33 def backward(self,grad_y): 34 delta = grad_y * np.where(self.u <= 0,0,1) 35 36 self.grad_w = np.dot(self.x.T,delta) 37 self.grad_b = np.sum(delta,axis=0) 38 self.grad_x = np.dot(delta,self.w.T) 39 40class GenOutLayer(BaseLayer): 41 def __init__(self,n_upper,n): 42 self.w = np.random.randn(n_upper,n) / np.sqrt(n_upper) 43 self.b = np.zeros(n) 44 45 def forward(self,x): 46 self.x = x 47 u = np.dot(x,self.w) + self.b 48 self.y = np.tanh(u) 49 50 def backward(self,grad_y): 51 delta = grad_y * (1-self.y**2) 52 53 self.grad_w = np.dot(self.x.T,delta) 54 self.grad_b = np.sum(delta,axis=0) 55 self.grad_x = np.dot(delta,self.w.T) 56 57class DiscOutLayer(BaseLayer): 58 def __init__(self,n_upper,n): 59 self.w = np.random.randn(n_upper,n) / np.sqrt(n_upper) 60 self.b = np.zeros(n) 61 62 def forward(self,x): 63 self.x = x 64 u = np.dot(x,self.w) + self.b 65 self.y = 1/(1+np.exp(-u)) 66 67 def backward(self, t): 68 delta = self.y - t 69 70 self.grad_w = np.dot(self.x.T,delta) 71 self.grad_b = np.sum(delta,axis=0) 72 self.grad_x = np.dot(delta,self.w.T) 73 74gen_layers = [MiddleLayer(n_noise,32), 75 MiddleLayer(32,64), 76 GenOutLayer(64,img_size*img_size)] 77 78disc_layers = [MiddleLayer(img_size*img_size,64), 79 MiddleLayer(64,32), 80 MiddleLayer(32,1)] 81 82def forward_propagation(x,layers): 83 for layer in layers: 84 layer.forward(x) 85 x = layer.y 86 return x 87 88def backpropagation(t,layers): 89 grad_y = t 90 for layer in reversed(layers): 91 layer.backward(grad_y) 92 grad_y = layer.grad_x 93 x = layer.y 94 return grad_y 95 96def update_params(layers): 97 for layer in layers: 98 layer.update(eta) 99 100def get_error(y,t): 101 eps = 1e-5 102 return -np.sum(t*np.log(y+eps) + (1-t)*np.log(1-y+eps)) / len(y) 103 104def get_accuracy(y,t): 105 correct = np.sum(np.where(y<0.5,0,1)==t) 106 return correct / len(y) 107 108def train_model(x,t,prop_layers,update_layers): 109 y = forward_propagation(x,prop_layers) 110 backpropagation(t,prop_layers) 111 update_params(update_layers) 112 return (get_error(y,t),get_accuracy(y,t)) 113 114def generate_images(i): 115 n_rows = 16 116 n_cols = 16 117 noise = np.random.normal(0,1,(n_rows,n_cols,n_noise)) 118 g_imgs = forward_propagation(noise,gen_layers) 119 g_imgs = g_imgs/2 + 0.5 120 121 img_size_spaced = img_size+2 122 matrix_image = np.zeros((img_size_spaced*n_rows, 123 img_size_spaced*n_cols)) 124 125 for r in range(n_rows): 126 for c in range(n_cols): 127 g_img = g_imgs[r,c].reshape(img_size,img_size) 128 129 top = r*img_size_spaced 130 left = c*img_size_spaced 131 matrix_image[top : top+img_size, 132 left : left+img_size] = g_img 133 134 plt.figure(figsize=(8,8)) 135 plt.imshow(matrix_image.tolist(),cmap="Greys_r") 136 plt.tick_params(labelbottom=False,labelleft=False,bottom=False,left=False) 137 plt.show() 138 139batch_half = batch_size // 2 140error_record = np.zeros((n_learn,2)) 141acc_record = np.zeros((n_learn,2)) 142 143for i in range(n_learn): 144 noise = np.random.normal(0,1,(batch_half,n_noise)) 145 imgs_fake = forward_propagation(noise,gen_layers) 146 t = np.zeros((batch_half,1)) 147 error,accuracy = train_model(imgs_fake,t,disc_layers,disc_layers) 148 error_record[i][1] = error 149 acc_record[i][1] = accuracy 150 151 152 rand_ids = np.random.randint(len(x_train),size=batch_half) 153 imgs_real = x_train[rand_ids,:] 154 t = np.ones((batch_half,1)) 155 error, accuracy = train_model(imgs_real,t,disc_layers,disc_layers) 156 error_record[i][1] = error 157 acc_record[i][1] = accuracy 158 159 160 noise = np.random.normal(0,1,(batch_size,n_noise)) 161 t = np.ones((batch_size,1)) 162 train_model(noise,t,gen_layers+disc_layers,gen_layers) 163 164 if i % interval == 0: 165 print("n_learn:",i) 166 print("Error_fake:",error_record[i][0], 167 "Acc_fake:",acc_record[i][0]) 168 print("Error_fake:",error_record[i][1], 169 "Acc_fake:",acc_record[i][1]) 170 generate_images(i) 171
補足情報(FW/ツールのバージョンなど)
Python 3.10.0
numpy 1.21.0
matplotlib 3.7.0
回答1件
あなたの回答
tips
プレビュー