本文整理匯總了Python中result.Result.add_all方法的典型用法代碼示例。如果您正苦於以下問題:Python Result.add_all方法的具體用法?Python Result.add_all怎麽用?Python Result.add_all使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類result.Result
的用法示例。
在下文中一共展示了Result.add_all方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Optimizer
# 需要導入模塊: from result import Result [as 別名]
# 或者: from result.Result import add_all [as 別名]
class Optimizer(object):
KEY_TRAIN_COST = "cost"
KEY_TRAIN_COST_TIME = "train_time"
KEY_TRAIN_BATCH_ERROR = "train_batch_error"
KEY_TRAIN_BATCH_ERROR_AVERAGE = "train_batch_error_average"
KEY_TRAIN_BATCH_ERROR_TIME = "train_batch_error_time"
KEY_TEST_TOTAL_ERROR = "total_error"
KEY_TEST_TOTAL_ERROR_AVERAGE = "total_error_average"
KEY_TEST_TOTAL_ERROR_TIME = "total_test_time"
KEY_TEST_BATCH_ERROR = "test_batch_error"
KEY_TEST_BATCH_ERROR_AVERAGE = "test_batch_error_average"
KEY_TEST_BATCH_ERROR_TIME = "test_batch_time"
def __init__(self, data, model, learning_rate=0.01, l1_rate=0.0, l2_rate=0.0):
assert isinstance(data, Data)
assert isinstance(model, Model)
self.data = data
self.model = model
self.result = Result()
self.params_result = Result(os.path.join(self.result.dir, "params"))
weights = [p for p in self.model.params if "weight" in p.name or "filter" in p.name]
l1 = np.sum([abs(w).sum() for w in weights])
l2 = np.sum([(w ** 2).sum() for w in weights])
self._cost = function(
inputs=(self.model.input_symbol, self.model.answer_symbol),
outputs=self.model.cost(True) + l1_rate * l1 + l2_rate * l2,
updates=self._update(learning_rate),
)
self._train_error = function(
inputs=(self.model.input_symbol, self.model.answer_symbol), outputs=self.model.error(True), updates=[]
)
self._test_error = function(
inputs=(self.model.input_symbol, self.model.answer_symbol), outputs=self.model.error(False), updates=[]
)
def optimize(
self,
n_iter,
n_batch,
is_total_test_enabled=True,
is_params_saved=True,
is_print_enabled=True,
on_optimized=None,
):
x_train, x_test, y_train, y_test = self.data.data()
bs_train = len(x_train) / n_batch
bs_test = len(x_test) / n_batch
# 指定したバッチ數で餘りが出る場合、餘ったデータでも學習・テストするようにバッチ數+1
if bs_train * n_batch < len(x_train) or bs_test * n_batch < len(x_test):
n_batch += 1
sum_error_all = 0.0
for i in xrange(n_iter):
batch_train_error_sum = 0.0
batch_test_error_sum = 0.0
for j in xrange(n_batch):
b_x_train = x_train[j * bs_train : (j + 1) * bs_train]
b_x_test = x_test[j * bs_test : (j + 1) * bs_test]
b_y_train = y_train[j * bs_train : (j + 1) * bs_train]
b_y_test = y_test[j * bs_test : (j + 1) * bs_test]
# train cost
start = time.clock()
cost = self._cost(b_x_train, b_y_train)
train_time = time.clock() - start
# batch train error
start = time.clock()
batch_train_error = self._train_error(b_x_train, b_y_train)
batch_train_time = time.clock() - start
# batch test error
start = time.clock()
batch_test_error = self._test_error(b_x_test, b_y_test)
batch_test_time = time.clock() - start
batch_train_error_sum += batch_train_error
batch_test_error_sum += batch_test_error
# 結果の保存
self.result.add_all(
(
(self.KEY_TRAIN_COST_TIME, train_time),
(self.KEY_TRAIN_BATCH_ERROR_TIME, batch_train_time),
(self.KEY_TEST_BATCH_ERROR_TIME, batch_test_time),
(self.KEY_TRAIN_COST, cost),
(self.KEY_TRAIN_BATCH_ERROR, batch_train_error),
(self.KEY_TRAIN_BATCH_ERROR_AVERAGE, batch_train_error_sum / (j + 1)),
(self.KEY_TEST_BATCH_ERROR, batch_test_error),
#.........這裏部分代碼省略.........