<commit_before>import numpy as np
import tensorflow as tf
import h5py
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import sys

# usage: python exp7g.py 64 0 0 0 0
# system arg
nhidden = 64
SMALL_FLAG = 1
FAST_FLAG = 1
BN_FLAG = 1
SYS_FLAG = 0 # 0 for bridges, 1 for supermic

try:
	nhidden = int(sys.argv[1])
	SMALL_FLAG = int(sys.argv[2])
	FAST_FLAG = int(sys.argv[3])
	BN_FLAG = int(sys.argv[4])
	SYS_FLAG = int(sys.argv[5])
except Exception, e:
	print('-- {}'.format(e))

print('-- Number of features = {}'.format(nhidden))
print('-- SMALL FLAG: {}'.format(SMALL_FLAG))
print('-- FAST FLAG: {}'.format(FAST_FLAG))
print('-- NORMALIZATION FLAG: {}'.format(BN_FLAG))
print('-- SYS FLAG: {}'.format(SYS_FLAG))


# Functions for initializing neural nets parameters
def init_weight_variable(shape):
	initial = tf.truncated_normal(shape, stddev=0.1, dtype=tf.float32)
	return tf.Variable(initial)

def init_bias_variable(shape):
	initial = tf.constant(0.1, shape=shape, dtype=tf.float32)
	return tf.Variable(initial)

def batch_nm(x, eps=1e-5):
	# batch normalization to have zero mean and unit variance
	mu, var = tf.nn.moments(x, [0])
	return tf.nn.batch_normalization(x, mu, var, None, None, eps)


# ==============================================
# ==============================================
# 					main driver
# ==============================================
# ==============================================
print('==> Experiment 7g: Autoencoder...')
sys_path = '/pylon2/ci560sp/haunter/'
if SYS_FLAG:
	sys_path = '/scratch/zwang3/'
filename = 'exp7g.mat'
if SMALL_FLAG:
	filename = 'exp7g_small.mat'
filepath = sys_path + filename
print('==> Loading data from {}...'.format(filepath))
# benchmark
t_start = time.time()

# ==============================================
# 				reading data
# ==============================================
train_frac = 0.8
f = h5py.File(filepath)
X = np.array(f.get('data'))
t_end = time.time()
print('--Time elapsed for loading data: {t:.2f} \
		seconds'.format(t = t_end - t_start))
del f
# shuffle and split
np.random.shuffle(X)
total_samples, total_features = X.shape
X_train = X[:int(train_frac * total_samples), :]
X_val = X[int(train_frac):, :]

num_training_vec = X_train.shape[0]
num_val_vec = X_val.shape[0]
print('-- Number of training samples: {}'.format(num_training_vec))
print('-- Number of validation samples: {}'.format(num_val_vec))

# ==============================================
# Neural-network model set-up
# ==============================================
batch_size = 1000

num_epochs = 500
print_freq = 5
if FAST_FLAG:
	num_epochs = 5
	print_freq = 1

# reset placeholders
x = tf.placeholder(tf.float32, [None, total_features])

# autoencoder: x -> a1 -> x
W_ae = init_weight_variable([total_features, nhidden])
W_ae = tf.identity(W_ae, name='W_ae')
b_ae = init_bias_variable([nhidden])
b_ae = tf.identity(b_ae, name='b_ae')
a1 = tf.nn.relu(tf.matmul(x, W_ae) + b_ae)
W_ad = init_bias_variable([nhidden, total_features])
b_ad = init_bias_variable([total_features])
h1 = tf.nn.relu(tf.matmul(a1, W_ad + b_ad))
error = tf.losses.mean_squared_error(x, h1)
train_step = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(error)

sess = tf.InteractiveSession()
if SYS_FLAG:
	sess.run(tf.initialize_all_variables())
else:
	sess.run(tf.global_variables_initializer())

# evaluation metrics
train_err_list = []<commit_msg>debugging variables<commit_after>import numpy as np
import tensorflow as tf
import h5py
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import sys

# usage: python exp7g.py 64 0 0 0 0
# system arg
nhidden = 64
SMALL_FLAG = 1
FAST_FLAG = 1
BN_FLAG = 1
SYS_FLAG = 0 # 0 for bridges, 1 for supermic

try:
	nhidden = int(sys.argv[1])
	SMALL_FLAG = int(sys.argv[2])
	FAST_FLAG = int(sys.argv[3])
	BN_FLAG = int(sys.argv[4])
	SYS_FLAG = int(sys.argv[5])
except Exception, e:
	print('-- {}'.format(e))

print('-- Number of features = {}'.format(nhidden))
print('-- SMALL FLAG: {}'.format(SMALL_FLAG))
print('-- FAST FLAG: {}'.format(FAST_FLAG))
print('-- NORMALIZATION FLAG: {}'.format(BN_FLAG))
print('-- SYS FLAG: {}'.format(SYS_FLAG))


# Functions for initializing neural nets parameters
def init_weight_variable(shape):
	initial = tf.truncated_normal(shape, stddev=0.1, dtype=tf.float32)
	return tf.Variable(initial)

def init_bias_variable(shape):
	initial = tf.constant(0.1, shape=shape, dtype=tf.float32)
	return tf.Variable(initial)

def batch_nm(x, eps=1e-5):
	# batch normalization to have zero mean and unit variance
	mu, var = tf.nn.moments(x, [0])
	return tf.nn.batch_normalization(x, mu, var, None, None, eps)


# ==============================================
# ==============================================
# 					main driver
# ==============================================
# ==============================================
print('==> Experiment 7g: Autoencoder...')
sys_path = '/pylon2/ci560sp/haunter/'
if SYS_FLAG:
	sys_path = '/scratch/zwang3/'
filename = 'exp7g.mat'
if SMALL_FLAG:
	filename = 'exp7g_small.mat'
filepath = sys_path + filename
print('==> Loading data from {}...'.format(filepath))
# benchmark
t_start = time.time()

# ==============================================
# 				reading data
# ==============================================
train_frac = 0.8
f = h5py.File(filepath)
X = np.array(f.get('data'))
t_end = time.time()
print('--Time elapsed for loading data: {t:.2f} \
		seconds'.format(t = t_end - t_start))
del f
# shuffle and split
np.random.shuffle(X)
total_samples, total_features = X.shape
X_train = X[:int(train_frac * total_samples), :]
X_val = X[int(train_frac):, :]

num_training_vec = X_train.shape[0]
num_val_vec = X_val.shape[0]
print('-- Number of training samples: {}'.format(num_training_vec))
print('-- Number of validation samples: {}'.format(num_val_vec))

# ==============================================
# Neural-network model set-up
# ==============================================
batch_size = 1000

num_epochs = 500
print_freq = 5
if FAST_FLAG:
	num_epochs = 5
	print_freq = 1

# reset placeholders
x = tf.placeholder(tf.float32, [None, total_features])

# autoencoder: x -> a1 -> x
W_ae = init_weight_variable([total_features, nhidden])
b_ae = init_bias_variable([nhidden])
a1 = tf.nn.relu(tf.matmul(x, W_ae) + b_ae)
W_ad = init_bias_variable([nhidden, total_features])
b_ad = init_bias_variable([total_features])
h1 = tf.nn.relu(tf.matmul(a1, W_ad + b_ad))
error = tf.losses.mean_squared_error(x, h1)
train_step = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(error)

sess = tf.InteractiveSession()
if SYS_FLAG:
	sess.run(tf.initialize_all_variables())
else:
	sess.run(tf.global_variables_initializer())

# evaluation metrics
train_err_list = []
{'fraction_non_alphanumeric': 0.24297960390186224, 'fraction_numerical': 0.019213715637008574, 'mean_word_length': 5.713293650793651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '765603', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2370, 'n_words': 537}