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 / unit_test.py
Last active August 7, 2018 23:04
fast prototyping with eager
block = Residual()
x = tf.random_normal(shape=(N, C, H, W))
dy = tf.random_normal(shape=(N, C, H, W))
with tf.GradientTape() as tape:
tape.watch(x)
y = block(x)
# Compute true grads
dx_true = tape.gradient(y, x, output_gradients=dy)
# Compute grads from reconstruction
@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 / residual.py
Last active August 3, 2018 19:42
define residual block
class Residual(tf.keras.Model):
def __init__(self, filters):
super(Residual, self).__init__()
self.f = ResidualInner(filters=filters, strides=(1, 1))
self.g = ResidualInner(filters=filters, strides=(1, 1))
def call(self, x, training=True):
x1, x2 = tf.split(x, num_or_size_splits=2, axis=self.axis)
f_x2 = self.f(x2, training=training)
y1 = f_x2 + x1
@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)