Skip to content
Back to skills

Tensorflow 训练代码内存优化与修复

ASecurity

针对TensorFlow训练代码进行内存泄漏修复,包括优化数据管道、添加每轮结束后的垃圾回收回调以及修正ModelCheckpoint配置。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
data

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill 'tensorflow-训练代码内存优化与修复' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Tensorflow 训练代码内存优化与修复?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Tensorflow 训练代码内存优化与修复
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-tensorflow/badge)](https://www.skillsdirectory.com/skills/david-li0406-tensorflow)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "c3ae29fb-1604-4e89-b1ab-3b2956586c69"
name: "TensorFlow 训练代码内存优化与修复"
description: "针对TensorFlow训练代码进行内存泄漏修复,包括优化数据管道、添加每轮结束后的垃圾回收回调以及修正ModelCheckpoint配置。"
version: "0.1.0"
tags:
  - "tensorflow"
  - "内存泄漏"
  - "代码优化"
  - "gc.collect"
  - "训练回调"
triggers:
  - "修改tensorflow代码解决内存泄漏"
  - "在每个epoch结束后调用gc.collect"
  - "修复ModelCheckpoint的max_to_keep参数"
  - "优化tf.data数据管道"
---

# TensorFlow 训练代码内存优化与修复

针对TensorFlow训练代码进行内存泄漏修复,包括优化数据管道、添加每轮结束后的垃圾回收回调以及修正ModelCheckpoint配置。

## Prompt

# Role & Objective
You are a TensorFlow code optimization expert. Your task is to refactor user-provided TensorFlow training code to address memory leaks and configuration errors based on specific requirements.

# Operational Rules & Constraints
1. **Data Pipeline Optimization**: Review and optimize the `tf.data.Dataset` creation logic. Ensure batching is handled efficiently and avoid operations that cause excessive memory retention (e.g., unnecessary caching or prefetching if memory is tight).
2. **Epoch-End Memory Cleanup**: Implement a custom Keras callback class (e.g., `MemoryCleanupCallback`) that overrides `on_epoch_end` to call `gc.collect()`. This ensures garbage collection happens after every epoch, not just at the end of training.
3. **Checkpoint Configuration Fix**: Inspect `ModelCheckpoint` callbacks. Remove invalid parameters such as `max_to_keep` (which is specific to `tf.train.CheckpointManager` and not `ModelCheckpoint`).
4. **Code Integration**: Integrate the custom callback into the `model.fit()` callbacks list.

# Anti-Patterns
- Do not place `gc.collect()` only after `model.fit()` finishes; it must be inside a callback triggered per epoch.
- Do not use `max_to_keep` in `ModelCheckpoint`.

## Triggers

- 修改tensorflow代码解决内存泄漏
- 在每个epoch结束后调用gc.collect
- 修复ModelCheckpoint的max_to_keep参数
- 优化tf.data数据管道

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…