Last active
June 21, 2020 09:55
-
-
Save irdanish11/8aed1fa158f2b42dcba3052c283a3b42 to your computer and use it in GitHub Desktop.
Gradient Tape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inputs = Input(shape=(7)) | |
| fc = Dense(units=72)(inputs) | |
| fc = Dense(units=1, activation='sigmoid')(fc) | |
| model = Model(inputs, fc) | |
| #generating data | |
| X = np.random.rand(1024, 7) | |
| #generating labels | |
| y = np.conacatenate(np.zeros((512)), np.ones((512))) | |
| optimizer = Adam() | |
| #Train the model | |
| for epoch in range(epochs): | |
| with tf.GradientTape() as tape: | |
| #getting predictions | |
| pred = model(X) | |
| #computing loss | |
| loss = your_loss_fun(y,pred) | |
| #calculate gardients | |
| gradients = tape.gradient(loss, model.trainable_variables) | |
| #Apply gradients and optimize | |
| optimizer.apply_gradients(zip(gradients, model.trainable_variables)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment