This file contains 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
# Copyright (C) Xuechen Li | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
This file contains 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
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import tqdm | |
import os | |
import gzip | |
from absl import flags | |
import urllib.request as req |
This file contains 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
def tensor_square(x, stop_when): # x is scalar Tensor | |
cnt = 0 | |
while x < stop_when: | |
x = tf.square(x) | |
cnt += 1 | |
return cnt |
This file contains 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
def model_fn(features, labels, mode, params): | |
model = RevNet(params["hyperparameters"]) | |
if mode == tf.estimator.ModeKeys.TRAIN: | |
optimizer = tf.train.MomentumOptimizer(learning_rate, momentum) | |
logits, saved_hidden = model(features, training=True) | |
grads, loss = model.compute_gradients(saved_hidden, labels, training=True) | |
with tf.control_dependencies(model.get_updates_for(features)): | |
train_op = optimizer.apply_gradients(zip(grads, model.trainable_variables)) | |
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) |
This file contains 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
for image, label in dataset: | |
logits = model(image, training=True) | |
... |
This file contains 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
dataset = tf.data.TFRecordDataset(filename) | |
dataset = dataset.repeat(epochs).map(parser).batch(batch_size) |
This file contains 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
def apply_gradients(optimizer, gradients, variables, global_step=None): | |
optimizer.apply_gradients( | |
zip(gradients, variables), global_step=global_step) | |
apply_gradients = tfe.defun(apply_gradients) |
This file contains 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
tfe = tf.contrib.eager | |
model.call = tfe.defun(model.call) | |
model.compute_gradients = tfe.defun(model.compute_gradients) |
This file contains 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
checkpoint.save(file_prefix) | |
checkpoint.restore(save_path) |
This file contains 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
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer, | |
learning_rate=learning_rate, global_step=global_step) |
NewerOlder