def train(epoch): model.train() loss_queue = [0] * display #存储 correct_num_queue = [0] * display for i, (images, labels) in enumerate(train_loader): # 把训练数据和标签放到cuda images = images.cuda() labels = labels.cuda() # 清除累积梯度 optimizer.zero_grad() #前向传播 outputs = model(images) #计算loss loss = loss_fn(outputs, labels) #存储loss loss_queue.append(loss.data.item()) loss_queue.pop(0) #反向传播,计算出梯度矩阵 loss.backward() if args.sparsity_lambda: for m in model.modules(): if isinstance(m, nn.BatchNorm2d): #将γ的原本梯度矩阵加上由L1稀疏化引入的梯度 m.weight.grad.data.add_(args.sparsity_lambda * torch.sign(m.weight.data)) #根据梯度更新参数 optimizer.step() #当前正确个数 _, prediction = torch.max(outputs.data, 1) current_correct_num = torch.sum(prediction == labels.data) correct_num_queue.append(current_correct_num) correct_num_queue.pop(0) if i != 0 and i % display == 0: print_and_write( current_time() + ' Epoch {}, Iteration {}, lr = {}'.format( epoch, i, optimizer.param_groups[0]['lr']), log_file) #print_and_write(current_time()+' Training loss cur = {:.3f}'.format(loss.data.item()), log_file) print_and_write( current_time() + ' Training loss ave = {:.3f}'.format(mean(loss_queue)), log_file) #print_and_write(current_time()+' Training accu cur = {:.3f}'.format(current_correct_num/batch_size), log_file) print_and_write( current_time() + ' Training accu ave = {:.3f}'.format( sum(correct_num_queue) / (batch_size * display)), log_file)
def update_user(self, id, name, key, table="users"): conditions = {"id": id} user = {"name": name} current_time = common.current_time() user["timestamp"] = current_time user["key"] = key self.update(table=table, values=user, conditions=conditions)
def update_datas(self, id, name, conditions, value, condition_extension={}, value_extension={}): # 出去变化值之外的所有条件值 conditions.update({"id": id, "name": name}) if condition_extension: conditions["condition_extension"] = common.gen_json_str( condition_extension) # 变化值 current_time = common.current_time() uuid = "%s-%s" % (hash_utils.hash( conditions, length=8), hash_utils.hash(current_time, length=8)) values = {"uuid": uuid} values["timestamp"] = current_time values["value"] = value if value_extension: values["value_extension"] = common.gen_json_str(value_extension) self.update(table=self.table_datas, values=values, conditions=conditions) values.update(conditions) self.insert(table=self.table_history, values=values)
def user(): # 获取cookie中的值,已登录用户 id = request.cookies.get('id') name = request.cookies.get('name') form_update_data = UpdateData() if form_update_data.validate_on_submit(): common.print_info("提交表单数据") city = form_update_data.city.data project = form_update_data.project.data value = form_update_data.value.data common.print_info("city: %s, project: %s, value: %s" % (city, project, value)) conditions = {"city": city, "project": project} config.update_datas(id, name, conditions, value) common.print_info("get id:%s, name:%s from cookies" % (id, name)) # return render_template('user.html', id=id, name=name, form=form_update_data) if request.method == 'POST': file = request.files['file'] filename = file.filename common.print_info("上传文件: %s" % filename) try: # file.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(filename))) excel = os.path.join( app.config['UPLOAD_FOLDER'], "%s_%s_%s_%s" % (common.current_time(fmt='%Y%m%d-%H%M%S-%f'), id, name, filename)) file.save(excel) # 检查 ret, info = excel_check(excel, titles_std=titles_std) if ret: project_name, date_power = info flash("%s 上传成功,项目名称:%s,时间力度:%s" % (filename, project_name, date_power)) # 重命名文件 filename_new = "%s_%s_%s_%s" % (date_power, project_name, id, name) excel_new = file_utils.replace_filename(excel, filename_new) file_utils.copy_file(excel, excel_new) # 源文件备份 excel_backup = os.path.join("backups", os.path.basename(excel)) file_utils.move_file(excel, excel_backup) else: flash(info) except Exception as e: flash("ERROR! %s" % e) # return render_template('user.html', form=form_update_data) return render_template('user.html', id=id, name=name, form=form_update_data)
info.get(subsidy_str), info.get(gmv_str), ",".join(tmp)) csv_contents.append(line) for city, info in result.citys.items(): line = "%s,%s,%s,%s,%s" % (city, info.get(battle_zone_str), info.get(needed_date), info.get(subsidy_str), info.get(gmv_str)) values = [] for project_name in result.project_names: project_value = info.get("projects").get(project_name) values.append( "%s,%s" % (project_value.get("power"), project_value.get("subsidy"))) line = "%s,%s" % (line, ",".join(values)) csv_contents.append(line) csv_contents = "\n".join(csv_contents) csv_path = os.path.join( excel_path, "result_%s.csv" % common.current_time(fmt='%Y%m%d_%H%M%S%f')) with open(csv_path, "w") as f: f.write(csv_contents) print("") print("") print("==============================================") print("") print("报表输出到:\n%s" % os.path.abspath(csv_path)) print("") print("==============================================")
# return x # model = torchvision.models.resnet18(num_classes=10) # model.conv1 = nn.Conv2d(3,64,kernel_size=3, stride=1, padding=1) # model._forward_impl = types.MethodType(_forward_impl,model) # 2.mobilenet_v2 # model = torchvision.models.mobilenet_v2(num_classes=200) # 3.vgg19_bn去掉多余全连接 model = torchvision.models.vgg19_bn(num_classes=10) model.avgpool = nn.AdaptiveAvgPool2d((1, 1)) model.classifier = nn.Linear(512, 10) print_and_write( current_time() + ' Epochs:{}, Batch size:{}, Init lr:{}'.format( max_epoch, batch_size, base_lr), log_file) #如果有指定预训练模型 if not args.pretrain_weights == '0': print_and_write( current_time() + ' Loading from {}'.format(args.pretrain_weights), log_file) pretrained_dict = torch.load(args.pretrain_weights) model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } #过滤掉不用的层 pretrained_dict = { k: v