- Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathmnist_dpsgd_tutorial_common.py
71 lines (60 loc) · 2.43 KB
/
mnist_dpsgd_tutorial_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright 2020, The TensorFlow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common tools for DP-SGD MNIST tutorials."""
importtensorflowastf
importtensorflow_datasetsastfds
defget_cnn_model(features):
"""Given input features, returns the logits from a simple CNN model."""
input_layer=tf.reshape(features, [-1, 28, 28, 1])
y=tf.keras.layers.Conv2D(
16, 8, strides=2, padding='same', activation='relu')(
input_layer)
y=tf.keras.layers.MaxPool2D(2, 1)(y)
y=tf.keras.layers.Conv2D(
32, 4, strides=2, padding='valid', activation='relu')(
y)
y=tf.keras.layers.MaxPool2D(2, 1)(y)
y=tf.keras.layers.Flatten()(y)
y=tf.keras.layers.Dense(32, activation='relu')(y)
logits=tf.keras.layers.Dense(10)(y)
returnlogits
defmake_input_fn(split, input_batch_size=256, repetitions=-1, tpu=False):
"""Make input function on given MNIST split."""
definput_fn(params=None):
"""A simple input function."""
batch_size=params.get('batch_size', input_batch_size)
defparser(example):
image, label=example['image'], example['label']
image=tf.cast(image, tf.float32)
image/=255.0
label=tf.cast(label, tf.int32)
returnimage, label
dataset=tfds.load(name='mnist', split=split)
dataset=dataset.map(parser).shuffle(60000).repeat(repetitions).batch(
batch_size)
# If this input function is not meant for TPUs, we can stop here.
# Otherwise, we need to explicitly set its shape. Note that for unknown
# reasons, returning the latter format causes performance regression
# on non-TPUs.
ifnottpu:
returndataset
# Give inputs statically known shapes; needed for TPUs.
images, labels=tf.data.make_one_shot_iterator(dataset).get_next()
# return images, labels
images.set_shape([batch_size, 28, 28, 1])
labels.set_shape([
batch_size,
])
returnimages, labels
returninput_fn