Stripping disconnects input layer from graph
@lenscloth 已经在做这个了。
开始于 2023年5月2日。
评估
这个 Issue 还没有评估数据。
描述
Describe the bug
Stripping the pruning layers seems to somehow disconnect the input layer from the graph.
System information
TensorFlow version (installed from source or binary): 2.11 (macos)
TensorFlow Model Optimization version (installed from source or binary): 0.7.4
Python version: 3.10
Describe the expected behavior
Pruning a model during training, stripping the pruning layers, then creating a new model based on a subset of layers (e.g. to remove additional targets used during training) should work, if I didn't miss anything.
Describe the current behavior
It fails, although doing it in the order of pruning it, creating the model and then stripping works.
Code to reproduce the issue
import tempfile
import tensorflow as tf
import numpy as np
from tensorflow import keras
import tensorflow_model_optimization as tfmot
from src.common.path import MODELS_DIR
if __name__ == '__main__':
# Load MNIST dataset
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize the input image so that each pixel value is between 0 and 1.
train_images = train_images / 255.0
test_images = test_images / 255.0
# Define the model architecture.
model = keras.Sequential(
[
keras.layers.InputLayer(input_shape=(28, 28, 1)),
keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(10),
]
)
model = tf.keras.Model(inputs=model.inputs, outputs=model.outputs)
prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude
# Compute end step to finish pruning after 2 epochs.
batch_size = 128
epochs = 1
validation_split = 0.1 # 10% of training set will be used for validation set.
num_images = train_images.shape[0] * (1 - validation_split)
end_step = np.ceil(num_images / batch_size).astype(np.int32) * epochs
print("end step", end_step)
# Define model for pruning.
pruning_params = {
'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(
initial_sparsity=0.05,
final_sparsity=0.95,
begin_step=1,
end_step=end_step,
frequency=422,
)
}
model_for_pruning = prune_low_magnitude(model, **pruning_params)
# `prune_low_magnitude` requires a recompile.
model_for_pruning.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'],
)
model_for_pruning.summary()
logdir = tempfile.mkdtemp()
callbacks = [
tfmot.sparsity.keras.UpdatePruningStep(),
tfmot.sparsity.keras.PruningSummaries(log_dir=logdir),
]
model_for_pruning.fit(
train_images,
train_labels,
batch_size=batch_size,
epochs=epochs,
validation_split=validation_split,
callbacks=callbacks,
)
Given the above setup code, running the following snippet fails:
pruned_model = tfmot.sparsity.keras.strip_pruning(model_for_pruning)
inputs = [pruned_model.get_layer("input_1").input]
outputs = pruned_model.get_layer("dense").output
_new_model = tf.keras.Model(inputs=inputs, outputs=outputs) # ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'") at layer "conv2d". The following previous layers were accessed without issue: []
while the following snippets works
inputs = [model_for_pruning.get_layer("input_1").input]
outputs = model_for_pruning.get_layer("prune_low_magnitude_dense").output
_new_model = tf.keras.Model(inputs=inputs, outputs=outputs)
_new_model = tfmot.sparsity.keras.strip_pruning(_new_model)
pruned_model = tfmot.sparsity.keras.strip_pruning(model_for_pruning)
inputs = [pruned_model.get_layer("conv2d").input] # skipping the input layer
outputs = pruned_model.get_layer("dense").output
_new_model = tf.keras.Model(inputs=inputs, outputs=outputs)
- 主要语言
- Python
- 星标
- 1.6k
- 派生
- 349
- 平均合并
- 3 天 2 小时
- 30 天内合并 PR
- 1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/model-optimization 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
tensorflow/model-optimization#1301 · 1 条评论 ·
-
bug
难度 4/5 3-5 天 新手友好度 48/100
tensorflow/model-optimization#1272 ·
-
bug
难度 3/5 1-2 天 新手友好度 50/100
tensorflow/model-optimization#1270 · 2 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 48/100
tensorflow/model-optimization#1241 ·
-
bug
难度 4/5 3-5 天 新手友好度 35/100
tensorflow/model-optimization#1182 · 1 条评论 ·
查看 tensorflow/model-optimization 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
-
hcocena 未关闭policies-accepted pre-review precheck-passed
难度 1/5 1 小时以内 新手友好度 88/100
Bioconductor/BiocContributions#214 · 5 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
TencentCloud/Octop#1169 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
521xueweihan/HelloGitHub#3778 ·
-
The version checker's trailing attribute region has no control for a less-than inside a quoted value 未关闭area: dashboard area: tests bug perceived difficulty: 2 python
难度 2/5 1-3 小时 新手友好度 84/100
Nitjsefnie-Harness-Commons/daedalus#1105 · 1 条评论 ·