(self, X)
| 405 | |
| 406 | |
| 407 | def fit(self, X): |
| 408 | d_costs = [] |
| 409 | g_costs = [] |
| 410 | |
| 411 | N = len(X) |
| 412 | n_batches = N // BATCH_SIZE |
| 413 | total_iters = 0 |
| 414 | for i in range(EPOCHS): |
| 415 | print("epoch:", i) |
| 416 | np.random.shuffle(X) |
| 417 | for j in range(n_batches): |
| 418 | t0 = datetime.now() |
| 419 | |
| 420 | if type(X[0]) is str: |
| 421 | # is celeb dataset |
| 422 | batch = util.files2images( |
| 423 | X[j*BATCH_SIZE:(j+1)*BATCH_SIZE] |
| 424 | ) |
| 425 | |
| 426 | else: |
| 427 | # is mnist dataset |
| 428 | batch = X[j*BATCH_SIZE:(j+1)*BATCH_SIZE] |
| 429 | |
| 430 | Z = np.random.uniform(-1, 1, size=(BATCH_SIZE, self.latent_dims)) |
| 431 | |
| 432 | # train the discriminator |
| 433 | _, d_cost, d_acc = self.sess.run( |
| 434 | (self.d_train_op, self.d_cost, self.d_accuracy), |
| 435 | feed_dict={self.X: batch, self.Z: Z, self.batch_sz: BATCH_SIZE}, |
| 436 | ) |
| 437 | d_costs.append(d_cost) |
| 438 | |
| 439 | # train the generator |
| 440 | _, g_cost1 = self.sess.run( |
| 441 | (self.g_train_op, self.g_cost), |
| 442 | feed_dict={self.Z: Z, self.batch_sz: BATCH_SIZE}, |
| 443 | ) |
| 444 | # g_costs.append(g_cost1) |
| 445 | _, g_cost2 = self.sess.run( |
| 446 | (self.g_train_op, self.g_cost), |
| 447 | feed_dict={self.Z: Z, self.batch_sz: BATCH_SIZE}, |
| 448 | ) |
| 449 | g_costs.append((g_cost1 + g_cost2)/2) # just use the avg |
| 450 | |
| 451 | print(" batch: %d/%d - dt: %s - d_acc: %.2f" % (j+1, n_batches, datetime.now() - t0, d_acc)) |
| 452 | |
| 453 | |
| 454 | # save samples periodically |
| 455 | total_iters += 1 |
| 456 | if total_iters % SAVE_SAMPLE_PERIOD == 0: |
| 457 | print("saving a sample...") |
| 458 | samples = self.sample(64) # shape is (64, D, D, color) |
| 459 | |
| 460 | # for convenience |
| 461 | d = self.img_length |
| 462 | |
| 463 | if samples.shape[-1] == 1: |
| 464 | # if color == 1, we want a 2-D image (N x N) |
no test coverage detected