欢迎使用本教程! 在以下笔记本中,您将学习如何提供隐私预测。隐私预测是指在整个过程中对数据进行持续加密。用户绝不会共享原始数据,而只会共享加密(即秘密共享)的数据。为了提供这些私人预测,Syft Keras在后台使用了一个名为TF Encrypted的库。 TF Encrypted结合了最先进的加密技术和机器学习技术,但您不必为此担心,可以专注于您的机器学习应用程序。
您只需三个步骤即可开始提供隐私预测:
好吧,让我们完成这三个步骤,以便您可以在不牺牲用户隐私或模型安全性的情况下部署有效的机器学习服务。
作者:
代表:
中文版译者:
In [ ]:
from __future__ import print_function
import tensorflow.keras as keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, AveragePooling2D
from tensorflow.keras.layers import Activation
batch_size = 128
num_classes = 10
epochs = 2
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(10, (3, 3), input_shape=input_shape))
model.add(AveragePooling2D((2, 2)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(AveragePooling2D((2, 2)))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(AveragePooling2D((2, 2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
好!您的模型已经过训练。让我们使用model.save()保存模型权重。在下一个笔记本中,我们将把这些权重加载到Syft Keras中,以开始提供私人预测。
In [ ]:
model.save('short-conv-mnist.h5')
In [ ]:
In [ ]: