def fit(self, x, t, batch_size, epochs, validation=None): #レイヤの行列を計算する y = np.zeros((batch_size, x.shape[1])) for layer in self.sequential.values(): y = layer.fit(y) # バリデーションが最初からセットされているとき if validation != None: x_val = validation[0] t_val = validation[1] else: x_val, x = __sorting__(x, 100) t_val, t = __sorting__(t, 100) loop = int(x.shape[0] / batch_size) # 繰り返し回数 # メインルーチン for i in range(epochs): loss_sum = 0 for j in range(loop): # 行数からbatch_sizeだけランダムに値を抽出 replace(重複) batch_mask = np.random.choice(x.shape[0], batch_size, replace=False) x_batch = x[batch_mask] # 全データからbatch_size分データを抽出 t_batch = t[batch_mask] """ if (validation != None): x_val = x_val[batch_mask] t_val = t_val[batch_mask] """ loss = self.gradient(x_batch, t_batch) # 誤差の計算 loss_sum += (loss / batch_size) loss_ave = loss_sum / loop # 誤差の平均値の計算 self.history['loss_ave'].append(loss_ave) # 誤差の保存 #--------------------------- # validation誤差の計算 #--------------------------- if (validation != None): val_loss = self.loss(x_val, t_val) val_loss_ave = val_loss / x_val[0].shape[0] self.history['val_loss'].append(val_loss_ave) #--------------------------- # 正解率の計算 #--------------------------- train_acc = self.accuracy(x_batch, t_batch, self.metrics_func['train']) acc = [] for k in range(len(train_acc)): ave = np.sum(train_acc[k]) / train_acc[k].shape[0] # 全行での平均をとる acc.append(ave) self.metrics_log['train'].append(acc) """ train_acc = self.accuracy(x, t, func='r2_score') # 要素ごとの評価を行う train_acc_ave = np.sum(train_acc) / train_acc.shape[0] # 全行での平均をとる self.history['train_acc'].append(train_acc_ave) # 平均の正解率を記録する """ #print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f' % (i+1, self.history['loss_ave'][i], self.history['val_loss'][i], self.metrics_log['train'][i])) print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f' % (i + 1, self.history['loss_ave'][i], self.history['val_loss'][i], np.min(self.metrics_log['train'][i]))) #print('学習%d回目 --loss:%f, --val=%f' % (i+1, self.history['loss_ave'][i], self.history['val_loss'][i])) print('loss=%f, val=%f' % (self.history['loss_ave'][epochs - 1], self.history['val_loss'][epochs - 1])) return self.history
def fit(self, x, t, batch_size, epochs, validation=None, callbacks=None): # コールバック関数 self.callbacks = callbacks #------------------------------- # Validation #------------------------------- if validation != None: # バリデーションが最初からセットされているとき x_val = validation[0] t_val = validation[1] else: x = __shuffle__(x) t = __shuffle__(t) #------------------ # Sorting #------------------ # ホールドアウト法を採用 # 教師用データと検証用データを6:4で分割 thresh = int((x.shape[0] * 0.4)) x_val, x = __sorting__(x, thresh) t_val, t = __sorting__(t, thresh) loop = int(x.shape[0] / batch_size) # 繰り返し回数 #--------------------------- # メインルーチン #--------------------------- for epoch in range(epochs): loss_sum = 0 for j in range(loop): # 行数からbatch_sizeだけランダムに値を抽出 replace(重複) batch_mask = np.random.choice(x.shape[0], batch_size, replace=False) x_batch = x[batch_mask] # 全データからbatch_size分データを抽出 t_batch = t[batch_mask] grads = self.gradient(x_batch, t_batch) self.func['optimizer'].update(self.params, grads) #loss = self.gradient(x_batch, t_batch) # 誤差の計算 loss_sum += (self.history['loss'] / batch_size) loss_ave = loss_sum / loop # 誤差の平均値の計算 self.history['loss_ave'].append(loss_ave) # 誤差の保存 #--------------------------- # validation誤差の計算 #--------------------------- val_loss = self.loss(x_val, t_val) val_loss_ave = val_loss / x_val[0].shape[0] self.history['val_loss'].append(val_loss_ave) #--------------------------- # 正解率の計算 #--------------------------- # printで値を表示するためのリスト prt_train_acc = [] prt_val_acc = [] logs = {} for key, List in self.logs.items(): #metric = [x for x in self.metrics_func.keys() if x in key][0] metric = [x for x in self.func.keys() if x in key][0] if 'train' in key: #acc_train = self.accuracy(x_batch, t_batch, self.metrics_func[metric]) acc_train = self.accuracy(x_batch, t_batch, self.func[metric]) List.append(acc_train) prt_train_acc.append(acc_train) logs[key] = acc_train if 'val' in key: #acc_val = self.accuracy(x_val, t_val, self.metrics_func[metric]) acc_val = self.accuracy(x_val, t_val, self.func[metric]) List.append(acc_val) prt_val_acc.append(acc_val) logs[key] = acc_val #--------------------------- # one_epoch_endコールバック #--------------------------- if self.callbacks != None: for call in callbacks: call.one_epoch_end(logs) #print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f' % (i+1, self.history['loss_ave'][i], self.history['val_loss'][i], self.metrics_log['train'][i])) print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f, --val_acc=%f' % \ (epoch+1, self.history['loss_ave'][epoch], self.history['val_loss'][epoch], np.min(prt_train_acc), np.min(prt_val_acc))) nb_epoch = epochs plt.plot(range(nb_epoch), self.history['loss_ave'], marker='.', label='loss') plt.plot(range(nb_epoch), self.history['val_loss'], marker='.', label='val_loss') plt.legend(loc='best', fontsize=10) plt.grid() plt.xlabel('epoch') plt.ylabel('loss') # 再描画する plt.pause(0.001) plt.show() print('loss=%f, val=%f' % (self.history['loss_ave'][epochs - 1], self.history['val_loss'][epochs - 1])) return self.history
def nn(x, t, batch_size, epochs, feature=None, validation=None): """ 簡単なニューラルネットワークのモデルを作成する関数 Parameters ---------- x : ndarray 学習用データ t : ndarray 教師データ batch_size : int バッチサイズ eopchs : int エポック数 feature : int Feature Scalingの選択 """ #------------------------------- # DataFeature #------------------------------- if feature != None: x = Datafeature(x, feature) t = Datafeature(t, feature) #------------------------------- # Validation #------------------------------- if validation != None: # バリデーションが最初からセットされているとき x_val = validation[0] t_val = validation[1] else: x = __shuffle__(x) t = __shuffle__(t) x_val, x = __sorting__(x, 100) t_val, t = __sorting__(t, 100) # 学習曲線を可視化するコールバックを用意する higher_better_metrics = ['r2'] visualize_cb = LearningVisualizationCallback(higher_better_metrics) callbacks = [ visualize_cb, ] model = Sequential() model.add(Input(input_shape=x.shape[1])) model.add(Dense(50, activation='relu', weight_initializer='relu')) model.add(Dense(50, activation='relu', weight_initializer='relu')) #model.add(Dense(50, activation='sigmoid', weight_initializer='sigmoid')) #model.add(Dense(50, activation='sigmoid', weight_initializer='sigmoid')) #model.add(Dense(t.shape[1], activation='softmax')) #model.compile(loss='cross_entropy_error') model.add(Dense(t.shape[1], activation = 'liner')) model.compile(loss='mean_squared_error', metrics = ['r2', 'rsme']) #history=model.fit(x, t, batch_size=batch_size, epochs=epochs, validation=validation) history = model.fit(x, t, batch_size=batch_size, epochs=epochs, validation=(x_val, t_val), callbacks=callbacks) # lossグラフ loss = history['loss_ave'] val_loss = history['val_loss'] nb_epoch = len(loss) plt.plot(range(nb_epoch), loss, marker = '.', label = 'loss') plt.plot(range(nb_epoch), val_loss, marker='.', label='val_loss') plt.legend(loc = 'best', fontsize = 10) plt.grid() plt.xlabel('epoch') plt.ylabel('loss') plt.show()
def fit(self, x, t, batch_size, epochs, validation=None, callbacks=None): # コールバック関数 """ if callbacks != None: one_epoch_end = [] for call in callbacks: if ('one_epoch_end' in dir(call)): one_epoch_end.append(call.one_epoch_end) """ #レイヤの行列を計算する y = np.zeros((batch_size, x.shape[1])) for layer in self.sequential.values(): y = layer.fit(y) # バリデーションが最初からセットされているとき if validation != None: x_val = validation[0] t_val = validation[1] else: x = __shuffle__(x) t = __shuffle__(t) x_val, x = __sorting__(x, 100) t_val, t = __sorting__(t, 100) loop = int(x.shape[0] / batch_size) # 繰り返し回数 # メインルーチン for epoch in range(epochs): loss_sum = 0 for j in range(loop): # 行数からbatch_sizeだけランダムに値を抽出 replace(重複) batch_mask = np.random.choice(x.shape[0], batch_size, replace=False) x_batch = x[batch_mask] # 全データからbatch_size分データを抽出 t_batch = t[batch_mask] loss = self.gradient(x_batch, t_batch) # 誤差の計算 loss_sum += (loss / batch_size) loss_ave = loss_sum / loop # 誤差の平均値の計算 self.history['loss_ave'].append(loss_ave) # 誤差の保存 #--------------------------- # validation誤差の計算 #--------------------------- val_loss = self.loss(x_val, t_val) val_loss_ave = val_loss / x_val[0].shape[0] self.history['val_loss'].append(val_loss_ave) #--------------------------- # 正解率の計算 #--------------------------- logs = {} # printで値を表示するためのリスト prt_train_acc = [] prt_val_acc = [] for key, List in self.logs.items(): metric = [x for x in self.metrics_func.keys() if x in key][0] if 'train' in key: acc = self.accuracy(x_batch, t_batch, self.metrics_func[metric]) List.append(acc) prt_train_acc.append(acc) if 'val' in key: acc = self.accuracy(x_val, t_val, self.metrics_func[metric]) List.append(acc) prt_val_acc.append(acc) logs[key] = acc #--------------------------- # one_epoch_endコールバック #--------------------------- if callbacks != None: for call in callbacks: call.one_epoch_end(epoch, logs) #print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f' % (i+1, self.history['loss_ave'][i], self.history['val_loss'][i], self.metrics_log['train'][i])) print('学習%d回目 --loss:%f, --val=%f, --train_acc=%f, --val_acc=%f' % \ (epoch+1, self.history['loss_ave'][epoch], self.history['val_loss'][epoch], np.min(prt_train_acc), np.min(prt_val_acc))) print('loss=%f, val=%f' % (self.history['loss_ave'][epochs - 1], self.history['val_loss'][epochs - 1])) return self.history