Skip to content

Instantly share code, notes, and snippets.

View NiloyPurkait's full-sized avatar
🎯
Focusing

NiloyPurkait

🎯
Focusing
View GitHub Profile
def discriminator_loss(real_output, fake_output):
loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=True)
real_loss = loss_object(tf.ones_like(real_output), real_output)
fake_loss = loss_object(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
def generator_loss(preds):
loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=True)
preds = tf.convert_to_tensor(preds, dtype=tf.float32)
loss = loss_object(tf.ones_like(preds, dtype=tf.float32), preds)
return loss
def train_step(inp, tar):
# targets shifted by 1 index position
tar_inp = tar[:, :-1]
tar_real = tar[:, 1:]
#Get encoding, combined and decoding masks
enc_padding_mask, combined_mask, dec_padding_mask = create_masks(inp, tar_inp)
# Initialize Generator and Discriminator gradient tapes
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
@NiloyPurkait
NiloyPurkait / MNIST_Keras2DML.py
Last active October 20, 2021 05:26
An example of using Apache SparkML to train a convolutional neural network in parallel using the MNIST dataset, on IBM watson studio. Written for medium article: https://medium.com/@niloypurkait/how-to-train-your-neural-networks-in-parallel-with-keras-and-apache-spark-ea8a3f48cae6
################################### Keras2DML: Parallely training neural network with SystemML#######################################
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Input, Dense, Conv1D, Conv2D, MaxPooling2D, Dropout,Flatten
from keras import backend as K
from keras.models import Model
import numpy as np
import matplotlib.pyplot as plt