模型训练常用函数

1. 多元素累加器

class Accumulator: #@save
	"""在n个变量上累加"""
	def __init__(self, n):
		self.data = [0.0] * n
	def add(self, *args):
		self.data = [a + float(b) for a, b in zip(self.data, args)]
	def reset(self):
		self.data = [0.0] * len(self.data)
	def __getitem__(self, idx):
		return self.data[idx]

metric = Accumulator(2)
metric.add(1, 1)

2. 准确率计算

def accuracy(y_hat, y): #@save
	"""计算预测正确的数量"""
	if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
		y_hat = y_hat.argmax(axis=1)
	cmp = y_hat.type(y.dtype) == y
	return float(cmp.type(y.dtype).sum())

3. 计算模型在某数据集上的精度(pytorch)

def evaluate_accuracy(net, data_iter): #@save
	"""计算在指定数据集上模型的精度"""
	if isinstance(net, torch.nn.Module):
		net.eval() # 将模型设置为评估模式
	metric = Accumulator(2) # 正确预测数、预测总数
	with torch.no_grad():
		for X, y in data_iter:
			metric.add(accuracy(net(X), y), y.numel())
	return metric[0] / metric[1]

4. 网络参数初始化(pytorch)

def init_weights(m):
	if type(m) == nn.Linear or type(m) == nn.Conv2d:
		nn.init.normal_(m.weight, std=0.01)
		
net.apply(init_weights);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值