Skip to content

Instantly share code, notes, and snippets.

@ApprenticeZ
ApprenticeZ / pytorch_dataloader_example.py
Last active December 21, 2022 14:22
pre-process MNIST/SVHN with PyTorch
import numpy as np
from torchvision.datasets import MNIST, SVHN
from torchvision import transforms
import torch
from torch.utils.data import DataLoader
def normalize(data_tensor):
'''re-scale image values to [-1, 1]'''
return (data_tensor / 255.) * 2. - 1.
@ApprenticeZ
ApprenticeZ / test_tf_cond.py
Created October 7, 2017 04:28
a simple example that demonstrates `tf.cond`
'''add ta and tb 5 times, and then multiply them 5 times'''
import tensorflow as tf
ta = tf.ones([3], dtype=tf.float32)
tb = tf.zeros([3], dtype=tf.float32)
global_step = tf.Variable(0, trainable=False)
add_global_step = global_step.assign_add(1)
add_step = 5
total_step = 10
import tensorflow as tf
# get variables by name
def _param_by_name(name):
all_vars = tf.trainable_variables()
filtered_vars = []
for v in all_vars:
if name in v.name:
filtered_vars.append(v)
@ApprenticeZ
ApprenticeZ / lstm_cell_shape.py
Created July 13, 2017 04:59
a sample to inspect LSTM cell variable shape
import tensorflow as tf
num_units = 2
batch_size = 3
n_dim = 4
seq_len = 5
sess = tf.Session()
image_embeddings = tf.zeros([batch_size, n_dim])
seq_embeddings = tf.zeros([batch_size, seq_len, n_dim])
@ApprenticeZ
ApprenticeZ / test_pair_embeddings.py
Created July 10, 2017 07:37
an example to create 2d image-like pair embeddings as described in arxiv:1704.06933
import tensorflow as tf
import numpy as np
T = 2
d = 3
x = np.arange(T*d).reshape((T,d))
y = x+1
tx = tf.constant(x)
ty = tf.constant(y)
@ApprenticeZ
ApprenticeZ / pt_example_share_templates.py
Created December 21, 2016 07:17
prettytensor share templates
import tensorflow as tf
import prettytensor as pt
shared_template = (pt.template('c_input').fully_connected(128).fully_connected(256))
a_template = (pt.template("a_input").fully_connected(64))
b_template = (pt.template('b_input').fully_connected(64))
a_model = a_template.attach_template(shared_template,_key='c_input')
b_model = b_template.attach_template(shared_template,_key='c_input')
@ApprenticeZ
ApprenticeZ / max-sum-btree.cpp
Created September 13, 2014 12:03
[leetcode]maxPathSum
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@ApprenticeZ
ApprenticeZ / edit_distance.cpp
Created September 13, 2014 10:40
[leetcode]edit distance
class Solution {
public:
// if a==b, return 0
// else return 1
int diffDistance(char a, char b){
if(a==b)
return 0;
else
return 1;
}
@ApprenticeZ
ApprenticeZ / lis.cpp
Created September 15, 2013 08:08
the longest increasing subsequence problem solved with DP Complexity: O(n^2)
#include <iostream>
#include <assert.h>
using namespace std;
/* function: find the length of longest increasing subsequences of input array
* arr - input integer array
* n - length of arr
*/
int lis(const int *arr, int n)
@ApprenticeZ
ApprenticeZ / dp_fabonacci.cpp
Created September 15, 2013 06:59
solve fabonacci problem with dynamic programming CLRS ex15.1-5
#include <iostream>
using namespace std;
int Fabonacci(int n)
{
if(n>=0 && n<2)
return 1;
// create a new array for memoization
int *f = new int[n+1];