class Client:
    def __init__(self, input_shape, clients_num):
        (train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
        self.test_images = np.load("FL_QR_mnist//data//test_R.npy")
        self.test_labels = test_labels
        self.clients_num = clients_num
        self.model = D_CNN(input_shape)
        self.train_images = train_x(clients_num)
        self.train_labels = train_y(clients_num)

    def run_test(self, test_num):
        test_images = self.test_images[:test_num]
        test_labels_OneHot = to_categorical(self.test_labels[:test_num], 10)
        test_images = test_images.reshape(-1, 28, 28, 1)
        loss, acc = self.model.evaluate(test_images,
                                        test_labels_OneHot, verbose=0)
        return loss, acc

    def train_epoch(self, client_id):
        train_images_C = self.train_images[client_id]
        train_labels_OneHot_C = self.train_labels[client_id]
        self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        train_history_C = self.model.fit(train_images_C, train_labels_OneHot_C, epochs=1, batch_size=128, validation_split=0.2)
        return train_history_C.history

    def choose_clients(self, ratio=1.0):
        choose_num = math.ceil(self.clients_num * ratio)
        return np.random.permutation(self.clients_num)[:choose_num]  # 序列进行随机排序
Exemple #2
0
    "FL_QR_mnist//data//test_Q.npy")  # "solve_DL_gradient//test_gam_32.npy"
train_images_R = np.load("FL_QR_mnist//data//train_R.npy")
test_images_R = np.load("FL_QR_mnist//data//test_R.npy")

train_labels_OneHot = to_categorical(train_labels)
test_labels_OneHot = to_categorical(test_labels)

############################# all
model = D_CNN((28, 28, 1))
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
train_images = train_images[:500].reshape(-1, 28, 28, 1)
model.fit(train_images,
          train_labels_OneHot[:500],
          batch_size=128,
          epochs=20,
          validation_split=0.2)

test_images = test_images.reshape(10000, 28, 28, 1)
score = model.evaluate(test_images, test_labels_OneHot)
model.save("za_all//all_local.h5")
#########################    Q_CNN   ###############
model = D_CNN((28, 28, 1))
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
train_images_Q = train_images_Q.reshape(60000, 28, 28, 1)
loss_acc = []
loss_acc0 = []
loss_acc1 = []