本文整理汇总了Python中solver.Solver.set_metric方法的典型用法代码示例。如果您正苦于以下问题:Python Solver.set_metric方法的具体用法?Python Solver.set_metric怎么用?Python Solver.set_metric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类solver.Solver
的用法示例。
在下文中一共展示了Solver.set_metric方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: finetune
# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import set_metric [as 别名]
def finetune(self, X, batch_size, n_iter, optimizer, l_rate, decay, lr_scheduler=None):
def l2_norm(label, pred):
return np.mean(np.square(label-pred))/2.0
solver = Solver('sgd', momentum=0.9, wd=decay, learning_rate=l_rate, lr_scheduler=lr_scheduler)
solver.set_metric(mx.metric.CustomMetric(l2_norm))
solver.set_monitor(Monitor(1000))
data_iter = mx.io.NDArrayIter({'data': X}, batch_size=batch_size, shuffle=False,
last_batch_handle='roll_over')
logging.info('Fine tuning...')
solver.solve(self.xpu, self.loss, self.args, self.args_grad, data_iter,
0, n_iter, {}, False)
示例2: cluster
# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import set_metric [as 别名]
def cluster(self, X, y=None, update_interval=None):
N = X.shape[0]
if not update_interval:
update_interval = N
batch_size = 256
test_iter = mx.io.NDArrayIter({"data": X}, batch_size=batch_size, shuffle=False, last_batch_handle="pad")
args = {k: mx.nd.array(v.asnumpy(), ctx=self.xpu) for k, v in self.args.items()}
z = model.extract_feature(self.feature, args, None, test_iter, N, self.xpu).values()[0]
kmeans = KMeans(self.num_centers, n_init=20)
kmeans.fit(z)
args["dec_mu"][:] = kmeans.cluster_centers_
solver = Solver("sgd", momentum=0.9, wd=0.0, learning_rate=0.01)
def ce(label, pred):
return np.sum(label * np.log(label / (pred + 0.000001))) / label.shape[0]
solver.set_metric(mx.metric.CustomMetric(ce))
label_buff = np.zeros((X.shape[0], self.num_centers))
train_iter = mx.io.NDArrayIter(
{"data": X}, {"label": label_buff}, batch_size=batch_size, shuffle=False, last_batch_handle="roll_over"
)
self.y_pred = np.zeros((X.shape[0]))
def refresh(i):
if i % update_interval == 0:
z = model.extract_feature(self.feature, args, None, test_iter, N, self.xpu).values()[0]
p = np.zeros((z.shape[0], self.num_centers))
self.dec_op.forward([z, args["dec_mu"].asnumpy()], [p])
y_pred = p.argmax(axis=1)
print np.std(np.bincount(y_pred)), np.bincount(y_pred)
print np.std(np.bincount(y.astype(np.int))), np.bincount(y.astype(np.int))
if y is not None:
print (cluster_acc(y_pred, y)[0])
weight = 1.0 / p.sum(axis=0)
weight *= self.num_centers / weight.sum()
p = (p ** 2) * weight
train_iter.data_list[1][:] = (p.T / p.sum(axis=1)).T
print np.sum(y_pred != self.y_pred), 0.001 * y_pred.shape[0]
if np.sum(y_pred != self.y_pred) < 0.001 * y_pred.shape[0]:
self.y_pred = y_pred
return True
self.y_pred = y_pred
solver.set_iter_start_callback(refresh)
solver.set_monitor(Monitor(50))
solver.solve(self.xpu, self.loss, args, self.args_grad, None, train_iter, 0, 1000000000, {}, False)
self.end_args = args
if y is not None:
return cluster_acc(self.y_pred, y)[0]
else:
return -1
示例3: finetune
# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import set_metric [as 别名]
def finetune(self, X, R, V, lambda_v_rt, lambda_u, lambda_v, dir_save, batch_size, n_iter, optimizer, l_rate, decay, lr_scheduler=None):
def l2_norm(label, pred):
return np.mean(np.square(label-pred))/2.0
solver = Solver(optimizer, momentum=0.9, wd=decay, learning_rate=l_rate, lr_scheduler=lr_scheduler)
solver.set_metric(mx.metric.CustomMetric(l2_norm))
solver.set_monitor(Monitor(1000))
data_iter = mx.io.NDArrayIter({'data': X, 'V': V, 'lambda_v_rt':
lambda_v_rt},
batch_size=batch_size, shuffle=False,
last_batch_handle='pad')
logging.info('Fine tuning...')
# self.loss is the net
U, V, theta, BCD_loss = solver.solve(X, R, V, lambda_v_rt, lambda_u,
lambda_v, dir_save, batch_size, self.xpu, self.loss, self.args, self.args_grad, self.auxs, data_iter,
0, n_iter, {}, False)
return U, V, theta, BCD_loss
示例4: layerwise_pretrain
# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import set_metric [as 别名]
def layerwise_pretrain(self, X, batch_size, n_iter, optimizer, l_rate, decay, lr_scheduler=None):
def l2_norm(label, pred):
return np.mean(np.square(label-pred))/2.0
solver = Solver('sgd', momentum=0.9, wd=decay, learning_rate=l_rate, lr_scheduler=lr_scheduler)
solver.set_metric(mx.metric.CustomMetric(l2_norm))
solver.set_monitor(Monitor(1000))
data_iter = mx.io.NDArrayIter({'data': X}, batch_size=batch_size, shuffle=False,
last_batch_handle='roll_over')
for i in range(self.N):
if i == 0:
data_iter_i = data_iter
else:
X_i = model.extract_feature(self.internals[i-1], self.args,
data_iter, X.shape[0], self.xpu).values()[0]
data_iter_i = mx.io.NDArrayIter({'data': X_i}, batch_size=batch_size,
last_batch_handle='roll_over')
logging.info('Pre-training layer %d...'%i)
solver.solve(self.xpu, self.stacks[i], self.args, self.args_grad, data_iter_i,
0, n_iter, {}, False)