Created
June 27, 2020 00:27
-
-
Save ahmedfgad/2b668e7a43197e30d0ec7994eb841542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Preparing the NumPy array of the inputs. | |
data_inputs = numpy.array([[1, 1], | |
[1, 0], | |
[0, 1], | |
[0, 0]]) | |
# Preparing the NumPy array of the outputs. | |
data_outputs = numpy.array([0, | |
1, | |
1, | |
0]) | |
model = None | |
def reply(self, received_data): | |
global GANN_instance, data_inputs, data_outputs, model | |
if (type(received_data) is dict): | |
if (("data" in received_data.keys()) and ("subject" in received_data.keys())): | |
subject = received_data["subject"] | |
print("Client's Message Subject is {subject}.".format(subject=subject)) | |
print("Replying to the Client.") | |
if subject == "echo": | |
try: | |
data = {"subject": "model", "data": GANN_instance} | |
response = pickle.dumps(data) | |
except BaseException as e: | |
print("Error Decoding the Client's Data: {msg}.\n".format(msg=e)) | |
elif subject == "model": | |
try: | |
GANN_instance = received_data["data"] | |
best_model_idx = received_data["best_solution_idx"] | |
best_model = GANN_instance.population_networks[best_model_idx] | |
if model is None: | |
model = best_model | |
else: | |
predictions = pygad.nn.predict(last_layer=model, data_inputs=data_inputs) | |
error = numpy.sum(numpy.abs(predictions - data_outputs)) | |
# In case a client sent a model to the server despite that the model error is 0.0. In this case, no need to make changes in the model. | |
if error == 0: | |
data = {"subject": "done", "data": None} | |
response = pickle.dumps(data) | |
return | |
self.model_averaging(model, best_model) | |
predictions = pygad.nn.predict(last_layer=model, data_inputs=data_inputs) | |
print("Model Predictions: {predictions}".format(predictions=predictions)) | |
error = numpy.sum(numpy.abs(predictions - data_outputs)) | |
print("Error = {error}".format(error=error)) | |
if error != 0: | |
data = {"subject": "model", "data": GANN_instance} | |
response = pickle.dumps(data) | |
else: | |
data = {"subject": "done", "data": None} | |
response = pickle.dumps(data) | |
except BaseException as e: | |
print("Error Decoding the Client's Data: {msg}.\n".format(msg=e)) | |
else: | |
response = pickle.dumps("Response from the Server") | |
try: | |
self.connection.sendall(response) | |
except BaseException as e: | |
print("Error Sending Data to the Client: {msg}.\n".format(msg=e)) | |
else: | |
print("The received dictionary from the client must have the 'subject' and 'data' keys available. The existing keys are {d_keys}.".format(d_keys=received_data.keys())) | |
else: | |
print("A dictionary is expected to be received from the client but {d_type} received.".format(d_type=type(received_data))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment