analytical_derivative_n1.py 31.5 KB
Newer Older
1
# Vergleich: Berechnung von Gradient und Hesse-Matrix mit AD vs. analytische Ableitung per Hand berechnet
2
# Kernfunktionalität: grad_and_hesse_matrix, berechnet die analytischen Ableitungen
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

import numpy as np
from numpy.linalg import norm
import tensorflow as tf
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

from generate_dataset import generate_tictactoe


# Aktivierungsfunktionen und Ableitungen 

def tau(x):
        return 1/(1+ np.exp(-x)) # sigmoid

def ddx_tau(x):
    return sigma(x) * (1 - sigma(x)) # komponentenweise Ableitung von sigmoid

def sigma(x):
    return 1/(1+ np.exp(-x)) # sigmoid

def ddx_sigma(x):
    return sigma(x) * (1 - sigma(x)) # komponentenweise Ableitung von sigmoid

def ddx2_sigma(x):
    return ddx_sigma(x) * (1 - 2*sigma(x))

def ddx2_tau(x):
    return ddx_tau(x) * (1 - 2*tau(x))


#HELPERS

def d_W_1_d_W_1(k, i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    ddthetak_ddthetai_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    x_i = np.reshape(train_set[:,i], (126,1))
    x_k = np.reshape(train_set[:,k], (126,1))
41
    temp = theta_11*x_i*(x_k*ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_11+theta_14)+(ddx_sigma(z_1))**2*theta_11*x_k*ddx2_tau(sigma(z_1)*theta_11+theta_14))
42
    ddthetak_ddthetai_f_x_j[:,0] = np.reshape(temp, (126,))
43
    temp = theta_12*x_i*(x_k*ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_12+theta_15)+(ddx_sigma(z_1))**2*theta_12*x_k*ddx2_tau(sigma(z_1)*theta_12+theta_15))
44
    ddthetak_ddthetai_f_x_j[:,1] = np.reshape(temp, (126,))
45
    temp = theta_13*x_i*(x_k*ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_13+theta_16)+ddx_sigma(z_1)**2*theta_13*x_k*ddx2_tau(sigma(z_1)*theta_13+theta_16) )
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    ddthetak_ddthetai_f_x_j[:,2] = np.reshape(temp, (126,))
    return ddthetak_ddthetai_f_x_j

def d_b_1_d_w_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta10_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = theta_11*x_i*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_11+theta_14)+theta_11*(ddx_sigma(z_1)**2)*ddx2_tau(sigma(z_1)*theta_11+theta_14))
    d_dtheta10_d_dthetai_f_x_j[:,0] = np.reshape(temp, (126,))
    temp = theta_12*x_i*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_12+theta_15)+theta_12*(ddx_sigma(z_1)**2)*ddx2_tau(sigma(z_1)*theta_12+theta_15))
    d_dtheta10_d_dthetai_f_x_j[:,1] = np.reshape(temp, (126,))
    temp = theta_13*x_i*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_13+theta_16)+theta_13*(ddx_sigma(z_1)**2)*ddx2_tau(sigma(z_1)*theta_13+theta_16))
    d_dtheta10_d_dthetai_f_x_j[:,2] = np.reshape(temp, (126,))
    return d_dtheta10_d_dthetai_f_x_j

def d_theta11_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta11_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = x_i * ddx_sigma(z_1) * (ddx_tau(sigma(z_1) * theta_11 + theta_14) + theta_11 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_11 + theta_14))
    d_dtheta11_d_dthetai_f_x_j[:,0] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta11_d_dthetai_f_x_j

def d_theta12_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta12_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = x_i * ddx_sigma(z_1) * (ddx_tau(sigma(z_1) * theta_12 + theta_15) + theta_12 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_12 + theta_15))
    d_dtheta12_d_dthetai_f_x_j[:,1] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta12_d_dthetai_f_x_j

def d_theta13_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta13_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = x_i * ddx_sigma(z_1) * (ddx_tau(sigma(z_1) * theta_13 + theta_16) + theta_13 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_13 + theta_16))
    d_dtheta13_d_dthetai_f_x_j[:,2] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta13_d_dthetai_f_x_j

def d_theta14_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta14_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = theta_11 * x_i * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_11 + theta_14)
    d_dtheta14_d_dthetai_f_x_j[:,0] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta14_d_dthetai_f_x_j

def d_theta15_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta15_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = theta_12 * x_i * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_12 + theta_15)
    d_dtheta15_d_dthetai_f_x_j[:,1] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta15_d_dthetai_f_x_j

def d_theta16_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16):
    d_dtheta16_d_dthetai_f_x_j = np.zeros((126,3))
    x_i = np.reshape(train_set[:,i], (126,1))
    temp = theta_13 * x_i * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_13 + theta_16)
    d_dtheta16_d_dthetai_f_x_j[:,2] = np.reshape(temp, (126,)) # andere Komponenten = 0
    return d_dtheta16_d_dthetai_f_x_j


### ------------------------ Ableitungen analytisch berechnet ------------------------------------ ###
def grad_and_hesse_matrix(model, train_set, train_labels):
    weights_and_biases_list = model.get_weights()
    W_1 = weights_and_biases_list[0]
    b_1 = weights_and_biases_list[1]
    W_2 = weights_and_biases_list[2]
    b_2 = weights_and_biases_list[3]

    loss_fn = tf.keras.losses.MeanSquaredError()
    model.compile(optimizer='adam', loss=loss_fn)
    loss_keras = loss_fn(train_labels, model.predict(train_set))
    
    # Berechnung des Losses (zur Kontrolle)
    z_1 = train_set @ W_1 + b_1
    a_2 = sigma(z_1)
    z_2 = sigma(z_1) @ W_2 + b_2
    a_3 = tau(z_2)
    f_x = tau(z_2)

    # Mean squared error
    local_loss = 1/3* norm(f_x - train_labels, ord=2, axis=1)**2
    loss_hand = 1/126 * np.sum(local_loss)

    print("Keras berechnet als Loss: ", loss_keras.numpy(), "Per Hand: ", loss_hand)


    ### ----------------------------     GRADIENT    -------------------------------  ####
    #                   d/dtheta_1 C(theta)
    #                   d/dtheta_2 C(theta)  
    #                           .
    # gradient_hand =           .
    #                           .
    #                   d/dtheta_16 C(theta)
    #


    # Speicher für den Gradienten und die partiellen Ableitungen des KNN nach den thetas 
    gradient_hand = np.zeros((16,))
    d_dtheta_f_x_j = np.zeros((16,126,3))

    x_1 = np.reshape(train_set[:,0], (126,1))
    x_2 = np.reshape(train_set[:,1], (126,1))
    x_3 = np.reshape(train_set[:,2], (126,1))
    x_4 = np.reshape(train_set[:,3], (126,1))
    x_5 = np.reshape(train_set[:,4], (126,1))
    x_6 = np.reshape(train_set[:,5], (126,1))
    x_7 = np.reshape(train_set[:,6], (126,1))
    x_8 = np.reshape(train_set[:,7], (126,1))
    x_9 = np.reshape(train_set[:,8], (126,1))

    theta_1 = W_1[0,0]
    theta_2 = W_1[1,0]
    theta_3 = W_1[2,0]
    theta_4 = W_1[3,0]
    theta_5 = W_1[4,0]
    theta_6 = W_1[5,0]
    theta_7 = W_1[6,0]
    theta_8 = W_1[7,0]
    theta_9 = W_1[8,0]
    theta_10 = b_1[0]
    theta_11 = W_2[0,0]
    theta_12 = W_2[0,1]
    theta_13 = W_2[0,2]
    theta_14 = b_2[0]
    theta_15 = b_2[1]
    theta_16 = b_2[2]

    # Ableitung nach theta_1
    d_dtheta1_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta1_f_x_j[:,0] = np.reshape(theta_11 * x_1 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta1_f_x_j[:,1] = np.reshape(theta_12 * x_1 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta1_f_x_j[:,2] = np.reshape(theta_13 * x_1 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[0,:,:] = d_dtheta1_f_x_j

    d_dtheta1_C_j_summanden = 2* d_dtheta1_f_x_j * (f_x - train_labels)
    gradient_hand[0] = 1/(126*3) * np.sum(d_dtheta1_C_j_summanden)

    # Ableitung nach theta_2
    d_dtheta2_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta2_f_x_j[:,0] = np.reshape(theta_11 * x_2 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta2_f_x_j[:,1] = np.reshape(theta_12 * x_2 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta2_f_x_j[:,2] = np.reshape(theta_13 * x_2 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[1,:,:] = d_dtheta2_f_x_j

    d_dtheta2_C_j_summanden = 2* d_dtheta2_f_x_j * (f_x - train_labels)
    gradient_hand[1] = 1/(126*3) * np.sum(d_dtheta2_C_j_summanden)

    # Ableitung nach theta_3
    d_dtheta3_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta3_f_x_j[:,0] = np.reshape(theta_11 * x_3 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta3_f_x_j[:,1] = np.reshape(theta_12 * x_3 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta3_f_x_j[:,2] = np.reshape(theta_13 * x_3 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[2,:,:] = d_dtheta3_f_x_j

    d_dtheta3_C_j_summanden = 2* d_dtheta3_f_x_j * (f_x - train_labels)
    gradient_hand[2] = 1/(126*3) * np.sum(d_dtheta3_C_j_summanden)

    # Ableitung nach theta_4
    d_dtheta4_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta4_f_x_j[:,0] = np.reshape(theta_11 * x_4 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta4_f_x_j[:,1] = np.reshape(theta_12 * x_4 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta4_f_x_j[:,2] = np.reshape(theta_13 * x_4 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[3,:,:] = d_dtheta4_f_x_j

    d_dtheta4_C_j_summanden = 2* d_dtheta4_f_x_j * (f_x - train_labels)
    gradient_hand[3] = 1/(126*3) * np.sum(d_dtheta4_C_j_summanden)

    # Ableitung nach theta_5
    d_dtheta5_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta5_f_x_j[:,0] = np.reshape(theta_11 * x_5 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta5_f_x_j[:,1] = np.reshape(theta_12 * x_5 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta5_f_x_j[:,2] = np.reshape(theta_13 * x_5 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[4,:,:] = d_dtheta5_f_x_j

    d_dtheta5_C_j_summanden = 2* d_dtheta5_f_x_j * (f_x - train_labels)
    gradient_hand[4] = 1/(126*3) * np.sum(d_dtheta5_C_j_summanden)

    # Ableitung nach theta_6
    d_dtheta6_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta6_f_x_j[:,0] = np.reshape(theta_11 * x_6 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta6_f_x_j[:,1] = np.reshape(theta_12 * x_6 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta6_f_x_j[:,2] = np.reshape(theta_13 * x_6 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[5,:,:] = d_dtheta6_f_x_j

    d_dtheta6_C_j_summanden = 2* d_dtheta6_f_x_j * (f_x - train_labels)
    gradient_hand[5] = 1/(126*3) * np.sum(d_dtheta6_C_j_summanden)

    # Ableitung nach theta_7
    d_dtheta7_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta7_f_x_j[:,0] = np.reshape(theta_11 * x_7 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta7_f_x_j[:,1] = np.reshape(theta_12 * x_7 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta7_f_x_j[:,2] = np.reshape(theta_13 * x_7 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[6,:,:] = d_dtheta7_f_x_j

    d_dtheta7_C_j_summanden = 2* d_dtheta7_f_x_j * (f_x - train_labels)
    gradient_hand[6] = 1/(126*3) * np.sum(d_dtheta7_C_j_summanden)

    # Ableitung nach theta_8
    d_dtheta8_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta8_f_x_j[:,0] = np.reshape(theta_11 * x_8 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta8_f_x_j[:,1] = np.reshape(theta_12 * x_8 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta8_f_x_j[:,2] = np.reshape(theta_13 * x_8 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[7,:,:] = d_dtheta8_f_x_j

    d_dtheta8_C_j_summanden = 2* d_dtheta8_f_x_j * (f_x - train_labels)
    gradient_hand[7] = 1/(126*3) * np.sum(d_dtheta8_C_j_summanden)

    # Ableitung nach theta_9
    d_dtheta9_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta9_f_x_j[:,0] = np.reshape(theta_11 * x_9 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,))
    d_dtheta9_f_x_j[:,1] = np.reshape(theta_12 * x_9 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,))
    d_dtheta9_f_x_j[:,2] = np.reshape(theta_13 * x_9 * ddx_sigma(z_1) * ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[8,:,:] = d_dtheta9_f_x_j

    d_dtheta9_C_j_summanden = 2* d_dtheta9_f_x_j * (f_x - train_labels)
    gradient_hand[8] = 1/(126*3) * np.sum(d_dtheta9_C_j_summanden)

    # Ableitung nach theta_10
    d_dtheta10_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta10_f_x_j[:,0] = np.reshape(theta_11 * ddx_sigma(z_1) * ddx_tau(sigma(z_1) * theta_11 + theta_14), (126,))
    d_dtheta10_f_x_j[:,1] = np.reshape(theta_12 * ddx_sigma(z_1) * ddx_tau(sigma(z_1) * theta_12 + theta_15), (126,))
    d_dtheta10_f_x_j[:,2] = np.reshape(theta_13 * ddx_sigma(z_1) * ddx_tau(sigma(z_1) * theta_13 + theta_16), (126,))

    d_dtheta_f_x_j[9,:,:] = d_dtheta10_f_x_j

    d_dtheta10_C_j_summanden = 2* d_dtheta10_f_x_j * (f_x - train_labels)
    gradient_hand[9] = 1/(126*3) * np.sum(d_dtheta10_C_j_summanden)

    # Ableitung nach theta_11
    d_dtheta11_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta11_f_x_j[:,0] = np.reshape(sigma(z_1)* ddx_tau(sigma(z_1)*theta_11 + theta_14), (126,)) # andere Komponenten sind 0

    d_dtheta_f_x_j[10,:,:] = d_dtheta11_f_x_j

    d_dtheta11_C_j_summanden = 2* d_dtheta11_f_x_j * (f_x - train_labels)
    gradient_hand[10] = 1/(126*3) * np.sum(d_dtheta11_C_j_summanden)

    # Ableitung nach theta_12
    d_dtheta12_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta12_f_x_j[:,1] = np.reshape(sigma(z_1)* ddx_tau(sigma(z_1)*theta_12 + theta_15), (126,)) # andere Komponenten sind 0

    d_dtheta_f_x_j[11,:,:] = d_dtheta12_f_x_j

    d_dtheta12_C_j_summanden = 2* d_dtheta12_f_x_j * (f_x - train_labels)
    gradient_hand[11] = 1/(126*3) * np.sum(d_dtheta12_C_j_summanden)

    # Ableitung nach theta_13
    d_dtheta13_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta13_f_x_j[:,2] = np.reshape(sigma(z_1)* ddx_tau(sigma(z_1)*theta_13 + theta_16), (126,)) #andere Komponenten sind 0

    d_dtheta_f_x_j[12,:,:] = d_dtheta13_f_x_j

    d_dtheta13_C_j_summanden = 2* d_dtheta13_f_x_j * (f_x - train_labels)
    gradient_hand[12] = 1/(126*3) * np.sum(d_dtheta13_C_j_summanden)

    # Ableitung nach theta_14
    d_dtheta14_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta14_f_x_j[:,0] = np.reshape(ddx_tau(sigma(z_1) * theta_11 + theta_14), (126,)) #andere Komponenten sind 0

    d_dtheta_f_x_j[13,:,:] = d_dtheta14_f_x_j

    d_dtheta14_C_j_summanden = 2* d_dtheta14_f_x_j * (f_x - train_labels)
    gradient_hand[13] = 1/(126*3) * np.sum(d_dtheta14_C_j_summanden)

    # Ableitung nach theta_15
    d_dtheta15_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta15_f_x_j[:,1] = np.reshape(ddx_tau(sigma(z_1) * theta_12 + theta_15), (126,)) #andere Komponenten sind 0

    d_dtheta_f_x_j[14,:,:] = d_dtheta15_f_x_j

    d_dtheta15_C_j_summanden = 2* d_dtheta15_f_x_j * (f_x - train_labels)
    gradient_hand[14] = 1/(126*3) * np.sum(d_dtheta15_C_j_summanden)

    # Ableitung nach theta_16
    d_dtheta16_f_x_j = np.zeros((126,3)) # Zeilen: Datenpunkte x_j, Spalten: drei Komponenten für den lokalen Loss
    d_dtheta16_f_x_j[:,2] = np.reshape(ddx_tau(sigma(z_1) * theta_13 + theta_16), (126,)) #andere Komponenten sind 0

    d_dtheta_f_x_j[15,:,:] = d_dtheta16_f_x_j

    d_dtheta16_C_j_summanden = 2* d_dtheta16_f_x_j * (f_x - train_labels)
    gradient_hand[15] = 1/(126*3) * np.sum(d_dtheta16_C_j_summanden)


    ### ----------------------------     HESSE-MATRIX   -------------------------------  ####

    # Blockweise Berechnung der Hesse-Matrix

    # W_1, W_1 todo fixen
    hessian_hand = np.zeros((16,16))

    for i in range(9):
        for k in range(9):
            ddthetak_ddthetai_f_x_j = d_W_1_d_W_1(k, i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
            ddthetak_ddthetai_C_j_summanden = 2 * (ddthetak_ddthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[k,:,:]) 
            C = 1/(126*3) * np.sum(ddthetak_ddthetai_C_j_summanden)
            hessian_hand[i,k] = C
            hessian_hand[k,i] = C

    # b_1, W_1 (läuft)

    for i in range(9):
        ddtheta10_ddthetai_f_x_j = d_b_1_d_w_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        ddtheta10_ddthetai_C_j_summanden = 2 * (ddtheta10_ddthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[9,:,:]) 
        C = 1/(126*3) * np.sum(ddtheta10_ddthetai_C_j_summanden)
        hessian_hand[i,9] = C
        hessian_hand[9,i] = C

    # b_1, b_1 (läuft)
    d_dtheta10_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = theta_11*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_11+theta_14) + theta_11 * (ddx_sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_11+theta_14))
    d_dtheta10_d_dtheta10_f_x_j[:,0] = np.reshape(temp, (126,))
    temp = theta_12*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_12+theta_15) + theta_12 * (ddx_sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_12+theta_15))
    d_dtheta10_d_dtheta10_f_x_j[:,1] = np.reshape(temp, (126,))
    temp = theta_13*(ddx2_sigma(z_1)*ddx_tau(sigma(z_1)*theta_13+theta_16) + theta_13 * (ddx_sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_13+theta_16))
    d_dtheta10_d_dtheta10_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta10_ddtheta10_C_j_summanden = 2 * (d_dtheta10_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[9,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta10_ddtheta10_C_j_summanden)
    hessian_hand[9,9] = C

    # W_2, W_1 (läuft)

    for i in range(9):
        d_dtheta11_d_dthetai_f_x_j = d_theta11_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta11_d_dthetai_C_j_summanden = 2 * (d_dtheta11_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[10,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta11_d_dthetai_C_j_summanden)
        hessian_hand[i,10] = C
        hessian_hand[10,i] = C

    for i in range(9):
        d_dtheta12_d_dthetai_f_x_j = d_theta12_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta12_d_dthetai_C_j_summanden = 2 * (d_dtheta12_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[11,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta12_d_dthetai_C_j_summanden)
        hessian_hand[i,11] = C
        hessian_hand[11,i] = C


    for i in range(9):
        d_dtheta13_d_dthetai_f_x_j = d_theta13_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta13_d_dthetai_C_j_summanden = 2 * (d_dtheta13_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[12,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta13_d_dthetai_C_j_summanden)
        hessian_hand[i,12] = C
        hessian_hand[12,i] = C

    # W_2, b_1 (läuft)
    d_dtheta11_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = ddx_sigma(z_1)*(ddx_tau(sigma(z_1)*theta_11+theta_14) + theta_11 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_11+theta_14))
    d_dtheta11_d_dtheta10_f_x_j[:,0] = np.reshape(temp, (126,))

    ddtheta11_ddtheta10_C_j_summanden = 2 * (d_dtheta11_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[10,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta11_ddtheta10_C_j_summanden)
    hessian_hand[10,9] = C
    hessian_hand[9,10] = C

    d_dtheta12_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = ddx_sigma(z_1)*(ddx_tau(sigma(z_1)*theta_12+theta_15) + theta_12 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_12+theta_15))
    d_dtheta12_d_dtheta10_f_x_j[:,1] = np.reshape(temp, (126,))

    ddtheta12_ddtheta10_C_j_summanden = 2 * (d_dtheta12_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[11,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta12_ddtheta10_C_j_summanden)
    hessian_hand[11,9] = C
    hessian_hand[9,11] = C

    d_dtheta13_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = ddx_sigma(z_1)*(ddx_tau(sigma(z_1)*theta_13+theta_16) + theta_13 * sigma(z_1) * ddx2_tau(sigma(z_1)*theta_13+theta_16))
    d_dtheta13_d_dtheta10_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta13_ddtheta10_C_j_summanden = 2 * (d_dtheta13_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[12,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta13_ddtheta10_C_j_summanden)
    hessian_hand[12,9] = C
    hessian_hand[9,12] = C

    # W_2, W_2 (läuft)
    d_dtheta11_d_dtheta11_f_x_j = np.zeros((126,3))
    temp = (sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_11 + theta_14)
    d_dtheta11_d_dtheta11_f_x_j[:,0] = np.reshape(temp, (126,))

    ddtheta11_ddtheta11_C_j_summanden = 2 * (d_dtheta11_d_dtheta11_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[10,:,:] * d_dtheta_f_x_j[10,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta11_ddtheta11_C_j_summanden)
    hessian_hand[10,10] = C

    d_dtheta12_d_dtheta12_f_x_j = np.zeros((126,3))
    temp = (sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_12 + theta_15)
    d_dtheta12_d_dtheta12_f_x_j[:,1] = np.reshape(temp, (126,))

    ddtheta12_ddtheta12_C_j_summanden = 2 * (d_dtheta12_d_dtheta12_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[11,:,:] * d_dtheta_f_x_j[11,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta12_ddtheta12_C_j_summanden)
    hessian_hand[11,11] = C

    d_dtheta13_d_dtheta13_f_x_j = np.zeros((126,3))
    temp = (sigma(z_1))**2 * ddx2_tau(sigma(z_1)*theta_13 + theta_16)
    d_dtheta13_d_dtheta13_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta13_ddtheta13_C_j_summanden = 2 * (d_dtheta13_d_dtheta13_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[12,:,:] * d_dtheta_f_x_j[12,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta13_ddtheta13_C_j_summanden)
    hessian_hand[12,12] = C

    # alle gemischten Ableitungen im Block W_1, W_1 sind 0, somit sind auch alle Einträge der Hesse-Matrix = 0

    # b_2, W_1 (läuft)

    for i in range(9):
        d_dtheta14_d_dthetai_f_x_j = d_theta14_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta14_d_dthetai_C_j_summanden = 2 * (d_dtheta14_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[13,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta14_d_dthetai_C_j_summanden)
        hessian_hand[i,13] = C
        hessian_hand[13,i] = C


    for i in range(9):
        d_dtheta15_d_dthetai_f_x_j = d_theta15_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta15_d_dthetai_C_j_summanden = 2 * (d_dtheta15_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[14,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta15_d_dthetai_C_j_summanden)
        hessian_hand[i,14] = C
        hessian_hand[14,i] = C


    for i in range(9):
        d_dtheta16_d_dthetai_f_x_j =  d_theta16_d_W_1(i, train_set, z_1, theta_11, theta_12, theta_13, theta_14, theta_15, theta_16)
        d_dtheta16_d_dthetai_C_j_summanden = 2 * (d_dtheta16_d_dthetai_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[i,:,:] * d_dtheta_f_x_j[15,:,:]) 
        C = 1/(126*3) * np.sum(d_dtheta16_d_dthetai_C_j_summanden)
        hessian_hand[i,15] = C
        hessian_hand[15,i] = C

    # b_2, b_1 (läuft)
    d_dtheta14_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = theta_11 * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_11 + theta_14)
    d_dtheta14_d_dtheta10_f_x_j[:,0] = np.reshape(temp, (126,))

    ddtheta14_ddtheta10_C_j_summanden = 2 * (d_dtheta14_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[13,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta14_ddtheta10_C_j_summanden)
    hessian_hand[13,9] = C
    hessian_hand[9,13] = C

    d_dtheta15_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = theta_12 * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_12 + theta_15)
    d_dtheta15_d_dtheta10_f_x_j[:,1] = np.reshape(temp, (126,))

    ddtheta15_ddtheta10_C_j_summanden = 2 * (d_dtheta15_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[14,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta15_ddtheta10_C_j_summanden)
    hessian_hand[14,9] = C
    hessian_hand[9,14] = C

    d_dtheta16_d_dtheta10_f_x_j = np.zeros((126,3))
    temp = theta_13 * ddx_sigma(z_1) * ddx2_tau(sigma(z_1)*theta_13 + theta_16)
    d_dtheta16_d_dtheta10_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta16_ddtheta10_C_j_summanden = 2 * (d_dtheta16_d_dtheta10_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[15,:,:] * d_dtheta_f_x_j[9,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta16_ddtheta10_C_j_summanden)
    hessian_hand[15,9] = C
    hessian_hand[9,15] = C

    #b_2, W_2
    d_dtheta14_d_dtheta11_f_x_j = np.zeros((126,3))
    temp = sigma(z_1) * ddx2_tau(sigma(z_1)*theta_11 + theta_14)
    d_dtheta14_d_dtheta11_f_x_j[:,0] = np.reshape(temp, (126,))

    ddtheta14_ddtheta11_C_j_summanden = 2 * (d_dtheta14_d_dtheta11_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[13,:,:] * d_dtheta_f_x_j[10,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta14_ddtheta11_C_j_summanden)
    hessian_hand[13,10] = C
    hessian_hand[10,13] = C

    d_dtheta15_d_dtheta12_f_x_j = np.zeros((126,3))
    temp = sigma(z_1) * ddx2_tau(sigma(z_1)*theta_12 + theta_15)
    d_dtheta15_d_dtheta12_f_x_j[:,1] = np.reshape(temp, (126,))

    ddtheta15_ddtheta12_C_j_summanden = 2 * (d_dtheta15_d_dtheta12_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[14,:,:] * d_dtheta_f_x_j[11,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta15_ddtheta12_C_j_summanden)
    hessian_hand[14,11] = C
    hessian_hand[11,14] = C

    d_dtheta16_d_dtheta13_f_x_j = np.zeros((126,3))
    temp = sigma(z_1) * ddx2_tau(sigma(z_1)*theta_13 + theta_16)
    d_dtheta16_d_dtheta13_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta16_ddtheta13_C_j_summanden = 2 * (d_dtheta16_d_dtheta13_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[15,:,:] * d_dtheta_f_x_j[12,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta16_ddtheta13_C_j_summanden)
    hessian_hand[15,12] = C
    hessian_hand[12,15] = C

    #b_2, b_2
    d_dtheta14_d_dtheta14_f_x_j = np.zeros((126,3))
    temp = ddx2_tau(sigma(z_1)*theta_11 + theta_14)
    d_dtheta14_d_dtheta14_f_x_j[:,0] = np.reshape(temp, (126,))

    ddtheta14_ddtheta14_C_j_summanden = 2 * (d_dtheta14_d_dtheta14_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[13,:,:] * d_dtheta_f_x_j[13,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta14_ddtheta14_C_j_summanden)
    hessian_hand[13,13] = C

    d_dtheta15_d_dtheta15_f_x_j = np.zeros((126,3))
    temp = ddx2_tau(sigma(z_1)*theta_12 + theta_15)
    d_dtheta15_d_dtheta15_f_x_j[:,1] = np.reshape(temp, (126,))

    ddtheta15_ddtheta15_C_j_summanden = 2 * (d_dtheta15_d_dtheta15_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[14,:,:] * d_dtheta_f_x_j[14,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta15_ddtheta15_C_j_summanden)
    hessian_hand[14,14] = C

    d_dtheta16_d_dtheta16_f_x_j = np.zeros((126,3))
    temp = ddx2_tau(sigma(z_1)*theta_13 + theta_16)
    d_dtheta16_d_dtheta16_f_x_j[:,2] = np.reshape(temp, (126,))

    ddtheta16_ddtheta16_C_j_summanden = 2 * (d_dtheta16_d_dtheta16_f_x_j * (f_x - train_labels) + d_dtheta_f_x_j[15,:,:] * d_dtheta_f_x_j[15,:,:]) 
    C = 1/(126*3) * np.sum(ddtheta16_ddtheta16_C_j_summanden)
    hessian_hand[15,15] = C


    return gradient_hand, hessian_hand


def imshow_zero_center(image, title):
    lim = tf.reduce_max(abs(image))
    plt.imshow(image, vmin=-lim, vmax=lim, cmap='seismic')
    plt.title(title)
    plt.colorbar()
    plt.show()

if __name__ == "__main__":
    # Modell und Daten laden
    size_hidden_layer = 1

    from keras.models import Sequential
    from keras.layers import Dense

575
    tf.keras.backend.set_floatx('float64')
576
    model = Sequential()
577 578
    model.add(Dense(size_hidden_layer, input_dim = 9,activation='sigmoid'))
    model.add(Dense(3, input_dim=size_hidden_layer, activation='sigmoid'))
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    weights_and_biases_list = model.get_weights()
    W_1 = weights_and_biases_list[0]
    b_1 = weights_and_biases_list[1]
    W_2 = weights_and_biases_list[2]
    b_2 = weights_and_biases_list[3]

    train_set, train_labels = generate_tictactoe()
    dataset = tf.data.Dataset.from_tensor_slices((train_set, train_labels))

    loss_fn = tf.keras.losses.MeanSquaredError()
    model.compile(optimizer='adam', loss=loss_fn)
    loss_keras = loss_fn(train_labels, model.predict(train_set))

    # Gradient und Hesse-Matrix mittels Autodiff ermitteln
    layer1 = model.layers[0]
    layer2 = model.layers[1]
    x = train_set

    with tf.GradientTape() as t2:
        with tf.GradientTape() as t1:
            x = layer1(x)
            x = layer2(x)
            loss = loss_fn(train_labels, x)

        g = t1.gradient(loss, [layer1.kernel, layer1.bias, layer2.kernel, layer2.bias])
        grad = tf.concat([tf.reshape(g[0], [9*size_hidden_layer,1]), tf.reshape(g[1], [size_hidden_layer,1]), tf.reshape(g[2], [size_hidden_layer*3, 1]), tf.reshape(g[3], [3,1])], axis=0)

    h = t2.jacobian(grad, [layer1.kernel, layer1.bias, layer2.kernel, layer2.bias])

    n_params = tf.reduce_prod(layer1.kernel.shape) + tf.reduce_prod(layer2.kernel.shape) + tf.reduce_prod(layer1.bias.shape) + tf.reduce_prod(layer2.bias.shape)

    #h[0] ist die Ableitung des Gradienten nach den Gewichten Layer 1
    n_params_D_weights_1 = tf.reduce_prod(layer1.kernel.shape)
    H_weights_1 = tf.reshape(h[0], [n_params, n_params_D_weights_1])

    #h[1] ist die Ableitung des Gradienten nach den Biasen Layer 1
    n_params_D_bias_1 = tf.reduce_prod(layer1.bias.shape)
    H_bias_1 = tf.reshape(h[1], [n_params, n_params_D_bias_1])

    #h[2] ist die Ableitung des Gradienten nach den Gewichten Layer 2
    n_params_D_weights_2 = tf.reduce_prod(layer2.kernel.shape)
    H_weights_2 = tf.reshape(h[2], [n_params, n_params_D_weights_2])

    #h[3] ist die Ableitung des Gradienten nach den Biasen Layer 2
    n_params_D_bias_2 = tf.reduce_prod(layer2.bias.shape)
    H_bias_2 = tf.reshape(h[3], [n_params, n_params_D_bias_2])

    # Hesse-Matrix zusammensetzen ToDo vorher allokieren
    h_mat_keras = tf.concat([H_weights_1, H_bias_1, H_weights_2, H_bias_2], axis = 1)

629
    # Gradient & Hesse-Matrix wie per Hand berechnet
630 631
    gradient_hand, hessian_hand = grad_and_hesse_matrix(model, train_set, train_labels)

Eva Lina Fesefeldt's avatar
Eva Lina Fesefeldt committed
632

633
    # vergleichende Plots
634
    imshow_zero_center(hessian_hand - h_mat_keras.numpy(), "Hesse-Matrix n=1 analytisch vs. AD Absoluter Fehler")
Eva Lina Fesefeldt's avatar
Eva Lina Fesefeldt committed
635
    imshow_zero_center((hessian_hand - h_mat_keras.numpy())/h_mat_keras.numpy(), "Hesse-Matrix n=1 analytisch vs. AD Relativer Fehler")