├── Office_Cold_6000-9000_6-10_13.npz ├── README.md ├── federated model optimization.ipynb ├── fine-tune.py └── single_federated_model.ipynb /Office_Cold_6000-9000_6-10_13.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kirito-E2D/Federated-learning-based-prediction/ea5c80437b8e484306615bfeffdd1d73b7c5ed81/Office_Cold_6000-9000_6-10_13.npz -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Federated-learning-based-prediction 2 | 3 | The aim of this project is to develop A Federated learning-based method to predict building energy consumption without data privacy leakage. 4 | 5 | The operational data from 13 similar office buildings can be downloaded from the repository. 6 | 7 | These codes should be processed on Google Colaboratory or Jupyter. 8 | 9 | federated model optimization.ipynb is utilized to optimize the hyperparameters of the federated model. 10 | 11 | fine-tune.py is utilized to fine-tune the federated model using local data. 12 | -------------------------------------------------------------------------------- /federated model optimization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "ANN_keras", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "GJVRTox5IHN7", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "pip install --quiet --upgrade tensorflow_federated" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "Rk1cK0dPIUiS", 33 | "colab_type": "code", 34 | "colab": {} 35 | }, 36 | "source": [ 37 | "import collections\n", 38 | "\n", 39 | "import numpy as np\n", 40 | "import tensorflow as tf\n", 41 | "import tensorflow_federated as tff\n", 42 | "\n", 43 | "tf.compat.v1.enable_v2_behavior()\n", 44 | "\n", 45 | "# TODO(b/148678573,b/148685415): must use the ReferenceExecutor because it\n", 46 | "# supports unbounded references and tff.sequence_* intrinsics.\n", 47 | "tff.framework.set_default_executor(tff.framework.ReferenceExecutor())" 48 | ], 49 | "execution_count": null, 50 | "outputs": [] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "metadata": { 55 | "id": "cbQtG7eNIY1Y", 56 | "colab_type": "code", 57 | "colab": {} 58 | }, 59 | "source": [ 60 | "@tff.federated_computation\n", 61 | "def hello_world():\n", 62 | " return 'Hello, World!'\n", 63 | "\n", 64 | "hello_world()" 65 | ], 66 | "execution_count": null, 67 | "outputs": [] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "metadata": { 72 | "id": "hrSfLIAfIby4", 73 | "colab_type": "code", 74 | "colab": {} 75 | }, 76 | "source": [ 77 | "from google.colab import drive\n", 78 | "drive.mount('/content/drive')" 79 | ], 80 | "execution_count": null, 81 | "outputs": [] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "metadata": { 86 | "id": "ZTOh3zmEIgfJ", 87 | "colab_type": "code", 88 | "colab": {} 89 | }, 90 | "source": [ 91 | "import numpy as np\n", 92 | "import pandas as pd\n", 93 | "\n", 94 | "#all_buildings = pd.read_csv('/content/drive/My Drive/Dataset/building meta data.csv')\n", 95 | "zipfile = np.load('/content/drive/My Drive/Dataset/Office_Cold_6000-9000_6-10_13.npz', allow_pickle=True)\n", 96 | "\n", 97 | "Building_data = zipfile['data']\n", 98 | "Building_data_norm = zipfile['data_norm']\n", 99 | "Building_names = zipfile['name']\n", 100 | "List_of_names = pd.Series(Building_names).unique()\n", 101 | "\n", 102 | "NUM_EXAMPLES_PER_USER = 10000\n", 103 | "BATCH_SIZE = 500\n", 104 | "\n", 105 | "EC_min = np.min(Building_data[:,-1])\n", 106 | "EC_max = np.max(Building_data[:,-1])\n", 107 | "EC_mean = np.mean(Building_data[:,-1])\n", 108 | "\n", 109 | "def get_data_for_name(Data, Names, name):\n", 110 | " output_sequence = []\n", 111 | " all_samples = [i for i, n in enumerate(Names) if n == name]\n", 112 | "\n", 113 | " for i in range(0, min(len(all_samples), NUM_EXAMPLES_PER_USER), BATCH_SIZE):\n", 114 | " batch_samples = all_samples[i:i+BATCH_SIZE]\n", 115 | " output_sequence.append({'x':np.array([Data[i,:-1] for i in batch_samples], dtype=np.float32),\n", 116 | " 'y':np.array([Data[i,-1] for i in batch_samples], dtype=np.float32)})\n", 117 | " return output_sequence\n", 118 | "\n", 119 | "def get_label_for_name(Data, Names, name):\n", 120 | " output_sequence = []\n", 121 | " all_samples = [i for i, n in enumerate(Names) if n == name]\n", 122 | "\n", 123 | " for i in range(0, min(len(all_samples), NUM_EXAMPLES_PER_USER), BATCH_SIZE):\n", 124 | " batch_samples = all_samples[i:i+BATCH_SIZE]\n", 125 | " output_sequence.append({'y':np.array([Data[i,-1] for i in batch_samples], dtype=np.float32)})\n", 126 | " return output_sequence\n", 127 | "\n", 128 | "\n", 129 | "federated_train_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[:-2]]\n", 130 | "\n", 131 | "federated_valid_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[-2:-1]]\n", 132 | "\n", 133 | "#local_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[-1:]]\n", 134 | "\n", 135 | "#local_train_data = local_data[0][:2]\n", 136 | "\n", 137 | "#local_test_data = local_data[0][-2:]\n", 138 | "\n", 139 | "#Label_test_data = [get_label_for_name(Building_data, Building_names, name) for name in List_of_names[-1:]][0][-2:]" 140 | ], 141 | "execution_count": null, 142 | "outputs": [] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "metadata": { 147 | "id": "zWQQMwCfKXTQ", 148 | "colab_type": "code", 149 | "colab": {} 150 | }, 151 | "source": [ 152 | "BATCH_SPEC = collections.OrderedDict(x=tf.TensorSpec(shape=[None,6], dtype=tf.float32), y=tf.TensorSpec(shape=[None], dtype=tf.float32))" 153 | ], 154 | "execution_count": null, 155 | "outputs": [] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "metadata": { 160 | "id": "q251EVM6ky-d", 161 | "colab_type": "code", 162 | "colab": {} 163 | }, 164 | "source": [ 165 | "num_layers = 1\n", 166 | "num_neurons = 5\n", 167 | "activation = 'relu'\n", 168 | "\n", 169 | "def create_keras_model(num_layers, num_neurons, activation):\n", 170 | " model = tf.keras.models.Sequential()\n", 171 | " model.add(tf.keras.layers.Dense(num_neurons, activation=activation, input_shape=(6,)))\n", 172 | " for i in range(num_layers-1):\n", 173 | " model.add(tf.keras.layers.Dense(num_neurons, activation=activation))\n", 174 | " model.add(tf.keras.layers.Dense(1))\n", 175 | " return model\n", 176 | "\n", 177 | "def model_fn():\n", 178 | " # We _must_ create a new model here, and _not_ capture it from an external\n", 179 | " # scope. TFF will call this within different graph contexts.\n", 180 | " keras_model = create_keras_model(num_layers=num_layers, num_neurons=num_neurons, activation=activation)\n", 181 | " return tff.learning.from_keras_model(keras_model, input_spec=BATCH_SPEC, loss=tf.keras.losses.MeanSquaredError(), metrics=[tf.keras.metrics.MeanAbsoluteError()])" 182 | ], 183 | "execution_count": null, 184 | "outputs": [] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "metadata": { 189 | "id": "vKnawUaNKcnI", 190 | "colab_type": "code", 191 | "colab": {} 192 | }, 193 | "source": [ 194 | "def create_keras_model():\n", 195 | " return tf.keras.models.Sequential([\n", 196 | " tf.keras.layers.Input(shape=(6)),\n", 197 | " tf.keras.layers.Dense(10, kernel_initializer='zeros'),\n", 198 | " tf.keras.layers.Dense(10, kernel_initializer='zeros'),\n", 199 | " tf.keras.layers.Dense(1)])\n", 200 | " \n", 201 | "def model_fn():\n", 202 | " # We _must_ create a new model here, and _not_ capture it from an external\n", 203 | " # scope. TFF will call this within different graph contexts.\n", 204 | " keras_model = create_keras_model()\n", 205 | " return tff.learning.from_keras_model(keras_model, input_spec=BATCH_SPEC, loss=tf.keras.losses.MeanSquaredError(), metrics=[tf.keras.metrics.MeanAbsoluteError()])" 206 | ], 207 | "execution_count": null, 208 | "outputs": [] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "metadata": { 213 | "id": "qXJEkEdWuOdD", 214 | "colab_type": "code", 215 | "colab": {} 216 | }, 217 | "source": [ 218 | "#initial_state = iterative_process.initialize()\n", 219 | "best_metric = 80\n", 220 | "for num_layers in [5]:\n", 221 | " for num_neurons in [5,10,15,20,25]:\n", 222 | " for activation in ['relu','sigmoid','tanh']:\n", 223 | " print('current parameters are: ', num_layers, num_neurons, activation)\n", 224 | " iterative_process = tff.learning.build_federated_averaging_process(\n", 225 | " model_fn,\n", 226 | " client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),\n", 227 | " server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0))\n", 228 | " state = iterative_process.initialize()\n", 229 | " NUM_ROUNDS = 100\n", 230 | " loss = []\n", 231 | " for round_num in range(NUM_ROUNDS):\n", 232 | " state, metrics = iterative_process.next(state, federated_train_data)\n", 233 | " evaluation = tff.learning.build_federated_evaluation(model_fn)\n", 234 | " valid_metrics = evaluation(state.model, federated_valid_data)\n", 235 | " loss.append(valid_metrics[1])\n", 236 | " if len(loss) > 1:\n", 237 | " if ((loss[-2]-loss[-1]) / loss[-2] < 0.005):\n", 238 | " print('Train process is done')\n", 239 | " break\n", 240 | " if valid_metrics[0] < best_metric:\n", 241 | " best_state = state\n", 242 | " best_metric = valid_metrics[0]\n", 243 | " best_num_layers = num_layers\n", 244 | " best_num_neurons = num_neurons\n", 245 | " best_activation = activation\n", 246 | "print('best parameters are: ', best_num_layers, best_num_neurons, best_activation, best_metric)" 247 | ], 248 | "execution_count": null, 249 | "outputs": [] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "metadata": { 254 | "id": "704Bux2pKsyo", 255 | "colab_type": "code", 256 | "colab": {} 257 | }, 258 | "source": [ 259 | "num_layers = 1\n", 260 | "num_neurons = 5\n", 261 | "activation = 'tanh'\n", 262 | "NUM_ROUNDS = 50\n", 263 | "iterative_process = tff.learning.build_federated_averaging_process(\n", 264 | " model_fn,\n", 265 | " client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),\n", 266 | " server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0))" 267 | ], 268 | "execution_count": null, 269 | "outputs": [] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "metadata": { 274 | "id": "E_LjlW-IK1cA", 275 | "colab_type": "code", 276 | "colab": {} 277 | }, 278 | "source": [ 279 | "str(iterative_process.initialize.type_signature)" 280 | ], 281 | "execution_count": null, 282 | "outputs": [] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "metadata": { 287 | "id": "eCHKq2qPK3_R", 288 | "colab_type": "code", 289 | "colab": {} 290 | }, 291 | "source": [ 292 | "state = iterative_process.initialize()" 293 | ], 294 | "execution_count": null, 295 | "outputs": [] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "metadata": { 300 | "id": "Rk0vwO8DdJmG", 301 | "colab_type": "code", 302 | "colab": {} 303 | }, 304 | "source": [ 305 | "loss = []\n", 306 | "for round_num in range(NUM_ROUNDS):\n", 307 | " state, metrics = iterative_process.next(state, federated_train_data)\n", 308 | " evaluation = tff.learning.build_federated_evaluation(model_fn)\n", 309 | " valid_metrics = evaluation(state.model, federated_valid_data)\n", 310 | " loss.append(valid_metrics[1])\n", 311 | " if len(loss) > 1:\n", 312 | " if ((loss[-2]-loss[-1]) / loss[-2] < 0.005):\n", 313 | " print('Train process is done')\n", 314 | " break\n", 315 | " print('round {:2d}, metrics={}'.format(round_num, metrics), valid_metrics[0])" 316 | ], 317 | "execution_count": null, 318 | "outputs": [] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "metadata": { 323 | "id": "m_YUeFaVxzWa", 324 | "colab_type": "code", 325 | "colab": {} 326 | }, 327 | "source": [ 328 | "loss" 329 | ], 330 | "execution_count": null, 331 | "outputs": [] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "metadata": { 336 | "id": "FMvpM_HjdhsN", 337 | "colab_type": "code", 338 | "colab": {} 339 | }, 340 | "source": [ 341 | "evaluation = tff.learning.build_federated_evaluation(model_fn)\n", 342 | "valid_metrics = evaluation(state.model, federated_valid_data)" 343 | ], 344 | "execution_count": null, 345 | "outputs": [] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "metadata": { 350 | "id": "7V-MVVo4dnW2", 351 | "colab_type": "code", 352 | "cellView": "code", 353 | "colab": {} 354 | }, 355 | "source": [ 356 | "str(valid_metrics)" 357 | ], 358 | "execution_count": null, 359 | "outputs": [] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "metadata": { 364 | "id": "OBgiYW52eyj_", 365 | "colab_type": "code", 366 | "colab": {} 367 | }, 368 | "source": [ 369 | "forward_pass(batch_input=federated_train_data, training=False)\n", 370 | "a = tff.learning.Model()" 371 | ], 372 | "execution_count": null, 373 | "outputs": [] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "metadata": { 378 | "id": "ApoN7hsde1-u", 379 | "colab_type": "code", 380 | "colab": {} 381 | }, 382 | "source": [ 383 | "local_model = create_keras_model(best_num_layers, best_num_neurons, best_activation)\n", 384 | "local_fed_model = create_keras_model(best_num_layers, best_num_neurons, best_activation)" 385 | ], 386 | "execution_count": null, 387 | "outputs": [] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "metadata": { 392 | "id": "7TvZ0sb8e48F", 393 | "colab_type": "code", 394 | "colab": {} 395 | }, 396 | "source": [ 397 | "tff.learning.assign_weights_to_keras_model(local_fed_model, best_state.model)\n", 398 | "tf.keras.models.save_model(local_fed_model, '/content/drive/My Drive/Federated learning/Office_Cold_6000-9000_6-10_13')\n", 399 | "tf.keras.models.save_model(local_model, '/content/drive/My Drive/Federated learning/Office_Cold_6000-9000_6-10_13_no_fed')\n", 400 | "local_model.compile(optimizer='adam', loss='mse', metrics=['mae'])\n", 401 | "local_fed_model.compile(optimizer='adam', loss='mse', metrics=['mae'])" 402 | ], 403 | "execution_count": null, 404 | "outputs": [] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "metadata": { 409 | "id": "fJ0apIq8BfVh", 410 | "colab_type": "code", 411 | "colab": {} 412 | }, 413 | "source": [ 414 | "local_fed_model.save('/content/drive/My Drive/Federated learning/Office_Cold_6000-9000_6-10_13_2.h5')\n", 415 | "local_model.save('/content/drive/My Drive/Federated learning/Office_Cold_6000-9000_6-10_13_no_fed_2.h5')\n", 416 | "#keras.models.load_model()" 417 | ], 418 | "execution_count": null, 419 | "outputs": [] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "metadata": { 424 | "id": "PDQBBHhCfHVT", 425 | "colab_type": "code", 426 | "colab": {} 427 | }, 428 | "source": [ 429 | "local_model.evaluate(local_test_data[0]['x'], local_test_data[0]['y'], batch_size=32)" 430 | ], 431 | "execution_count": null, 432 | "outputs": [] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "metadata": { 437 | "id": "BG3nWE4i6P0B", 438 | "colab_type": "code", 439 | "colab": {} 440 | }, 441 | "source": [ 442 | "local_fed_model.evaluate(local_test_data[0]['x'], local_test_data[0]['y'], batch_size=32)" 443 | ], 444 | "execution_count": null, 445 | "outputs": [] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "metadata": { 450 | "id": "ZfhIuhDIfQm0", 451 | "colab_type": "code", 452 | "colab": {} 453 | }, 454 | "source": [ 455 | "early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=5, mode='auto', baseline=None, restore_best_weights=True)\n", 456 | "local_model.fit(local_train_data[0]['x'], local_train_data[0]['y'], batch_size=64, epochs=100, validation_split=0.2, callbacks=[early_stopping])\n", 457 | "local_fed_model.fit(local_train_data[0]['x'], local_train_data[0]['y'], batch_size=64, epochs=100, validation_split=0.2, callbacks=[early_stopping])" 458 | ], 459 | "execution_count": null, 460 | "outputs": [] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "metadata": { 465 | "id": "R9UJxZXUfTlk", 466 | "colab_type": "code", 467 | "colab": {} 468 | }, 469 | "source": [ 470 | "p1 = local_fed_model.predict(local_test_data[1]['x'])\n", 471 | "p2 = local_model.predict(local_test_data[1]['x'])\n", 472 | "prediction1 = p1*(EC_max-EC_min)+EC_min\n", 473 | "prediction2 = p1*(EC_max-EC_min)+EC_min\n", 474 | "def CVRMSE(prediction, true):\n", 475 | " return (np.sqrt(np.mean(np.square(true - prediction))) / np.mean(true))" 476 | ], 477 | "execution_count": null, 478 | "outputs": [] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "metadata": { 483 | "id": "vqpp7rORfYCF", 484 | "colab_type": "code", 485 | "colab": {} 486 | }, 487 | "source": [ 488 | "CVRMSE(p1, local_test_data[0]['y'])" 489 | ], 490 | "execution_count": null, 491 | "outputs": [] 492 | } 493 | ] 494 | } -------------------------------------------------------------------------------- /fine-tune.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2020/05/13 4 | Updated on 2020/05/13 5 | @author: Li Junyang 6 | """ 7 | 8 | #——————————————————————————————————————ANN回归———————————————————————————————————— 9 | 10 | #———————————————固定随机数——————————————— 11 | from numpy.random import seed 12 | seed(1) 13 | from tensorflow import set_random_seed 14 | set_random_seed(1) 15 | 16 | 17 | #——————————导入所需Python包—————————— 18 | import math 19 | import time 20 | import keras 21 | import xlsxwriter 22 | import numpy as np 23 | import pandas as pd 24 | import tensorflow as tf 25 | import matplotlib.pyplot as plt 26 | from sklearn.neural_network import MLPRegressor 27 | from sklearn.model_selection import GridSearchCV 28 | from sklearn.model_selection import train_test_split 29 | 30 | 31 | #——————————定义常量—————————— 32 | model_name = 'ANN_fed' 33 | #STRAT = 3559-2 #对应6月1日20时 34 | #END = 7181-2 35 | #inputsize = 6 36 | 37 | 38 | #——————————————————————导入数据—————————————————————— 39 | #df_train = pd.read_csv(open(r'F:\建筑数据集\building data 507\data of office_5.csv')) 40 | #df_train.fillna(method='bfill', inplace=True) #填充时和后面的保持一致 41 | # 42 | #df_test = pd.read_csv(open(r'F:\建筑数据集\building data 507\data of office_2.csv')) 43 | #df_test.fillna(method='bfill', inplace=True) #填充时和后面的保持一致 44 | 45 | #df = df_train.append(df_test) 46 | 47 | df = pd.read_csv(open(r'E:\建筑数据集\building data 507\Office_Cold_6000-9000_6-10_13.csv')) 48 | #Office_NewYork_1000-3000_6-10_7.csv 21936为测试建筑的第一个数据,25586为最后一个数据 49 | 50 | #获取时间 51 | Time = np.array(df['hour']).astype(int).reshape(-1) 52 | 53 | #获取变量 54 | data = np.array(df.loc[:, ['TemperatureC', 'Humidity', 'hour', 'weekday', 'area', 'energy before', 'energy']]) 55 | EC_min = np.min(data[:, -1]) 56 | EC_max = np.max(data[:, -1]) 57 | normalized_data = (data-np.min(data,0))/(np.max(data,0)-np.min(data,0)) 58 | 59 | 60 | #——————————————————生成训练集、验证集、测试集—————————————————————— 61 | #划分训练集、验证集和测试集 62 | x_train = normalized_data[-3645:-744, :-1] #6个特征,-3645对应target building 6月1日0时,-744对应10月1日0时 63 | y_train = normalized_data[-3645:-744, -1] #能耗 64 | time_train = Time[-3645:-744] 65 | 66 | ##5% 67 | #x_train_5 = x_train[-int(len(x_train)*0.05):] 68 | #y_train_5 = y_train[-int(len(x_train)*0.05):] 69 | #time_train_5 = time_train[-int(len(x_train)*0.05):] 70 | # 71 | ##10% 72 | #x_train_10 = x_train[-int(len(x_train)*0.10):] 73 | #y_train_10 = y_train[-int(len(x_train)*0.10):] 74 | #time_train_10 = time_train[-int(len(x_train)*0.10):] 75 | # 76 | ##10% 77 | #x_train_15 = x_train[-int(len(x_train)*0.15):] 78 | #y_train_15 = y_train[-int(len(x_train)*0.15):] 79 | #time_train_15 = time_train[-int(len(x_train)*0.15):] 80 | # 81 | ##20% 82 | #x_train_20 = x_train[-int(len(x_train)*0.20):] 83 | #y_train_20 = y_train[-int(len(x_train)*0.20):] 84 | #time_train_20 = time_train[-int(len(x_train)*0.20):] 85 | # 86 | ##30% 87 | #x_train_30 = x_train[-int(len(x_train)*0.30):] 88 | #y_train_30 = y_train[-int(len(x_train)*0.30):] 89 | #time_train_30 = time_train[-int(len(x_train)*0.30):] 90 | # 91 | ##40% 92 | #x_train_40 = x_train[-int(len(x_train)*0.40):] 93 | #y_train_40 = y_train[-int(len(x_train)*0.40):] 94 | #time_train_40 = time_train[-int(len(x_train)*0.40):] 95 | # 96 | ##50% 97 | #x_train_50 = x_train[-int(len(x_train)*0.50):] 98 | #y_train_50 = y_train[-int(len(x_train)*0.50):] 99 | #time_train_50 = time_train[-int(len(x_train)*0.50):] 100 | # 101 | ##60% 102 | #x_train_60 = x_train[-int(len(x_train)*0.60):] 103 | #y_train_60 = y_train[-int(len(x_train)*0.60):] 104 | #time_train_60 = time_train[-int(len(x_train)*0.60):] 105 | # 106 | ##%70 107 | #x_train_70 = x_train[-int(len(x_train)*0.70):] 108 | #y_train_70 = y_train[-int(len(x_train)*0.70):] 109 | #time_train_70 = time_train[-int(len(x_train)*0.70):] 110 | # 111 | ##%80 112 | #x_train_80 = x_train[-int(len(x_train)*0.80):] 113 | #y_train_80 = y_train[-int(len(x_train)*0.80):] 114 | #time_train_80 = time_train[-int(len(x_train)*0.80):] 115 | # 116 | ##%100 117 | #x_train_100 = x_train[-int(len(x_train)):] 118 | #y_train_100 = y_train[-int(len(x_train)):] 119 | #time_train_100 = time_train[-int(len(x_train)):] 120 | # 121 | x_test = normalized_data[-744:, :-1] #6个特征,10月份31天的数据 122 | y_test = normalized_data[-744:, -1] 123 | Y_test = data[-744:, -1] #冷负荷 124 | time_test = Time[-744:] 125 | 126 | #Y_data = data[:, -1] 127 | #time_data = time[:] #时间 128 | #x_train, x_rest, y_train, y_rest = train_test_split(x_data,y_data,test_size=0.3, random_state=1) 129 | #time_train, time_rest, _, _ = train_test_split(time_data,y_data,test_size=0.3, random_state=1) 130 | #Y_train, Y_rest, _, _ = train_test_split(Y_data,y_data,test_size=0.3, random_state=1) 131 | # 132 | #x_valid, x_test, y_valid, y_test = train_test_split(x_rest,y_rest,test_size=0.5, random_state=1) 133 | #time_valid, time_test, _, _ = train_test_split(time_rest,y_rest,test_size=0.5, random_state=1) 134 | #Y_valid, Y_test, _, _ = train_test_split(Y_rest,y_rest,test_size=0.5, random_state=1) 135 | 136 | 137 | #——————————————————神经网络回归预测—————————————————— 138 | #ann = MLPRegressor(random_state=0) 139 | #param_grid = {'hidden_layer_sizes':[(math.ceil(x_train.shape[1]*2/3+1)), 140 | # (math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1)), 141 | # (math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1)), 142 | # (math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1)), 143 | # (math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1),math.ceil(x_train.shape[1]*2/3+1))]} 144 | #ANN = GridSearchCV(ann, param_grid, cv=5) #参数寻优 145 | #ANN = MLPRegressor((5,5), activation='relu', random_state=0, solver='adam') 146 | 147 | #ANN = tf.keras.models.load_model('Office_Cold_6000-9000_6-10_13.h5', compile=False) 148 | ##ANN = tf.keras.models.load_model('Office_Cold_6000-9000_6-10_13_no_fed.h5', compile=False) 149 | ##ANN = tf.keras.models.load_model('fed_model.h5', compile=False) 150 | ##y_ANN = ANN.predict(x_test) #无finetune,直接预测 151 | #ANN.compile(optimizer='sgd', loss='mse', metrics=['mae']) 152 | #early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, mode='auto', baseline=None, restore_best_weights=True) 153 | ##early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=2, mode='auto', baseline=None) 154 | ##ANN.fit(x_train_80, y_train_80, batch_size=64, epochs=100, validation_split=0.1, callbacks=[early_stopping]) #模型训练 155 | #y_ANN = ANN.predict(x_test) #模型预测 156 | #predictions = y_ANN*(EC_max-EC_min) + EC_min 157 | ##predictions = y_ANN 158 | #error = Y_test-predictions 159 | #MAE = np.mean(abs(Y_test-predictions)) #绝对误差 160 | #mae = np.mean(abs(y_test-y_ANN)) #绝对误差 161 | #RMSE = np.sqrt(np.mean(np.square(Y_test-predictions))) #均方根误差 162 | #rmse = np.sqrt(np.mean(np.square(y_test-y_ANN))) #均方根误差 163 | #MAPE = np.mean(abs(Y_test-predictions)/Y_test) #相对百分比误差 164 | #CV_RMSE = RMSE / np.mean(Y_test) 165 | ##CPGE = abs(predictions.cumsum()-Y_test.cumsum()) / Y_test.cumsum() * 100 #累积发电量误差 166 | #R2 = 1 - np.sum(np.square(Y_test-predictions)) / np.sum(np.square(Y_test-np.mean(Y_test))) 167 | #print('MAE on test set is %g, RMSE on test set is %g, MAPE on test set is %g, CV_RMSE on test set is %g, R2 on test set is %g.' %(MAE, RMSE, MAPE, CV_RMSE, R2)) 168 | 169 | 170 | # 所有比例数据 171 | MAEs = [] 172 | RMSEs = [] 173 | MAPEs = [] 174 | CV_RMSEs = [] 175 | EPOCHs = [] 176 | CC = [] 177 | P = [] 178 | 179 | for p in np.linspace(0.01, 1, num=100, endpoint=True): 180 | print('Current proportion is ' + str(p)) 181 | ANN = tf.keras.models.load_model('Office_Cold_6000-9000_6-10_13.h5', compile=False) 182 | ANN.compile(optimizer='sgd', loss='mse', metrics=['mae']) 183 | early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, mode='auto', baseline=None, restore_best_weights=True) 184 | #early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=2, mode='auto', baseline=None) 185 | x_train_ = x_train[-int(len(x_train)*p):] 186 | y_train_ = y_train[-int(len(x_train)*p):] 187 | time_train_ = time_train[-int(len(x_train)*p):] 188 | 189 | start = time.perf_counter() #计算程序运行时间 190 | history = ANN.fit(x_train_, y_train_, batch_size=64, epochs=1000, validation_split=0.1, callbacks=[early_stopping]) #模型训练 191 | end = time.perf_counter() 192 | t = end - start 193 | print("Training time is :", t) 194 | epoch = len(history.history['loss']) 195 | y_ANN = ANN.predict(x_test) #模型预测 196 | predictions = y_ANN*(EC_max-EC_min) + EC_min 197 | #predictions = y_ANN 198 | #error = Y_test-predictions 199 | MAE = np.mean(abs(Y_test-predictions)) #绝对误差 200 | #mae = np.mean(abs(y_test-y_ANN)) #绝对误差 201 | RMSE = np.sqrt(np.mean(np.square(Y_test-predictions))) #均方根误差 202 | #rmse = np.sqrt(np.mean(np.square(y_test-y_ANN))) #均方根误差 203 | MAPE = np.mean(abs(Y_test-predictions)/Y_test) #相对百分比误差 204 | CV_RMSE = RMSE / np.mean(Y_test) 205 | #CPGE = abs(predictions.cumsum()-Y_test.cumsum()) / Y_test.cumsum() * 100 #累积发电量误差 206 | #R2 = 1 - np.sum(np.square(Y_test-predictions)) / np.sum(np.square(Y_test-np.mean(Y_test))) 207 | MAEs.append(MAE) 208 | RMSEs.append(RMSE) 209 | MAPEs.append(MAPE) 210 | CV_RMSEs.append(CV_RMSE) 211 | EPOCHs.append(epoch) 212 | CC.append(t) 213 | P.append(predictions) 214 | 215 | PREDICTIONs = np.array(P).T 216 | 217 | 218 | ##——————————————————————保存ANN回归预测结果—————————————————————— 219 | #workbook = xlsxwriter.Workbook('./Prediction of ' + model_name + '.xlsx') 220 | #worksheet = workbook.add_worksheet('Prediction') 221 | #worksheet.write(0, 1, 'Real data') 222 | #worksheet.write(0, 2, 'Prediction data') 223 | #worksheet.write(0, 3, 'distacne') 224 | #worksheet.write(0, 4, 'error') 225 | #worksheet.write(0, 5, 'MAE of prediction') 226 | #worksheet.write(0, 6, 'RMSE of prediction') 227 | #worksheet.write(0, 7, 'MAPE of prediction') 228 | #worksheet.write(0, 8, 'CV_RMSE of prediction') 229 | #worksheet.write(0, 9, 'R2 of prediction') 230 | #worksheet.write(1, 5, MAE) 231 | #worksheet.write(1, 6, RMSE) 232 | #worksheet.write(1, 7, MAPE) 233 | #worksheet.write(1, 8, CV_RMSE) 234 | #worksheet.write(1, 9, R2) 235 | #for i in range(predictions.shape[0]): 236 | # worksheet.write(i+1, 1, Y_test[i]) 237 | # worksheet.write(i+1, 2, predictions[i]) 238 | # worksheet.write(i+1, 3, dis[i]) 239 | # worksheet.write(i+1, 4, error[i]) 240 | # #worksheet.write(i+1, 6, CPGE[i]) 241 | #workbook.close() 242 | 243 | 244 | ##——————————————————————可视化—————————————————————— 245 | #plt.close('all') 246 | #x = range(len(predictions[:240])) 247 | #names = time_test[:240].tolist()[::4] 248 | #pos = x[::4] 249 | #plt.figure(figsize=(6.4*3.2,4.8*3.2), dpi=100) 250 | #plt.plot(x, Y_test[:240], 'b-', label='Actual value') 251 | #plt.plot(x, predictions[:240], 'r--', label='Forecasting value') 252 | #plt.xticks(pos, names, rotation=80) 253 | #plt.tick_params(labelsize=15) 254 | #plt.xlabel('Time', fontsize=20) 255 | #plt.ylabel('Cooling load(kW)', fontsize=20) 256 | #plt.legend(fontsize=20) 257 | #plt.savefig('Prediction of ' + model_name + '.png') 258 | #plt.show() 259 | 260 | # 261 | #y_ANN = ANN.predict(x_train) #模型预测 262 | #x = range(len(y_ANN[:100])) 263 | #names = time_train[:100].tolist()[::2] 264 | #pos = x[::2] 265 | #plt.figure(figsize=(6.4*3.2,4.8*3.2), dpi=100) 266 | #plt.plot(x, y_train[:100], 'b-', label='Actual value') 267 | #plt.plot(x, y_ANN[:100], 'r--', label='Forecasting value') 268 | #plt.xticks(pos, names, rotation=80) 269 | #plt.tick_params(labelsize=15) 270 | #plt.xlabel('Time', fontsize=20) 271 | #plt.ylabel('Cooling load(kW)', fontsize=20) 272 | #plt.legend(fontsize=20) 273 | ##plt.savefig('Prediction of ' + model_name + '.png') 274 | #plt.show() -------------------------------------------------------------------------------- /single_federated_model.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "single federated model", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "qvRgFiYt3Ydt", 20 | "colab_type": "code", 21 | "colab": { 22 | "base_uri": "https://localhost:8080/", 23 | "height": 238 24 | }, 25 | "outputId": "c64de9ff-0e89-4a8f-b57b-8e59fc3f47c0" 26 | }, 27 | "source": [ 28 | "pip install --quiet --upgrade tensorflow_federated" 29 | ], 30 | "execution_count": null, 31 | "outputs": [ 32 | { 33 | "output_type": "stream", 34 | "text": [ 35 | "\u001b[K |████████████████████████████████| 430kB 2.7MB/s \n", 36 | "\u001b[K |████████████████████████████████| 296kB 41.5MB/s \n", 37 | "\u001b[K |████████████████████████████████| 2.8MB 40.5MB/s \n", 38 | "\u001b[K |████████████████████████████████| 421.8MB 37kB/s \n", 39 | "\u001b[K |████████████████████████████████| 2.2MB 33.2MB/s \n", 40 | "\u001b[K |████████████████████████████████| 20.0MB 69.6MB/s \n", 41 | "\u001b[K |████████████████████████████████| 102kB 11.3MB/s \n", 42 | "\u001b[K |████████████████████████████████| 450kB 52.7MB/s \n", 43 | "\u001b[K |████████████████████████████████| 3.9MB 41.8MB/s \n", 44 | "\u001b[?25h Building wheel for gast (setup.py) ... \u001b[?25l\u001b[?25hdone\n", 45 | "\u001b[31mERROR: tensorflow-probability 0.10.0rc0 has requirement gast>=0.3.2, but you'll have gast 0.2.2 which is incompatible.\u001b[0m\n", 46 | "\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.\u001b[0m\n", 47 | "\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n" 48 | ], 49 | "name": "stdout" 50 | } 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "metadata": { 56 | "id": "RYaVgXWP-NvN", 57 | "colab_type": "code", 58 | "colab": {} 59 | }, 60 | "source": [ 61 | "#pip install --upgrade tensorflow_federated" 62 | ], 63 | "execution_count": null, 64 | "outputs": [] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "metadata": { 69 | "id": "McS2mDMF3mCN", 70 | "colab_type": "code", 71 | "colab": {} 72 | }, 73 | "source": [ 74 | "#pip install --quiet tensorflow==2.1" 75 | ], 76 | "execution_count": null, 77 | "outputs": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "metadata": { 82 | "id": "vS7OhTaH4AH3", 83 | "colab_type": "code", 84 | "colab": {} 85 | }, 86 | "source": [ 87 | "import collections\n", 88 | "\n", 89 | "import numpy as np\n", 90 | "import tensorflow as tf\n", 91 | "import tensorflow_federated as tff\n", 92 | "\n", 93 | "tf.compat.v1.enable_v2_behavior()\n", 94 | "\n", 95 | "# TODO(b/148678573,b/148685415): must use the ReferenceExecutor because it\n", 96 | "# supports unbounded references and tff.sequence_* intrinsics.\n", 97 | "tff.framework.set_default_executor(tff.framework.ReferenceExecutor())" 98 | ], 99 | "execution_count": null, 100 | "outputs": [] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "metadata": { 105 | "id": "X7ccTzo84GBy", 106 | "colab_type": "code", 107 | "colab": { 108 | "base_uri": "https://localhost:8080/", 109 | "height": 34 110 | }, 111 | "outputId": "bd5e1f44-c398-4be5-95c5-6f2833be5a9d" 112 | }, 113 | "source": [ 114 | "@tff.federated_computation\n", 115 | "def hello_world():\n", 116 | " return 'Hello, World!'\n", 117 | "\n", 118 | "hello_world()" 119 | ], 120 | "execution_count": null, 121 | "outputs": [ 122 | { 123 | "output_type": "execute_result", 124 | "data": { 125 | "text/plain": [ 126 | "'Hello, World!'" 127 | ] 128 | }, 129 | "metadata": { 130 | "tags": [] 131 | }, 132 | "execution_count": 3 133 | } 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "metadata": { 139 | "id": "Ha8foHwobHIx", 140 | "colab_type": "code", 141 | "colab": { 142 | "base_uri": "https://localhost:8080/", 143 | "height": 122 144 | }, 145 | "outputId": "c7532157-3636-4e0f-f63b-ac8dab914092" 146 | }, 147 | "source": [ 148 | "from google.colab import drive\n", 149 | "drive.mount('/content/drive')" 150 | ], 151 | "execution_count": null, 152 | "outputs": [ 153 | { 154 | "output_type": "stream", 155 | "text": [ 156 | "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n", 157 | "\n", 158 | "Enter your authorization code:\n", 159 | "··········\n", 160 | "Mounted at /content/drive\n" 161 | ], 162 | "name": "stdout" 163 | } 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "metadata": { 169 | "id": "2dJq9Om5YACZ", 170 | "colab_type": "code", 171 | "colab": {} 172 | }, 173 | "source": [ 174 | "import numpy as np\n", 175 | "import pandas as pd\n", 176 | "\n", 177 | "all_buildings = pd.read_csv('/content/drive/My Drive/Dataset/building meta data.csv')\n", 178 | "zipfile = np.load('/content/drive/My Drive/Dataset/Office_NewYork_1000-3000_6-10_7.npz', allow_pickle=True)\n", 179 | "\n", 180 | "Building_data = zipfile['data']\n", 181 | "Building_data_norm = zipfile['data_norm']\n", 182 | "Building_names = zipfile['name']\n", 183 | "List_of_names = pd.Series(Building_names).unique()\n", 184 | "\n", 185 | "NUM_EXAMPLES_PER_USER = 10000\n", 186 | "BATCH_SIZE = 500\n", 187 | "\n", 188 | "EC_min = np.min(Building_data[:,-1])\n", 189 | "EC_max = np.max(Building_data[:,-1])\n", 190 | "EC_mean = np.mean(Building_data[:,-1])\n", 191 | "\n", 192 | "def get_data_for_name(Data, Names, name):\n", 193 | " output_sequence = []\n", 194 | " all_samples = [i for i, n in enumerate(Names) if n == name]\n", 195 | "\n", 196 | " for i in range(0, min(len(all_samples), NUM_EXAMPLES_PER_USER), BATCH_SIZE):\n", 197 | " batch_samples = all_samples[i:i+BATCH_SIZE]\n", 198 | " output_sequence.append({'x':np.array([Data[i,:-1] for i in batch_samples], dtype=np.float32),\n", 199 | " 'y':np.array([Data[i,-1] for i in batch_samples], dtype=np.float32)})\n", 200 | " return output_sequence\n", 201 | "\n", 202 | "def get_label_for_name(Data, Names, name):\n", 203 | " output_sequence = []\n", 204 | " all_samples = [i for i, n in enumerate(Names) if n == name]\n", 205 | "\n", 206 | " for i in range(0, min(len(all_samples), NUM_EXAMPLES_PER_USER), BATCH_SIZE):\n", 207 | " batch_samples = all_samples[i:i+BATCH_SIZE]\n", 208 | " output_sequence.append({'y':np.array([Data[i,-1] for i in batch_samples], dtype=np.float32)})\n", 209 | " return output_sequence\n", 210 | "\n", 211 | "\n", 212 | "federated_train_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[:-2]]\n", 213 | "\n", 214 | "federated_valid_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[-2:-1]]\n", 215 | "\n", 216 | "local_data = [get_data_for_name(Building_data_norm, Building_names, name) for name in List_of_names[-1:]]\n", 217 | "\n", 218 | "local_train_data = local_data[0][:2]\n", 219 | "\n", 220 | "local_test_data = local_data[0][-2:]\n", 221 | "\n", 222 | "Label_test_data = [get_label_for_name(Building_data, Building_names, name) for name in List_of_names[-1:]][0][-2:]" 223 | ], 224 | "execution_count": null, 225 | "outputs": [] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "id": "bCsQIocP4lFa", 231 | "colab_type": "code", 232 | "colab": { 233 | "base_uri": "https://localhost:8080/", 234 | "height": 561 235 | }, 236 | "outputId": "132b922d-2866-4b35-a357-a3c5a4ece58d" 237 | }, 238 | "source": [ 239 | "federated_train_data[4][-1]['y']" 240 | ], 241 | "execution_count": null, 242 | "outputs": [ 243 | { 244 | "output_type": "execute_result", 245 | "data": { 246 | "text/plain": [ 247 | "array([0.20318568, 0.2237399 , 0.2491381 , 0.24848352, 0.24521056,\n", 248 | " 0.25214925, 0.25699323, 0.25450578, 0.2609208 , 0.24573424,\n", 249 | " 0.2229544 , 0.21640848, 0.22125246, 0.22622737, 0.2264892 ,\n", 250 | " 0.22544184, 0.23264237, 0.23198777, 0.23238054, 0.23473707,\n", 251 | " 0.23656993, 0.23146412, 0.24442504, 0.24861443, 0.30504036,\n", 252 | " 0.28775913, 0.3495527 , 0.34470868, 0.35897884, 0.36041892,\n", 253 | " 0.39341044, 0.3432686 , 0.35766965, 0.37665287, 0.29456687,\n", 254 | " 0.25345844, 0.23997381, 0.23251146, 0.23552257, 0.23146412,\n", 255 | " 0.23355879, 0.23761728, 0.23958106, 0.23997381, 0.24010472,\n", 256 | " 0.24953088, 0.24874537, 0.25031638, 0.28998473, 0.3221907 ,\n", 257 | " 0.37612915, 0.40820426, 0.381235 , 0.35662228, 0.3648702 ,\n", 258 | " 0.37586734, 0.35112372, 0.33083135, 0.30058914, 0.25162557,\n", 259 | " 0.24953088, 0.23958106, 0.23604625, 0.23264237, 0.2292385 ,\n", 260 | " 0.22635828, 0.2271438 , 0.2293694 , 0.2271438 , 0.22766747,\n", 261 | " 0.23473707, 0.24324678, 0.2939123 , 0.33109316, 0.4010037 ,\n", 262 | " 0.41566658, 0.3804495 , 0.41723764, 0.42260528, 0.4028366 ,\n", 263 | " 0.3693214 , 0.35662228, 0.29875627, 0.27126336, 0.25005454,\n", 264 | " 0.22884575, 0.22033603, 0.22662012, 0.22190705, 0.21981235,\n", 265 | " 0.22151428, 0.21981235, 0.21680121, 0.21549204, 0.22491819,\n", 266 | " 0.23120227, 0.26222998, 0.28461707, 0.32101244, 0.35112372,\n", 267 | " 0.3403884 , 0.34143573, 0.3369845 , 0.34706524, 0.34300676,\n", 268 | " 0.32834387, 0.28082043, 0.24769801, 0.24036658, 0.23185687,\n", 269 | " 0.23290421, 0.23382065, 0.22177613, 0.22518001, 0.22164522,\n", 270 | " 0.22059786, 0.21981235, 0.22046694, 0.22635828, 0.23224962,\n", 271 | " 0.2757146 , 0.27833298, 0.27558368, 0.34353042, 0.35675323,\n", 272 | " 0.37167796, 0.3437923 , 0.34758893, 0.32101247, 0.29626882,\n", 273 | " 0.2554222 , 0.22609644, 0.22452542, 0.22085969, 0.223609 ,\n", 274 | " 0.23107135, 0.22897665, 0.23094043, 0.23133318, 0.23028585,\n", 275 | " 0.23670085, 0.2384028 , 0.23814094, 0.23801003, 0.22976217,\n", 276 | " 0.22452542, 0.21300457, 0.20331661, 0.19441414, 0.18590443,\n", 277 | " 0.19847262, 0.20030548, 0.21287367, 0.21077897, 0.21509929,\n", 278 | " 0.21902685, 0.22884572], dtype=float32)" 279 | ] 280 | }, 281 | "metadata": { 282 | "tags": [] 283 | }, 284 | "execution_count": 6 285 | } 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "metadata": { 291 | "id": "ZWHigle8c76z", 292 | "colab_type": "code", 293 | "colab": { 294 | "base_uri": "https://localhost:8080/", 295 | "height": 51 296 | }, 297 | "outputId": "396853ab-a35d-43e5-c1e9-df4c32ccc78b" 298 | }, 299 | "source": [ 300 | "EC_mean_test = (np.sum(Label_test_data[0]['y'])+np.sum(Label_test_data[1]['y'])) / (len(Label_test_data[0]['y'])+len(Label_test_data[1]['y']))\n", 301 | "print(EC_mean_test)\n", 302 | "print(EC_mean)" 303 | ], 304 | "execution_count": null, 305 | "outputs": [ 306 | { 307 | "output_type": "stream", 308 | "text": [ 309 | "59.98853926651306\n", 310 | "32.58286671659921\n" 311 | ], 312 | "name": "stdout" 313 | } 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "metadata": { 319 | "id": "jOd0xgJx4rxh", 320 | "colab_type": "code", 321 | "colab": { 322 | "base_uri": "https://localhost:8080/", 323 | "height": 34 324 | }, 325 | "outputId": "25e4bd40-604e-4d08-ef0c-54e457ef6390" 326 | }, 327 | "source": [ 328 | "BATCH_SPEC = collections.OrderedDict(x=tf.TensorSpec(shape=[None,6], dtype=tf.float32), y=tf.TensorSpec(shape=[None], dtype=tf.float32))\n", 329 | "BATCH_TYPE = tff.to_type(BATCH_SPEC)\n", 330 | "\n", 331 | "str(BATCH_TYPE)" 332 | ], 333 | "execution_count": null, 334 | "outputs": [ 335 | { 336 | "output_type": "execute_result", 337 | "data": { 338 | "text/plain": [ 339 | "''" 340 | ] 341 | }, 342 | "metadata": { 343 | "tags": [] 344 | }, 345 | "execution_count": 8 346 | } 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "metadata": { 352 | "id": "gKcWOEbhhPaN", 353 | "colab_type": "code", 354 | "colab": { 355 | "base_uri": "https://localhost:8080/", 356 | "height": 34 357 | }, 358 | "outputId": "53d917d2-0d21-4e8a-a02d-2fa20ee5a8f6" 359 | }, 360 | "source": [ 361 | "MODEL_SPEC = collections.OrderedDict(weights1=tf.TensorSpec(shape=[6,5], dtype=tf.float32),\n", 362 | " weights2=tf.TensorSpec(shape=[5,5], dtype=tf.float32),\n", 363 | " weights3=tf.TensorSpec(shape=[5,1], dtype=tf.float32),\n", 364 | " bias1=tf.TensorSpec(shape=[5], dtype=tf.float32),\n", 365 | " bias2=tf.TensorSpec(shape=[5], dtype=tf.float32),\n", 366 | " bias3=tf.TensorSpec(shape=[1], dtype=tf.float32))\n", 367 | "MODEL_TYPE = tff.to_type(MODEL_SPEC)\n", 368 | "\n", 369 | "print(MODEL_TYPE)" 370 | ], 371 | "execution_count": null, 372 | "outputs": [ 373 | { 374 | "output_type": "stream", 375 | "text": [ 376 | "\n" 377 | ], 378 | "name": "stdout" 379 | } 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "metadata": { 385 | "id": "0hdy-uMr5Hl6", 386 | "colab_type": "code", 387 | "colab": { 388 | "base_uri": "https://localhost:8080/", 389 | "height": 34 390 | }, 391 | "outputId": "45df809d-6d38-4279-c708-bf4e867f3f59" 392 | }, 393 | "source": [ 394 | "MODEL_SPEC = collections.OrderedDict(weights1=tf.TensorSpec(shape=[6,5], dtype=tf.float32),\n", 395 | " weights2=tf.TensorSpec(shape=[5,5], dtype=tf.float32),\n", 396 | " weights3=tf.TensorSpec(shape=[5,5], dtype=tf.float32),\n", 397 | " weights4=tf.TensorSpec(shape=[5,1], dtype=tf.float32),\n", 398 | " bias1=tf.TensorSpec(shape=[5], dtype=tf.float32),\n", 399 | " bias2=tf.TensorSpec(shape=[5], dtype=tf.float32),\n", 400 | " bias3=tf.TensorSpec(shape=[5], dtype=tf.float32),\n", 401 | " bias4=tf.TensorSpec(shape=[1], dtype=tf.float32))\n", 402 | "MODEL_TYPE = tff.to_type(MODEL_SPEC)\n", 403 | "\n", 404 | "print(MODEL_TYPE)" 405 | ], 406 | "execution_count": null, 407 | "outputs": [ 408 | { 409 | "output_type": "stream", 410 | "text": [ 411 | "\n" 412 | ], 413 | "name": "stdout" 414 | } 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "metadata": { 420 | "id": "zYJfUODfhdZH", 421 | "colab_type": "code", 422 | "colab": {} 423 | }, 424 | "source": [ 425 | "# NOTE: `forward_pass` is defined separately from `batch_loss` so that it can \n", 426 | "# be later called from within another tf.function. Necessary because a\n", 427 | "# @tf.function decorated method cannot invoke a @tff.tf_computation.\n", 428 | "\n", 429 | "@tf.function\n", 430 | "def forward_pass(model, batch):\n", 431 | " y1 = tf.nn.relu(tf.matmul(batch['x'],model['weights1']) + model['bias1'])\n", 432 | " y2 = tf.nn.relu(tf.matmul(y1,model['weights2']) + model['bias2'])\n", 433 | " predicted_y = tf.matmul(y2,model['weights3']) + model['bias3']\n", 434 | " return tf.reduce_mean(tf.square(batch['y'] - predicted_y))\n", 435 | "\n", 436 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 437 | "def batch_loss(model, batch):\n", 438 | " return forward_pass(model, batch)" 439 | ], 440 | "execution_count": null, 441 | "outputs": [] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "metadata": { 446 | "id": "z5Npq9315Nx6", 447 | "colab_type": "code", 448 | "colab": {} 449 | }, 450 | "source": [ 451 | "# NOTE: `forward_pass` is defined separately from `batch_loss` so that it can \n", 452 | "# be later called from within another tf.function. Necessary because a\n", 453 | "# @tf.function decorated method cannot invoke a @tff.tf_computation.\n", 454 | "\n", 455 | "@tf.function\n", 456 | "def forward_pass(model, batch):\n", 457 | " y1 = tf.nn.relu(tf.matmul(batch['x'],model['weights1']) + model['bias1'])\n", 458 | " y2 = tf.nn.relu(tf.matmul(y1,model['weights2']) + model['bias2'])\n", 459 | " y3 = tf.nn.relu(tf.matmul(y2,model['weights3']) + model['bias3'])\n", 460 | " predicted_y = tf.matmul(y3,model['weights4']) + model['bias4']\n", 461 | " return tf.reduce_mean(tf.square(batch['y'] - predicted_y))\n", 462 | "\n", 463 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 464 | "def batch_loss(model, batch):\n", 465 | " return forward_pass(model, batch)" 466 | ], 467 | "execution_count": null, 468 | "outputs": [] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "metadata": { 473 | "id": "14vQc9A45WBS", 474 | "colab_type": "code", 475 | "colab": { 476 | "base_uri": "https://localhost:8080/", 477 | "height": 34 478 | }, 479 | "outputId": "a7f452f9-48d7-41c7-b4ee-6eae4a86d3b5" 480 | }, 481 | "source": [ 482 | "str(batch_loss.type_signature)" 483 | ], 484 | "execution_count": null, 485 | "outputs": [ 486 | { 487 | "output_type": "execute_result", 488 | "data": { 489 | "text/plain": [ 490 | "'(<,> -> float32)'" 491 | ] 492 | }, 493 | "metadata": { 494 | "tags": [] 495 | }, 496 | "execution_count": 10 497 | } 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "metadata": { 503 | "id": "C7nJZkz1hyv9", 504 | "colab_type": "code", 505 | "colab": {} 506 | }, 507 | "source": [ 508 | "@tf.function\n", 509 | "def forward(model, batch):\n", 510 | " y1 = tf.nn.relu(tf.matmul(batch['x'],model['weights1']) + model['bias1'])\n", 511 | " y2 = tf.nn.relu(tf.matmul(y1,model['weights2']) + model['bias2'])\n", 512 | " predicted_y = tf.matmul(y2,model['weights3']) + model['bias3']\n", 513 | " return predicted_y" 514 | ], 515 | "execution_count": null, 516 | "outputs": [] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "metadata": { 521 | "id": "fs21yHKih2F9", 522 | "colab_type": "code", 523 | "colab": {} 524 | }, 525 | "source": [ 526 | "@tf.function\n", 527 | "def forward(model, batch):\n", 528 | " y1 = tf.nn.relu(tf.matmul(batch['x'],model['weights1']) + model['bias1'])\n", 529 | " y2 = tf.nn.relu(tf.matmul(y1,model['weights2']) + model['bias2'])\n", 530 | " y3 = tf.nn.relu(tf.matmul(y2,model['weights3']) + model['bias3'])\n", 531 | " predicted_y = tf.matmul(y3,model['weights4']) + model['bias4']\n", 532 | " return predicted_y" 533 | ], 534 | "execution_count": null, 535 | "outputs": [] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "metadata": { 540 | "id": "-Uor-bcj9xQj", 541 | "colab_type": "code", 542 | "colab": { 543 | "base_uri": "https://localhost:8080/", 544 | "height": 121 545 | }, 546 | "outputId": "e6b78f1c-7e75-4a78-f59d-4abecb3eefaa" 547 | }, 548 | "source": [ 549 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 550 | "def Metrics_CVRMSE(model, batch):\n", 551 | " prediction = forward(model, batch)\n", 552 | " return (tf.sqrt(tf.reduce_mean(tf.square(batch['y'] - prediction))) / tf.reduce_mean(batch['y'])) * (1-EC_min/(tf.reduce_mean(batch['y'])*(EC_max-EC_min)+EC_min))\n", 553 | "\n", 554 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 555 | "def Metrics_RMSE(model, batch):\n", 556 | " prediction = forward(model, batch)\n", 557 | " return tf.sqrt(tf.reduce_mean(tf.square(batch['y'] - prediction))) * (EC_max-EC_min)\n", 558 | "\n", 559 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 560 | "def Metrics_MAE(model, batch):\n", 561 | " prediction = forward(model, batch)\n", 562 | " return tf.reduce_mean(tf.abs(batch['y'] - prediction)) * (EC_max-EC_min)\n", 563 | "\n", 564 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 565 | "def Metrics_MAPE(model, batch):\n", 566 | " prediction = forward(model, batch)\n", 567 | " return tf.reduce_mean(tf.abs(batch['y'] - prediction) / (batch['y']+EC_min/(EC_max-EC_min)))\n", 568 | "\n", 569 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE)\n", 570 | "def Metrics_R2(model, batch):\n", 571 | " prediction = forward(model, batch)\n", 572 | " return 1 - tf.reduce_sum(tf.square(batch['y'] - prediction)) / tf.reduce_sum(tf.square(batch['y'] - tf.reduce_mean(batch['y'])))" 573 | ], 574 | "execution_count": null, 575 | "outputs": [ 576 | { 577 | "output_type": "stream", 578 | "text": [ 579 | "WARNING:tensorflow:6 out of the last 6 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", 580 | "WARNING:tensorflow:7 out of the last 7 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", 581 | "WARNING:tensorflow:8 out of the last 8 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", 582 | "WARNING:tensorflow:9 out of the last 9 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n", 583 | "WARNING:tensorflow:10 out of the last 10 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.\n" 584 | ], 585 | "name": "stdout" 586 | } 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "metadata": { 592 | "id": "U1vvN4XBiB0f", 593 | "colab_type": "code", 594 | "colab": { 595 | "base_uri": "https://localhost:8080/", 596 | "height": 34 597 | }, 598 | "outputId": "ccff0e34-ed48-4ea7-e61d-72cb9439e200" 599 | }, 600 | "source": [ 601 | "initial_model = collections.OrderedDict(weights1=np.zeros(shape=[6,5], dtype=np.float32),\n", 602 | " weights2=np.zeros(shape=[5,5], dtype=np.float32),\n", 603 | " weights3=np.zeros(shape=[5,1], dtype=np.float32),\n", 604 | " bias1=np.zeros(shape=[5], dtype=np.float32),\n", 605 | " bias2=np.zeros(shape=[5], dtype=np.float32),\n", 606 | " bias3=np.zeros(shape=[1], dtype=np.float32))\n", 607 | "\n", 608 | "sample_batch = federated_train_data[4][-2]\n", 609 | "\n", 610 | "batch_loss(initial_model, sample_batch)" 611 | ], 612 | "execution_count": null, 613 | "outputs": [ 614 | { 615 | "output_type": "execute_result", 616 | "data": { 617 | "text/plain": [ 618 | "0.0629788" 619 | ] 620 | }, 621 | "metadata": { 622 | "tags": [] 623 | }, 624 | "execution_count": 239 625 | } 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "metadata": { 631 | "id": "OzNQuqVQ5cD6", 632 | "colab_type": "code", 633 | "colab": {} 634 | }, 635 | "source": [ 636 | "initial_model = collections.OrderedDict(weights1=np.zeros(shape=[6,5], dtype=np.float32),\n", 637 | " weights2=np.zeros(shape=[5,5], dtype=np.float32),\n", 638 | " weights3=np.zeros(shape=[5,5], dtype=np.float32),\n", 639 | " weights4=np.zeros(shape=[5,1], dtype=np.float32),\n", 640 | " bias1=np.zeros(shape=[5], dtype=np.float32),\n", 641 | " bias2=np.zeros(shape=[5], dtype=np.float32),\n", 642 | " bias3=np.zeros(shape=[5], dtype=np.float32),\n", 643 | " bias4=np.zeros(shape=[1], dtype=np.float32))\n", 644 | "\n", 645 | "sample_batch = federated_train_data[4][-2]\n", 646 | "\n", 647 | "batch_loss(initial_model, sample_batch)" 648 | ], 649 | "execution_count": null, 650 | "outputs": [] 651 | }, 652 | { 653 | "cell_type": "code", 654 | "metadata": { 655 | "id": "ST9-hoQVdiKL", 656 | "colab_type": "code", 657 | "colab": { 658 | "base_uri": "https://localhost:8080/", 659 | "height": 34 660 | }, 661 | "outputId": "3df4308a-784b-4687-b29d-860854b24d95" 662 | }, 663 | "source": [ 664 | "Metrics_CVRMSE(initial_model, sample_batch)" 665 | ], 666 | "execution_count": null, 667 | "outputs": [ 668 | { 669 | "output_type": "execute_result", 670 | "data": { 671 | "text/plain": [ 672 | "0.9921009" 673 | ] 674 | }, 675 | "metadata": { 676 | "tags": [] 677 | }, 678 | "execution_count": 240 679 | } 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "metadata": { 685 | "id": "IYLtghAx-j-V", 686 | "colab_type": "code", 687 | "colab": { 688 | "base_uri": "https://localhost:8080/", 689 | "height": 34 690 | }, 691 | "outputId": "ce34b37e-5f6e-42ef-d4fc-68efa6363c88" 692 | }, 693 | "source": [ 694 | "Metrics_RMSE(initial_model, sample_batch)" 695 | ], 696 | "execution_count": null, 697 | "outputs": [ 698 | { 699 | "output_type": "execute_result", 700 | "data": { 701 | "text/plain": [ 702 | "47.922092" 703 | ] 704 | }, 705 | "metadata": { 706 | "tags": [] 707 | }, 708 | "execution_count": 241 709 | } 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "metadata": { 715 | "id": "mF1Ts3dU-mtw", 716 | "colab_type": "code", 717 | "colab": { 718 | "base_uri": "https://localhost:8080/", 719 | "height": 34 720 | }, 721 | "outputId": "961cb2cd-ad7a-4845-f334-b886647ccd0d" 722 | }, 723 | "source": [ 724 | "Metrics_MAE(initial_model, sample_batch)" 725 | ], 726 | "execution_count": null, 727 | "outputs": [ 728 | { 729 | "output_type": "execute_result", 730 | "data": { 731 | "text/plain": [ 732 | "46.67863" 733 | ] 734 | }, 735 | "metadata": { 736 | "tags": [] 737 | }, 738 | "execution_count": 242 739 | } 740 | ] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "metadata": { 745 | "id": "VrlzAQH--qjU", 746 | "colab_type": "code", 747 | "colab": { 748 | "base_uri": "https://localhost:8080/", 749 | "height": 34 750 | }, 751 | "outputId": "2cc4d5b9-ba2f-41a4-e5d4-9cb5892733eb" 752 | }, 753 | "source": [ 754 | "Metrics_MAPE(initial_model, sample_batch)" 755 | ], 756 | "execution_count": null, 757 | "outputs": [ 758 | { 759 | "output_type": "execute_result", 760 | "data": { 761 | "text/plain": [ 762 | "0.9646735" 763 | ] 764 | }, 765 | "metadata": { 766 | "tags": [] 767 | }, 768 | "execution_count": 243 769 | } 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "metadata": { 775 | "id": "MP4UbjZW-vgm", 776 | "colab_type": "code", 777 | "colab": { 778 | "base_uri": "https://localhost:8080/", 779 | "height": 34 780 | }, 781 | "outputId": "ba93b5a7-3954-4312-cf0f-c44aa16fe4c9" 782 | }, 783 | "source": [ 784 | "Metrics_R2(initial_model, sample_batch)" 785 | ], 786 | "execution_count": null, 787 | "outputs": [ 788 | { 789 | "output_type": "execute_result", 790 | "data": { 791 | "text/plain": [ 792 | "-9760.624" 793 | ] 794 | }, 795 | "metadata": { 796 | "tags": [] 797 | }, 798 | "execution_count": 244 799 | } 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "metadata": { 805 | "id": "5UZ6G9Tn5jLh", 806 | "colab_type": "code", 807 | "colab": {} 808 | }, 809 | "source": [ 810 | "@tff.tf_computation(MODEL_TYPE, BATCH_TYPE, tf.float32)\n", 811 | "def batch_train(initial_model, batch, learning_rate):\n", 812 | " # Define a group of model variables and set them to `initial_model`. Must\n", 813 | " # be defined outside the @tf.function.\n", 814 | " model_vars = collections.OrderedDict([(name, tf.Variable(name=name, initial_value=value)) for name, value in initial_model.items()])\n", 815 | " optimizer = tf.keras.optimizers.SGD(learning_rate)\n", 816 | "\n", 817 | " @tf.function\n", 818 | " def _train_on_batch(model_vars, batch):\n", 819 | " # Perform one step of gradient descent using loss from `batch_loss`.\n", 820 | " with tf.GradientTape() as tape:\n", 821 | " loss = forward_pass(model_vars, batch)\n", 822 | " grads = tape.gradient(loss, model_vars)\n", 823 | " optimizer.apply_gradients(zip(tf.nest.flatten(grads), tf.nest.flatten(model_vars)))\n", 824 | " return model_vars\n", 825 | "\n", 826 | " return _train_on_batch(model_vars, batch)" 827 | ], 828 | "execution_count": null, 829 | "outputs": [] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "metadata": { 834 | "id": "DzTbvD0f6P4P", 835 | "colab_type": "code", 836 | "colab": { 837 | "base_uri": "https://localhost:8080/", 838 | "height": 54 839 | }, 840 | "outputId": "7f524b6d-c5fd-4bbf-c878-bcbcc4150d26" 841 | }, 842 | "source": [ 843 | "str(batch_train.type_signature)" 844 | ], 845 | "execution_count": null, 846 | "outputs": [ 847 | { 848 | "output_type": "execute_result", 849 | "data": { 850 | "text/plain": [ 851 | "'(<,,float32> -> )'" 852 | ] 853 | }, 854 | "metadata": { 855 | "tags": [] 856 | }, 857 | "execution_count": 14 858 | } 859 | ] 860 | }, 861 | { 862 | "cell_type": "code", 863 | "metadata": { 864 | "id": "w7joW7oB6VEt", 865 | "colab_type": "code", 866 | "colab": {} 867 | }, 868 | "source": [ 869 | "model = initial_model\n", 870 | "losses = []\n", 871 | "CVRMSE = []\n", 872 | "for _ in range(5):\n", 873 | " model = batch_train(model, sample_batch, 0.1)\n", 874 | " losses.append(batch_loss(model, sample_batch))\n", 875 | " CVRMSE.append(Metrics_CVRMSE(model, sample_batch))" 876 | ], 877 | "execution_count": null, 878 | "outputs": [] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "metadata": { 883 | "id": "7IDB68Ao6m0H", 884 | "colab_type": "code", 885 | "colab": { 886 | "base_uri": "https://localhost:8080/", 887 | "height": 34 888 | }, 889 | "outputId": "3dea0873-336b-43fe-a7ab-c24cca13e911" 890 | }, 891 | "source": [ 892 | "losses" 893 | ], 894 | "execution_count": null, 895 | "outputs": [ 896 | { 897 | "output_type": "execute_result", 898 | "data": { 899 | "text/plain": [ 900 | "[0.041467734, 0.02770063, 0.018889714, 0.013250715, 0.009641752]" 901 | ] 902 | }, 903 | "metadata": { 904 | "tags": [] 905 | }, 906 | "execution_count": 247 907 | } 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "metadata": { 913 | "id": "eYTEaAYf_Wqy", 914 | "colab_type": "code", 915 | "colab": { 916 | "base_uri": "https://localhost:8080/", 917 | "height": 34 918 | }, 919 | "outputId": "85819d13-a246-4cc1-8cd6-c08f5482b9e7" 920 | }, 921 | "source": [ 922 | "CVRMSE" 923 | ], 924 | "execution_count": null, 925 | "outputs": [ 926 | { 927 | "output_type": "execute_result", 928 | "data": { 929 | "text/plain": [ 930 | "[0.80503327, 0.6579661, 0.5433396, 0.45507017, 0.38818312]" 931 | ] 932 | }, 933 | "metadata": { 934 | "tags": [] 935 | }, 936 | "execution_count": 248 937 | } 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "metadata": { 943 | "id": "LHegTHY06uAh", 944 | "colab_type": "code", 945 | "colab": {} 946 | }, 947 | "source": [ 948 | "LOCAL_DATA_TYPE = tff.SequenceType(BATCH_TYPE)\n", 949 | "\n", 950 | "@tff.federated_computation(MODEL_TYPE, tf.float32, LOCAL_DATA_TYPE)\n", 951 | "def local_train(initial_model, learning_rate, all_batches):\n", 952 | "\n", 953 | " # Mapping function to apply to each batch.\n", 954 | " @tff.federated_computation(MODEL_TYPE, BATCH_TYPE)\n", 955 | " def batch_fn(model, batch):\n", 956 | " return batch_train(model, batch, learning_rate)\n", 957 | "\n", 958 | " return tff.sequence_reduce(all_batches, initial_model, batch_fn)" 959 | ], 960 | "execution_count": null, 961 | "outputs": [] 962 | }, 963 | { 964 | "cell_type": "code", 965 | "metadata": { 966 | "id": "fHi44KFt6zv2", 967 | "colab_type": "code", 968 | "colab": { 969 | "base_uri": "https://localhost:8080/", 970 | "height": 54 971 | }, 972 | "outputId": "64e03017-2ca9-4a4b-dafc-6650e4406d7c" 973 | }, 974 | "source": [ 975 | "str(local_train.type_signature)" 976 | ], 977 | "execution_count": null, 978 | "outputs": [ 979 | { 980 | "output_type": "execute_result", 981 | "data": { 982 | "text/plain": [ 983 | "'(<,float32,*> -> )'" 984 | ] 985 | }, 986 | "metadata": { 987 | "tags": [] 988 | }, 989 | "execution_count": 160 990 | } 991 | ] 992 | }, 993 | { 994 | "cell_type": "code", 995 | "metadata": { 996 | "id": "wxNQjnbfGeea", 997 | "colab_type": "code", 998 | "colab": {} 999 | }, 1000 | "source": [ 1001 | "@tff.tf_computation(tf.float32, tf.float32)\n", 1002 | "def division(a, b):\n", 1003 | " return a / b" 1004 | ], 1005 | "execution_count": null, 1006 | "outputs": [] 1007 | }, 1008 | { 1009 | "cell_type": "code", 1010 | "metadata": { 1011 | "id": "rd5TDkkmJDmS", 1012 | "colab_type": "code", 1013 | "colab": {} 1014 | }, 1015 | "source": [ 1016 | "@tff.tf_computation(tf.float32, tf.float32)\n", 1017 | "def division(a, b):\n", 1018 | " return tf.divide(a, b)" 1019 | ], 1020 | "execution_count": null, 1021 | "outputs": [] 1022 | }, 1023 | { 1024 | "cell_type": "code", 1025 | "metadata": { 1026 | "id": "C1rhQmR2G7vn", 1027 | "colab_type": "code", 1028 | "colab": {} 1029 | }, 1030 | "source": [ 1031 | "@tff.tf_computation()\n", 1032 | "def get_one():\n", 1033 | " return 1.0" 1034 | ], 1035 | "execution_count": null, 1036 | "outputs": [] 1037 | }, 1038 | { 1039 | "cell_type": "code", 1040 | "metadata": { 1041 | "id": "EXrUtB2T65LE", 1042 | "colab_type": "code", 1043 | "colab": {} 1044 | }, 1045 | "source": [ 1046 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1047 | "def local_eval(model, all_batches):\n", 1048 | " # TODO(b/120157713): Replace with `tff.sequence_average()` once implemented.\n", 1049 | " return tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: batch_loss(model, b), BATCH_TYPE), all_batches))" 1050 | ], 1051 | "execution_count": null, 1052 | "outputs": [] 1053 | }, 1054 | { 1055 | "cell_type": "code", 1056 | "metadata": { 1057 | "id": "dRidTSsTm5NR", 1058 | "colab_type": "code", 1059 | "colab": {} 1060 | }, 1061 | "source": [ 1062 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1063 | "def local_eval_CVRMSE(model, all_batches):\n", 1064 | " sum_of_sequence = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: Metrics_CVRMSE(model, b), BATCH_TYPE), all_batches))\n", 1065 | " num = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: 1.0, BATCH_TYPE), all_batches))\n", 1066 | " return division(sum_of_sequence, num)\n", 1067 | "\n", 1068 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1069 | "def local_eval_RMSE(model, all_batches):\n", 1070 | " sum_of_sequence = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: Metrics_RMSE(model, b), BATCH_TYPE), all_batches))\n", 1071 | " num = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: 1.0, BATCH_TYPE), all_batches))\n", 1072 | " return division(sum_of_sequence, num)\n", 1073 | "\n", 1074 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1075 | "def local_eval_MAE(model, all_batches):\n", 1076 | " sum_of_sequence = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: Metrics_MAE(model, b), BATCH_TYPE), all_batches))\n", 1077 | " num = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: 1.0, BATCH_TYPE), all_batches))\n", 1078 | " return division(sum_of_sequence, num)\n", 1079 | "\n", 1080 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1081 | "def local_eval_MAPE(model, all_batches):\n", 1082 | " sum_of_sequence = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: Metrics_MAPE(model, b), BATCH_TYPE), all_batches))\n", 1083 | " num = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: 1.0, BATCH_TYPE), all_batches))\n", 1084 | " return division(sum_of_sequence, num)\n", 1085 | "\n", 1086 | "@tff.federated_computation(MODEL_TYPE, LOCAL_DATA_TYPE)\n", 1087 | "def local_eval_R2(model, all_batches):\n", 1088 | " sum_of_sequence = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: Metrics_R2(model, b), BATCH_TYPE), all_batches))\n", 1089 | " num = tff.sequence_sum(tff.sequence_map(tff.federated_computation(lambda b: 1.0, BATCH_TYPE), all_batches))\n", 1090 | " return division(sum_of_sequence, num)" 1091 | ], 1092 | "execution_count": null, 1093 | "outputs": [] 1094 | }, 1095 | { 1096 | "cell_type": "code", 1097 | "metadata": { 1098 | "id": "DwnqlyeJ7HKB", 1099 | "colab_type": "code", 1100 | "colab": { 1101 | "base_uri": "https://localhost:8080/", 1102 | "height": 34 1103 | }, 1104 | "outputId": "62006e2e-582a-415f-f51c-832e3a7428f5" 1105 | }, 1106 | "source": [ 1107 | "str(local_eval_CVRMSE.type_signature)" 1108 | ], 1109 | "execution_count": null, 1110 | "outputs": [ 1111 | { 1112 | "output_type": "execute_result", 1113 | "data": { 1114 | "text/plain": [ 1115 | "'(<,*> -> float32)'" 1116 | ] 1117 | }, 1118 | "metadata": { 1119 | "tags": [] 1120 | }, 1121 | "execution_count": 166 1122 | } 1123 | ] 1124 | }, 1125 | { 1126 | "cell_type": "code", 1127 | "metadata": { 1128 | "id": "ouiKOL9Y62QE", 1129 | "colab_type": "code", 1130 | "colab": {} 1131 | }, 1132 | "source": [ 1133 | "locally_trained_model = local_train(initial_model, 0.1, federated_train_data[4])" 1134 | ], 1135 | "execution_count": null, 1136 | "outputs": [] 1137 | }, 1138 | { 1139 | "cell_type": "code", 1140 | "metadata": { 1141 | "id": "KxFnH1_H7N46", 1142 | "colab_type": "code", 1143 | "colab": { 1144 | "base_uri": "https://localhost:8080/", 1145 | "height": 50 1146 | }, 1147 | "outputId": "1ae24117-7fe3-4db9-bffe-136e2530b59d" 1148 | }, 1149 | "source": [ 1150 | "print('initial_model loss =', local_eval(initial_model, federated_train_data[4]))\n", 1151 | "print('locally_trained_model loss =', local_eval(locally_trained_model, federated_train_data[4]))" 1152 | ], 1153 | "execution_count": null, 1154 | "outputs": [ 1155 | { 1156 | "output_type": "stream", 1157 | "text": [ 1158 | "initial_model loss = 0.49093497\n", 1159 | "locally_trained_model loss = 0.029914347\n" 1160 | ], 1161 | "name": "stdout" 1162 | } 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "code", 1167 | "metadata": { 1168 | "id": "eWZqyeYjNZ7N", 1169 | "colab_type": "code", 1170 | "colab": { 1171 | "base_uri": "https://localhost:8080/", 1172 | "height": 67 1173 | }, 1174 | "outputId": "9c97e430-eeb3-4ade-9d24-6e9625aed287" 1175 | }, 1176 | "source": [ 1177 | "print('initial_model CVRMSE =', local_eval_CVRMSE(initial_model, federated_train_data[4]))\n", 1178 | "print('locally_trained_model RMSE =', local_eval_RMSE(locally_trained_model, federated_train_data[4]))\n", 1179 | "print('locally_trained_model CVRMSE =', local_eval_CVRMSE(locally_trained_model, federated_train_data[4]))" 1180 | ], 1181 | "execution_count": null, 1182 | "outputs": [ 1183 | { 1184 | "output_type": "stream", 1185 | "text": [ 1186 | "initial_model CVRMSE = 0.983731\n", 1187 | "locally_trained_model RMSE = 11.177309\n", 1188 | "locally_trained_model CVRMSE = 0.22980188\n" 1189 | ], 1190 | "name": "stdout" 1191 | } 1192 | ] 1193 | }, 1194 | { 1195 | "cell_type": "code", 1196 | "metadata": { 1197 | "id": "4pcmKOng7ZSq", 1198 | "colab_type": "code", 1199 | "colab": { 1200 | "base_uri": "https://localhost:8080/", 1201 | "height": 50 1202 | }, 1203 | "outputId": "d8e63880-24f7-4cac-dbdc-e28fa9e2a1ba" 1204 | }, 1205 | "source": [ 1206 | "print('initial_model loss =', local_eval_CVRMSE(initial_model, federated_train_data[0]))\n", 1207 | "print('locally_trained_model loss =', local_eval_CVRMSE(locally_trained_model, federated_train_data[0]))" 1208 | ], 1209 | "execution_count": null, 1210 | "outputs": [ 1211 | { 1212 | "output_type": "stream", 1213 | "text": [ 1214 | "initial_model loss = 0.827339\n", 1215 | "locally_trained_model loss = 3.9903874\n" 1216 | ], 1217 | "name": "stdout" 1218 | } 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "metadata": { 1224 | "id": "ivGsomq57fCC", 1225 | "colab_type": "code", 1226 | "colab": {} 1227 | }, 1228 | "source": [ 1229 | "SERVER_MODEL_TYPE = tff.FederatedType(MODEL_TYPE, tff.SERVER)\n", 1230 | "CLIENT_DATA_TYPE = tff.FederatedType(LOCAL_DATA_TYPE, tff.CLIENTS)" 1231 | ], 1232 | "execution_count": null, 1233 | "outputs": [] 1234 | }, 1235 | { 1236 | "cell_type": "code", 1237 | "metadata": { 1238 | "id": "ymyNYUpQ7hPS", 1239 | "colab_type": "code", 1240 | "colab": {} 1241 | }, 1242 | "source": [ 1243 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1244 | "def federated_eval(model, data):\n", 1245 | " return tff.federated_mean(tff.federated_map(local_eval, [tff.federated_broadcast(model), data]))" 1246 | ], 1247 | "execution_count": null, 1248 | "outputs": [] 1249 | }, 1250 | { 1251 | "cell_type": "code", 1252 | "metadata": { 1253 | "id": "DuB0z60BXFa_", 1254 | "colab_type": "code", 1255 | "colab": {} 1256 | }, 1257 | "source": [ 1258 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1259 | "def federated_eval_CVRMSE(model, data):\n", 1260 | " return tff.federated_mean(tff.federated_map(local_eval_CVRMSE, [tff.federated_broadcast(model), data]))\n", 1261 | "\n", 1262 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1263 | "def federated_eval_RMSE(model, data):\n", 1264 | " return tff.federated_mean(tff.federated_map(local_eval_RMSE, [tff.federated_broadcast(model), data]))\n", 1265 | "\n", 1266 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1267 | "def federated_eval_MAE(model, data):\n", 1268 | " return tff.federated_mean(tff.federated_map(local_eval_MAE, [tff.federated_broadcast(model), data]))\n", 1269 | "\n", 1270 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1271 | "def federated_eval_MAPE(model, data):\n", 1272 | " return tff.federated_mean(tff.federated_map(local_eval_MAPE, [tff.federated_broadcast(model), data]))\n", 1273 | "\n", 1274 | "@tff.federated_computation(SERVER_MODEL_TYPE, CLIENT_DATA_TYPE)\n", 1275 | "def federated_eval_R2(model, data):\n", 1276 | " return tff.federated_mean(tff.federated_map(local_eval_R2, [tff.federated_broadcast(model), data]))" 1277 | ], 1278 | "execution_count": null, 1279 | "outputs": [] 1280 | }, 1281 | { 1282 | "cell_type": "code", 1283 | "metadata": { 1284 | "id": "CvPiC0fe8hET", 1285 | "colab_type": "code", 1286 | "colab": { 1287 | "base_uri": "https://localhost:8080/", 1288 | "height": 50 1289 | }, 1290 | "outputId": "ffc09586-033a-4ce6-81c6-54d9e3bee526" 1291 | }, 1292 | "source": [ 1293 | "print('initial_model loss =', federated_eval(initial_model, federated_train_data))\n", 1294 | "print('locally_trained_model loss =', federated_eval(locally_trained_model, federated_train_data))" 1295 | ], 1296 | "execution_count": null, 1297 | "outputs": [ 1298 | { 1299 | "output_type": "stream", 1300 | "text": [ 1301 | "initial_model loss = 0.3311762\n", 1302 | "locally_trained_model loss = 0.20935066\n" 1303 | ], 1304 | "name": "stdout" 1305 | } 1306 | ] 1307 | }, 1308 | { 1309 | "cell_type": "code", 1310 | "metadata": { 1311 | "id": "KSn4ePsrKbQM", 1312 | "colab_type": "code", 1313 | "colab": { 1314 | "base_uri": "https://localhost:8080/", 1315 | "height": 50 1316 | }, 1317 | "outputId": "0aa8b680-acdd-40bd-93c1-d2bf01c9147c" 1318 | }, 1319 | "source": [ 1320 | "print('initial_model CVRMSE =', federated_eval_CVRMSE(initial_model, federated_train_data))\n", 1321 | "print('locally_trained_model CVRMSE =', federated_eval_CVRMSE(locally_trained_model, federated_train_data))" 1322 | ], 1323 | "execution_count": null, 1324 | "outputs": [ 1325 | { 1326 | "output_type": "stream", 1327 | "text": [ 1328 | "initial_model CVRMSE = 0.96025115\n", 1329 | "locally_trained_model CVRMSE = 2.2337132\n" 1330 | ], 1331 | "name": "stdout" 1332 | } 1333 | ] 1334 | }, 1335 | { 1336 | "cell_type": "code", 1337 | "metadata": { 1338 | "id": "euoilORn9Prd", 1339 | "colab_type": "code", 1340 | "colab": {} 1341 | }, 1342 | "source": [ 1343 | "SERVER_FLOAT_TYPE = tff.FederatedType(tf.float32, tff.SERVER)\n", 1344 | "\n", 1345 | "@tff.federated_computation(SERVER_MODEL_TYPE, SERVER_FLOAT_TYPE, CLIENT_DATA_TYPE)\n", 1346 | "def federated_train(model, learning_rate, data):\n", 1347 | " return tff.federated_mean(tff.federated_map(local_train, [tff.federated_broadcast(model), tff.federated_broadcast(learning_rate), data]))" 1348 | ], 1349 | "execution_count": null, 1350 | "outputs": [] 1351 | }, 1352 | { 1353 | "cell_type": "code", 1354 | "metadata": { 1355 | "id": "yuufmbidmw4Q", 1356 | "colab_type": "code", 1357 | "colab": {} 1358 | }, 1359 | "source": [ 1360 | "initial_model = collections.OrderedDict(weights1=np.random.rand(6,5).astype(np.float32),\n", 1361 | " weights2=np.random.rand(5,5).astype(np.float32),\n", 1362 | " weights3=np.random.rand(5,1).astype(np.float32),\n", 1363 | " bias1=np.random.rand(5).astype(np.float32),\n", 1364 | " bias2=np.random.rand(5).astype(np.float32),\n", 1365 | " bias3=np.random.rand(1).astype(np.float32))" 1366 | ], 1367 | "execution_count": null, 1368 | "outputs": [] 1369 | }, 1370 | { 1371 | "cell_type": "code", 1372 | "metadata": { 1373 | "id": "qm-H2zfD-RP3", 1374 | "colab_type": "code", 1375 | "colab": { 1376 | "base_uri": "https://localhost:8080/", 1377 | "height": 118 1378 | }, 1379 | "outputId": "1c9fb761-da5e-4f5f-b658-e7ca6053613a" 1380 | }, 1381 | "source": [ 1382 | "fed_model = initial_model\n", 1383 | "learning_rate = 0.1\n", 1384 | "loss = []\n", 1385 | "for round_num in range(50):\n", 1386 | " fed_model = federated_train(fed_model, learning_rate, federated_train_data)\n", 1387 | " learning_rate = learning_rate * 0.9\n", 1388 | " loss_ = federated_eval(fed_model, federated_train_data)\n", 1389 | " RMSE = federated_eval_RMSE(fed_model, federated_train_data)\n", 1390 | " CVRMSE = federated_eval_CVRMSE(fed_model, federated_train_data)\n", 1391 | " loss.append(loss_) \n", 1392 | " if len(loss) > 1:\n", 1393 | " if ((loss[-2]-loss[-1]) / loss[-2] < 0.0001):\n", 1394 | " print('Train process is done')\n", 1395 | " break\n", 1396 | " print('round {}, loss={}, RMSE={}, CVRMSE={}'.format(round_num, loss_, RMSE, CVRMSE))" 1397 | ], 1398 | "execution_count": null, 1399 | "outputs": [ 1400 | { 1401 | "output_type": "stream", 1402 | "text": [ 1403 | "round 0, loss=3.5958471298217773, RMSE=125.65668487548828, CVRMSE=7.9549713134765625\n", 1404 | "round 1, loss=0.3102833330631256, RMSE=27.470705032348633, CVRMSE=0.8431023955345154\n", 1405 | "round 2, loss=0.18031781911849976, RMSE=22.725921630859375, CVRMSE=1.1795583963394165\n", 1406 | "round 3, loss=0.1737377643585205, RMSE=23.714635848999023, CVRMSE=1.412951111793518\n", 1407 | "round 4, loss=0.173425555229187, RMSE=24.03350067138672, CVRMSE=1.4761743545532227\n", 1408 | "Train process is done\n" 1409 | ], 1410 | "name": "stdout" 1411 | } 1412 | ] 1413 | }, 1414 | { 1415 | "cell_type": "code", 1416 | "metadata": { 1417 | "id": "c2k7LkpjruHq", 1418 | "colab_type": "code", 1419 | "colab": { 1420 | "base_uri": "https://localhost:8080/", 1421 | "height": 50 1422 | }, 1423 | "outputId": "a8d90582-d78d-4b03-8a7e-674f3d14f19a" 1424 | }, 1425 | "source": [ 1426 | "print('initial_model train CVRMSE =', federated_eval_CVRMSE(initial_model, federated_train_data))\n", 1427 | "print('trained_model train CVRMSE =', federated_eval_CVRMSE(fed_model, federated_train_data))" 1428 | ], 1429 | "execution_count": null, 1430 | "outputs": [ 1431 | { 1432 | "output_type": "stream", 1433 | "text": [ 1434 | "initial_model train CVRMSE = 223.89607\n", 1435 | "trained_model train CVRMSE = 1.493701\n" 1436 | ], 1437 | "name": "stdout" 1438 | } 1439 | ] 1440 | }, 1441 | { 1442 | "cell_type": "code", 1443 | "metadata": { 1444 | "id": "KG6fJXw_bYbA", 1445 | "colab_type": "code", 1446 | "colab": { 1447 | "base_uri": "https://localhost:8080/", 1448 | "height": 50 1449 | }, 1450 | "outputId": "0722fdca-f4fe-4d5b-eeaa-92636b8feedf" 1451 | }, 1452 | "source": [ 1453 | "print('initial_model test RMSE =', local_eval_RMSE(initial_model, local_test_data))\n", 1454 | "print('trained_model test RMSE =', local_eval_RMSE(fed_model, local_test_data))" 1455 | ], 1456 | "execution_count": null, 1457 | "outputs": [ 1458 | { 1459 | "output_type": "stream", 1460 | "text": [ 1461 | "initial_model test RMSE = 3072.248\n", 1462 | "trained_model test RMSE = 31.971203\n" 1463 | ], 1464 | "name": "stdout" 1465 | } 1466 | ] 1467 | }, 1468 | { 1469 | "cell_type": "code", 1470 | "metadata": { 1471 | "id": "G1R64R-NunoF", 1472 | "colab_type": "code", 1473 | "colab": { 1474 | "base_uri": "https://localhost:8080/", 1475 | "height": 34 1476 | }, 1477 | "outputId": "c6c46b9a-1aac-4b7c-d711-0321126c8383" 1478 | }, 1479 | "source": [ 1480 | "print('trained_model test CVRMSE =', local_eval_RMSE(fed_model, local_test_data)/EC_mean_test)" 1481 | ], 1482 | "execution_count": null, 1483 | "outputs": [ 1484 | { 1485 | "output_type": "stream", 1486 | "text": [ 1487 | "trained_model test CVRMSE = 0.5329551817940137\n" 1488 | ], 1489 | "name": "stdout" 1490 | } 1491 | ] 1492 | }, 1493 | { 1494 | "cell_type": "code", 1495 | "metadata": { 1496 | "id": "4Dp8RdBL-ndy", 1497 | "colab_type": "code", 1498 | "colab": { 1499 | "base_uri": "https://localhost:8080/", 1500 | "height": 50 1501 | }, 1502 | "outputId": "8de5a649-aec9-40ff-9c06-62cc17e91ecd" 1503 | }, 1504 | "source": [ 1505 | "print('initial_model test CVRMSE =', local_eval_CVRMSE(initial_model, local_test_data))\n", 1506 | "print('trained_model test CVRMSE =', local_eval_CVRMSE(fed_model, local_test_data))" 1507 | ], 1508 | "execution_count": null, 1509 | "outputs": [ 1510 | { 1511 | "output_type": "stream", 1512 | "text": [ 1513 | "initial_model test CVRMSE = 50.99831\n", 1514 | "trained_model test CVRMSE = 0.5306189\n" 1515 | ], 1516 | "name": "stdout" 1517 | } 1518 | ] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "metadata": { 1523 | "id": "uuRbWu7sHMxo", 1524 | "colab_type": "code", 1525 | "colab": { 1526 | "base_uri": "https://localhost:8080/", 1527 | "height": 672 1528 | }, 1529 | "outputId": "f7e2b63c-41a4-47e5-a559-0f012eff5f5f" 1530 | }, 1531 | "source": [ 1532 | "local_model = fed_model\n", 1533 | "learning_rate = 0.1\n", 1534 | "loss = []\n", 1535 | "for round_num in range(50):\n", 1536 | " local_model = local_train(local_model, learning_rate, local_train_data)\n", 1537 | " learning_rate = learning_rate * 0.9\n", 1538 | " loss_ = local_eval(local_model, local_train_data)\n", 1539 | " RMSE = local_eval_RMSE(local_model, local_train_data)\n", 1540 | " CVRMSE = local_eval_CVRMSE(local_model, local_train_data)\n", 1541 | " loss.append(loss_) \n", 1542 | " if len(loss) > 1:\n", 1543 | " if ((loss[-2]-loss[-1]) / loss[-2] < 0.0001):\n", 1544 | " print('Train process is done')\n", 1545 | " break\n", 1546 | " print('round {}, loss={}, RMSE={}, CVRMSE={}'.format(round_num, loss_, RMSE, CVRMSE))" 1547 | ], 1548 | "execution_count": null, 1549 | "outputs": [ 1550 | { 1551 | "output_type": "stream", 1552 | "text": [ 1553 | "round 0, loss=0.023509668186306953, RMSE=20.703439712524414, CVRMSE=0.3473283648490906\n", 1554 | "round 1, loss=0.011748472228646278, RMSE=14.635294914245605, CVRMSE=0.24552641808986664\n", 1555 | "round 2, loss=0.006828448735177517, RMSE=11.1571683883667, CVRMSE=0.1871756911277771\n", 1556 | "round 3, loss=0.004589683376252651, RMSE=9.146533012390137, CVRMSE=0.15344424545764923\n", 1557 | "round 4, loss=0.0034926123917102814, RMSE=7.9782562255859375, CVRMSE=0.13384458422660828\n", 1558 | "round 5, loss=0.002918635029345751, RMSE=7.2927751541137695, CVRMSE=0.12234457582235336\n", 1559 | "round 6, loss=0.0026004172395914793, RMSE=6.883371829986572, CVRMSE=0.11547618359327316\n", 1560 | "round 7, loss=0.0024146903306245804, RMSE=6.632757663726807, CVRMSE=0.11127173900604248\n", 1561 | "round 8, loss=0.0023012273013591766, RMSE=6.474887847900391, CVRMSE=0.10862323641777039\n", 1562 | "round 9, loss=0.002229036297649145, RMSE=6.372414588928223, CVRMSE=0.10690408945083618\n", 1563 | "round 10, loss=0.002181412884965539, RMSE=6.303908348083496, CVRMSE=0.1057547926902771\n", 1564 | "round 11, loss=0.0021489672362804413, RMSE=6.256810188293457, CVRMSE=0.10496465861797333\n", 1565 | "round 12, loss=0.0021262140944600105, RMSE=6.22357177734375, CVRMSE=0.10440704971551895\n", 1566 | "round 13, loss=0.0021098428405821323, RMSE=6.199549198150635, CVRMSE=0.10400402545928955\n", 1567 | "round 14, loss=0.002097789663821459, RMSE=6.181804656982422, CVRMSE=0.10370634496212006\n", 1568 | "round 15, loss=0.0020887325517833233, RMSE=6.168438911437988, CVRMSE=0.10348211228847504\n", 1569 | "round 16, loss=0.0020817983895540237, RMSE=6.1581878662109375, CVRMSE=0.10331013798713684\n", 1570 | "round 17, loss=0.0020764009095728397, RMSE=6.1501970291137695, CVRMSE=0.10317609459161758\n", 1571 | "round 18, loss=0.0020721382461488247, RMSE=6.1438798904418945, CVRMSE=0.10307011008262634\n", 1572 | "round 19, loss=0.0020687258802354336, RMSE=6.138818740844727, CVRMSE=0.1029852032661438\n", 1573 | "round 20, loss=0.0020659619476646185, RMSE=6.134716033935547, CVRMSE=0.10291637480258942\n", 1574 | "round 21, loss=0.0020636990666389465, RMSE=6.1313557624816895, CVRMSE=0.10286000370979309\n", 1575 | "round 22, loss=0.0020618289709091187, RMSE=6.12857723236084, CVRMSE=0.10281339287757874\n", 1576 | "round 23, loss=0.0020602697040885687, RMSE=6.126260280609131, CVRMSE=0.10277451574802399\n", 1577 | "round 24, loss=0.0020589600317180157, RMSE=6.1243133544921875, CVRMSE=0.10274185240268707\n", 1578 | "round 25, loss=0.0020578517578542233, RMSE=6.1226654052734375, CVRMSE=0.10271421074867249\n", 1579 | "round 26, loss=0.0020569078624248505, RMSE=6.1212615966796875, CVRMSE=0.1026906669139862\n", 1580 | "round 27, loss=0.002056100405752659, RMSE=6.120060443878174, CVRMSE=0.10267052054405212\n", 1581 | "round 28, loss=0.0020554051734507084, RMSE=6.119026184082031, CVRMSE=0.10265316814184189\n", 1582 | "round 29, loss=0.0020548044703900814, RMSE=6.118132591247559, CVRMSE=0.10263817757368088\n", 1583 | "round 30, loss=0.0020542836282402277, RMSE=6.11735725402832, CVRMSE=0.10262517631053925\n", 1584 | "round 31, loss=0.002053829375654459, RMSE=6.116681098937988, CVRMSE=0.10261382907629013\n", 1585 | "round 32, loss=0.002053432399407029, RMSE=6.116090774536133, CVRMSE=0.10260392725467682\n", 1586 | "round 33, loss=0.0020530850160866976, RMSE=6.115573883056641, CVRMSE=0.1025952473282814\n", 1587 | "round 34, loss=0.002052778610959649, RMSE=6.115118026733398, CVRMSE=0.10258759558200836\n", 1588 | "round 35, loss=0.002052509691566229, RMSE=6.114717483520508, CVRMSE=0.10258088260889053\n", 1589 | "round 36, loss=0.0020522731356322765, RMSE=6.114365577697754, CVRMSE=0.10257497429847717\n", 1590 | "round 37, loss=0.0020520624238997698, RMSE=6.114051818847656, CVRMSE=0.10256971418857574\n", 1591 | "Train process is done\n" 1592 | ], 1593 | "name": "stdout" 1594 | } 1595 | ] 1596 | }, 1597 | { 1598 | "cell_type": "code", 1599 | "metadata": { 1600 | "id": "w_htwSeFasdf", 1601 | "colab_type": "code", 1602 | "colab": { 1603 | "base_uri": "https://localhost:8080/", 1604 | "height": 50 1605 | }, 1606 | "outputId": "168800c7-32d2-43f2-8767-5bc8ae2c29ed" 1607 | }, 1608 | "source": [ 1609 | "print('initial_model test RMSE =', local_eval_RMSE(initial_model, local_test_data))\n", 1610 | "print('local_model test RMSE =', local_eval_RMSE(local_model, local_test_data))" 1611 | ], 1612 | "execution_count": null, 1613 | "outputs": [ 1614 | { 1615 | "output_type": "stream", 1616 | "text": [ 1617 | "initial_model test RMSE = 3072.248\n", 1618 | "local_model test RMSE = 5.2883606\n" 1619 | ], 1620 | "name": "stdout" 1621 | } 1622 | ] 1623 | }, 1624 | { 1625 | "cell_type": "code", 1626 | "metadata": { 1627 | "id": "_v5o4lXR8Dy1", 1628 | "colab_type": "code", 1629 | "colab": { 1630 | "base_uri": "https://localhost:8080/", 1631 | "height": 34 1632 | }, 1633 | "outputId": "96f3451e-88d0-4f3e-a5ab-f6b55d596a0e" 1634 | }, 1635 | "source": [ 1636 | "print('local_model test CVRMSE =', local_eval_RMSE(local_model, local_test_data)/EC_mean_test)" 1637 | ], 1638 | "execution_count": null, 1639 | "outputs": [ 1640 | { 1641 | "output_type": "stream", 1642 | "text": [ 1643 | "local_model test CVRMSE = 0.0881561821702034\n" 1644 | ], 1645 | "name": "stdout" 1646 | } 1647 | ] 1648 | }, 1649 | { 1650 | "cell_type": "code", 1651 | "metadata": { 1652 | "id": "SAut4kt0XZd9", 1653 | "colab_type": "code", 1654 | "colab": { 1655 | "base_uri": "https://localhost:8080/", 1656 | "height": 50 1657 | }, 1658 | "outputId": "b0f5bbcd-41f7-43d7-cd1a-2a5056f21283" 1659 | }, 1660 | "source": [ 1661 | "print('initial_model test CVRMSE =', local_eval_CVRMSE(initial_model, local_test_data))\n", 1662 | "print('local_model test CVRMSE =', local_eval_CVRMSE(local_model, local_test_data))" 1663 | ], 1664 | "execution_count": null, 1665 | "outputs": [ 1666 | { 1667 | "output_type": "stream", 1668 | "text": [ 1669 | "initial_model test CVRMSE = 50.99831\n", 1670 | "local_model test CVRMSE = 0.087783575\n" 1671 | ], 1672 | "name": "stdout" 1673 | } 1674 | ] 1675 | } 1676 | ] 1677 | } --------------------------------------------------------------------------------