Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

DepthwiseConv2D Layers cannot be clustered or sparsely pruned

未关闭
#1,132 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python, tensorflow

调研方向

首先,使用 TensorFlow Model Optimization 的 cluster_weights 和 sparse pruning API 运行提供的 MNIST 复现。跟踪 DepthwiseConv2D 内核的处理方式,然后验证聚类会生成三个唯一权重,并且 m-by-n pruning 会生成预期的 sparsity 模式。

由索引模型根据 Issue 内容生成。

描述

bug

When attempting to sparsely prune or cluster DepthwiseConv2D Layers it appears that no clustering or sparse pruning actually occurs.

System information

TensorFlow version (installed from source or binary): 2.16.1/2.15.1

TensorFlow Model Optimization version (installed from source or binary): 0.8.0/0.7.5

Python version: 3.9.18

Describe the expected behavior

I should see that the kernel for the Depthwise layer should have 3 unique weights, not 9.

Describe the current behavior

The Depthwise layer has 9 unique weights

The same issue occurs for sparse m by n pruning, the weights are not pruned correctly in an m by n manner

Code to reproduce the issue

import tensorflow as tf
import tensorflow_model_optimization as tfmot
from tensorflow import keras

import numpy as np

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0

model = keras.Sequential([
keras.layers.InputLayer(input_shape=(28, 28)),
keras.layers.Reshape(target_shape=(28, 28, 1)),
keras.layers.DepthwiseConv2D(kernel_size=(3, 3), activation=tf.nn.relu),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(10)
])

model.compile(optimizer='adam',
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

model.fit(
train_images,
train_labels,
validation_split=0.1,
epochs=10
)

cluster_weights = tfmot.clustering.keras.cluster_weights
CentroidInitialization = tfmot.clustering.keras.CentroidInitialization

clustering_params = {
'number_of_clusters': 3,
'cluster_centroids_init': CentroidInitialization.LINEAR
}

clustered_model = cluster_weights(model, **clustering_params)

opt = keras.optimizers.Adam(learning_rate=1e-5)

clustered_model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=opt,
metrics=['accuracy'])
clustered_model.fit(
train_images,
train_labels,
batch_size=500,
epochs=1,
validation_split=0.1)

for layer in model.layers:
for weight in layer.weights:
print(weight.name)
print(len(np.unique(weight)))

主要语言
Python
星标
1.6k
派生
349
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tensorflow/model-optimization 的其他 Issue

查看 tensorflow/model-optimization 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。