An error in the official pruning  guide
import tensorflow as tf
import tempfile
from tensorflow import keras

# 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)),
    keras.layers.Reshape(target_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)
])

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

# Train the model
model.fit(train_images, train_labels, epochs=4, validation_split=0.1)

# Evaluate baseline test accuracy and save the model for later usage
_, baseline_model_accuracy = model.evaluate(test_images, test_labels, verbose=0)
print('Baseline test accuracy:', baseline_model_accuracy)

# Define the pruning parameters
pruning_params = {
    'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.50,
                                                              final_sparsity=0.80,
                                                              begin_step=0,
                                                              end_step=100)
}

# Apply pruning to the model
model_for_pruning = tfmot.sparsity.keras.prune_low_magnitude(model, **pruning_params)

# Compile the pruned model
model_for_pruning.compile(optimizer='adam',
                          loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                          metrics=['accuracy'])

# Fine-tune the pruned model
model_for_pruning.fit(train_images, train_labels, epochs=2, validation_split=0.1)

# Evaluate pruned model accuracy
_, pruned_model_accuracy = model_for_pruning.evaluate(test_images, test_labels, verbose=0)
print('Pruned model test accuracy:', pruned_model_accuracy)

# Save the pruned model
_, keras_file = tempfile.mkstemp('.h5')
tf.keras.models.save_model(model_for_pruning, keras_file, include_optimizer=False)
print(f"Model saved at {keras_file}")
 `import tensorflow as tf
import tempfile
from tensorflow import keras

# 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)),
    keras.layers.Reshape(target_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)
])

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

# Train the model
model.fit(train_images, train_labels, epochs=4, validation_split=0.1)

# Evaluate baseline test accuracy and save the model for later usage
_, baseline_model_accuracy = model.evaluate(test_images, test_labels, verbose=0)
print('Baseline test accuracy:', baseline_model_accuracy)

# Define the pruning parameters
pruning_params = {
    'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.50,
                                                              final_sparsity=0.80,
                                                              begin_step=0,
                                                              end_step=100)
}

# Apply pruning to the model
model_for_pruning = tfmot.sparsity.keras.prune_low_magnitude(model, **pruning_params)

# Compile the pruned model
model_for_pruning.compile(optimizer='adam',
                          loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                          metrics=['accuracy'])

# Fine-tune the pruned model
model_for_pruning.fit(train_images, train_labels, epochs=2, validation_split=0.1)

# Evaluate pruned model accuracy
_, pruned_model_accuracy = model_for_pruning.evaluate(test_images, test_labels, verbose=0)
print('Pruned model test accuracy:', pruned_model_accuracy)

# Save the pruned model
_, keras_file = tempfile.mkstemp('.h5')
tf.keras.models.save_model(model_for_pruning, keras_file, include_optimizer=False)
print(f"Model saved at {keras_file}")
` const tf = ***@***.***/tfjs-node');

// Load MNIST dataset
const mnist = tf.data.mnist;
const { trainImages, trainLabels, testImages, testLabels } = mnist.getData();

// Normalize the input image so that each pixel value is between 0 and 1
const normalizedTrainImages = trainImages.div(255);
const normalizedTestImages = testImages.div(255);

// Define the model architecture
const model = tf.sequential();
model.add(tf.layers.inputLayer({ inputShape: [28, 28] }));
model.add(tf.layers.reshape({ targetShape: [28, 28, 1] }));
model.add(tf.layers.conv2d({ filters: 12, kernelSize: [3, 3], activation: 'relu' }));
model.add(tf.layers.maxPooling2d({ poolSize: [2, 2] }));
model.add(tf.layers.flatten());
model.add(tf.layers.dense({ units: 10 }));

// Compile the model
model.compile({
  optimizer: 'adam',
  loss: 'sparseCategoricalCrossentropy',
  metrics: ['accuracy'],
});

// Train the model
await model.fit(normalizedTrainImages, trainLabels, { epochs: 4, validationSplit: 0.1 });

// Evaluate baseline test accuracy
const baselineModelAccuracy = await model.evaluate(normalizedTestImages, testLabels);
console.log('Baseline test accuracy:', baselineModelAccuracy[1]);

// Define the pruning parameters
const pruningParams = {
  pruningSchedule: tfmot.sparsity.polynomialDecay({
    initialSparsity: 0.50,
    finalSparsity: 0.80,
    beginStep: 0,
    endStep: 100,
  }),
};

// Apply pruning to the model
const prunedModel = tfmot.sparsity.pruneLowMagnitude(model, pruningParams);

// Compile the pruned model
prunedModel.compile({
  optimizer: 'adam',
  loss: 'sparseCategoricalCrossentropy',
  metrics: ['accuracy'],
});

// Fine-tune the pruned model
await prunedModel.fit(normalizedTrainImages, trainLabels, { epochs: 2, validationSplit: 0.1 });

// Evaluate pruned model accuracy
const prunedModelAccuracy = await prunedModel.evaluate(normalizedTestImages, testLabels);
console.log('Pruned model test accuracy:', prunedModelAccuracy[1]);

// Save the pruned model
await prunedModel.save('file://path/to/your/model');
console.log('Pruned model saved.');

REGARDS,
KRISHNA MISHRA
JANAKPUR-2,NEPAL
E-MAIL:- ***@***.***
________________________________
From: Santabot123 ***@***.***>
Sent: Friday, February 9, 2024 6:59 PM
To: tensorflow/tensorflow ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [tensorflow/tensorflow] An error in the official pruning guide (Issue #62929)


Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

source

TensorFlow version

2.15

Custom code

No

OS platform and distribution

Google Colab

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

I followed this official pruning guide: https://www.tensorflow.org/model_optimization/guide/pruning/pruning_with_keras


I opened code in google colab (https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/pruning/pruning_with_keras.ipynb) and ran all cells..


And there is an ValueError: `prune_low_magnitude` can only prune an object of the following types: keras.models.Sequential, keras functional model, keras.layers.Layer, list of keras.layers.Layer. You passed an object of type: Sequential. in line model_for_pruning = prune_low_magnitude(model, **pruning_params)


I tried this code on my local machine and it gives the same error.
This looks like an bug.

Standalone code to reproduce the issue

Code from offical guide: https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/pruning/pruning_with_keras.ipynb

Relevant log output

No response

—
Reply to this email directly, view it on GitHub<https://github.com/tensorflow/tensorflow/issues/62929>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AZCZ53ZDUVNIQNPWNCCHJYLYSYOMRAVCNFSM6AAAAABDBODQVKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGEZDOMJRHA3TANI>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
 I solved this problem with: <br>
```! pip install tf-keras ```<br>
and replace ```from tensorflow import keras ``` with  ```import tf_keras as keras``` @Santabot123,
I tried to execute the mentioned official document with the **tensorflow v2.15, keras v3.0 and the tensorflow-model-optimization - 0.8.0** and it was executed without any issue/error. Kindly find the gist of it [here](https://colab.research.google.com/gist/tilakrayal/0eaea793a5186842e8aa4d3b2bd6bfa3/pruning_with_keras.ipynb). Thank you! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62929">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62929">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

2.15

### Custom code

No

### OS platform and distribution

Google Colab

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I followed this official pruning guide: https://www.tensorflow.org/model_optimization/guide/pruning/pruning_with_keras
<br>
I opened code in google colab (https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/pruning/pruning_with_keras.ipynb) and ran all cells..
<br>
And there is an ```ValueError: `prune_low_magnitude` can only prune an object of the following types: keras.models.Sequential, keras functional model, keras.layers.Layer, list of keras.layers.Layer. You passed an object of type: Sequential.``` in line ```model_for_pruning = prune_low_magnitude(model, **pruning_params)```
<br>
I tried this code on my local machine and it gives the same error.
This looks like an bug.

### Standalone code to reproduce the issue

```shell
Code from offical guide: https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/pruning/pruning_with_keras.ipynb
```


### Relevant log output

_No response_