Skip to content

Instantly share code, notes, and snippets.

View lxuechen's full-sized avatar

Xuechen Li lxuechen

View GitHub Profile
@lxuechen
lxuechen / ft_t5.py
Last active February 3, 2023 20:42
# 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
@lxuechen
lxuechen / vae.py
Last active November 15, 2018 20:44
testing vae on fashion mnist with different binarization schemes
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
@lxuechen
lxuechen / defun_vs_ag.py
Created August 3, 2018 20:39
defun vs autograph
def tensor_square(x, stop_when): # x is scalar Tensor
cnt = 0
while x < stop_when:
x = tf.square(x)
cnt += 1
return cnt
@lxuechen
lxuechen / estimator.py
Created August 3, 2018 19:41
wrapping in estimator
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)
@lxuechen
lxuechen / loop_ds.py
Created August 3, 2018 19:41
simple looping over dataset in eager
for image, label in dataset:
logits = model(image, training=True)
...
@lxuechen
lxuechen / init_ds.py
Created August 3, 2018 19:40
initialize dataset object
dataset = tf.data.TFRecordDataset(filename)
dataset = dataset.repeat(epochs).map(parser).batch(batch_size)
@lxuechen
lxuechen / defun_optim.py
Created August 3, 2018 19:39
defun optimizer
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)
@lxuechen
lxuechen / defun.py
Created August 3, 2018 19:39
general defun
tfe = tf.contrib.eager
model.call = tfe.defun(model.call)
model.compute_gradients = tfe.defun(model.compute_gradients)
@lxuechen
lxuechen / save_and_restore.py
Created August 3, 2018 19:38
save and restore checkpoint
checkpoint.save(file_prefix)
checkpoint.restore(save_path)
@lxuechen
lxuechen / init_checkpoint.py
Created August 3, 2018 19:37
initialize checkpoint
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer,
learning_rate=learning_rate, global_step=global_step)