Consider the configuration examples that match your model type.
Save the Tensorflow model
Copy link to section
import tensorflow as tf
from tensorflow.keras import *
from tensorflow.keras.layers import *
import numpy as np
import os
classMyModel(Model):
def__init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10)
defcall(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
# Create an instance of the model
model = MyModel()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True)
optimizer = tf.keras.optimizers.Adam()
acc = tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy')
model.compile(optimizer=optimizer, loss=loss_object, metrics=[acc])
img_rows, img_cols = 28, 28
input_shape = (None, img_rows, img_cols, 1)
model.compute_output_shape(input_shape=input_shape)
dir = "./model_architecture"ifnot os.path.exists(dir):
os.makedirs(dir)
model.save(dir)
Copy to clipboardCopied to clipboardShow more
If you choose Tensorflow as the model framework, you need to save a Keras model as the SavedModel format. A Keras model can be saved in SavedModel format by using tf.keras.model.save().
To compress your files, run the command zip -r mymodel.zip model_architecture. The contents of your .zip file must contain:
# SKLearn classificationfrom sklearn.linear_model import SGDClassifier
import numpy as np
import joblib
model = SGDClassifier(loss='log', penalty='l2')
model.classes_ = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# You must specify the class label for IBM Federated Learning using model.classes. Class labels must be contained in a numpy array.# In the example, there are 10 classes.
joblib.dump(model, "./model_architecture.pickle")
Copy to clipboardCopied to clipboard
SKLearn regression
Copy link to section
# Sklearn regressionfrom sklearn.linear_model import SGDRegressor
import pickle
model = SGDRegressor(loss='huber', penalty='l2')
withopen("./model_architecture.pickle", 'wb') as f:
pickle.dump(model, f)
You need to create a .zip file that contains your model in pickle format by running the command zip mymodel.zip model_architecture.pickle. The contents of your .zip file must contain:
You need to create a .zip file containing your model in pickle format. Run the command zip mymodel.zip model_architecture.pt. The contents of your .zip file should contain:
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.