本文整理匯總了Python中result.Result.set方法的典型用法代碼示例。如果您正苦於以下問題:Python Result.set方法的具體用法?Python Result.set怎麽用?Python Result.set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類result.Result
的用法示例。
在下文中一共展示了Result.set方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Optimizer
# 需要導入模塊: from result import Result [as 別名]
# 或者: from result.Result import set [as 別名]
#.........這裏部分代碼省略.........
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),
(self.KEY_TEST_BATCH_ERROR_AVERAGE, batch_test_error_sum / (j + 1)),
)
)
# total test
if is_total_test_enabled:
start = time.clock()
total_error = self._test_error(x_test, y_test)
total_error_time = time.clock() - start
sum_error_all += total_error
self.result.add_all(
(
(self.KEY_TEST_TOTAL_ERROR, total_error),
(self.KEY_TEST_TOTAL_ERROR_TIME, total_error_time),
(self.KEY_TEST_TOTAL_ERROR_AVERAGE, sum_error_all / (n_batch * i + j + 1)),
)
)
if is_params_saved:
for p in self.model.params:
self.params_result.set(p.name, p.get_value())
# output
if is_print_enabled:
print "\n{}th iteration / {}th batch".format(i + 1, j + 1)
for l, a in self.result.results.items():
m_form = "{:<26}: {}s" if "time" in l else "{:<26}: {}"
print m_form.format(l.replace("_", " "), a[-1])
# callback
if on_optimized is not None:
on_optimized()
def _update(self, learning_rate):
grads = T.grad(self.model.cost(True), self.model.params)
updates = [(p, p - learning_rate * g) for p, g in zip(self.model.params, grads)]
return updates