Skip to content

Instantly share code, notes, and snippets.

View hccho2's full-sized avatar

Heecheol Cho hccho2

View GitHub Profile
@hccho2
hccho2 / test.py
Created September 7, 2017 00:40
Gist Test
import numpy as np
def get_scope_name(scope_name,base_name,mod=1):
if scope_name =='':
scope_name = base_name
else: scope_name = scope_name + '/' + base_name
all_vars = tf.global_variables()
n = len([var for var in all_vars if var.name.startswith(scope_name)])
if n==0:
return base_name
else:
# coding: utf-8
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
col_size=6
n = 3
a = np.arange(col_size*n*n).reshape(col_size*n,n)
x = tf.convert_to_tensor(a)
y = tf.reshape(x,[col_size*n*n])
# coding: utf-8
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
batch_size=2
T=3
input_dim=4
hidden_dim=50
x = tf.placeholder(tf.float32,[3,5,5,7])
y = tf.placeholder(tf.float32,[3,5,5,8])
z1 = tf.layers.conv2d(x,filters=3,kernel_size=2,name='XXX')
z2 = tf.layers.conv2d(x,filters=3,kernel_size=2,name='XXX',reuse=True)
print(tf.trainable_variables())
@hccho2
hccho2 / attention-matmul.py
Last active July 25, 2019 06:49
attention계산과정에서의 행렬곱
N=2 # batch size
T=20 # encoder time length
D1=30 # encoder hidden dim
D2=6 # decoder hidden dim
D3=11 # attention dim
h = np.random.randn(N,T,D1) # all encoder hidden
s = np.random.randn(N,D2) # decoder hidden at one time step
Wm = np.random.randn(D1,D3)
Wq = np.random.randn(D2,D3)
A = np.matmul(h,Wm) + np.expand_dims(np.matmul(s,Wq),axis=1)
import numpy as np
import tensorflow as tf
tf.reset_default_graph()
x_data= tf.placeholder(tf.int32,shape=[None,6])
vocab_size=5
init = np.arange(5*3).reshape(vocab_size,-1)
embedding = tf.get_variable("embedding", initializer=init.astype(np.float32),dtype = tf.float32)
inputs = tf.nn.embedding_lookup(embedding, x_data)
@hccho2
hccho2 / torch_test.py
Created September 3, 2019 09:08
torch code
# coding: utf-8
import torch
w_true = torch.Tensor([1, 2, 3])
X = torch.cat([torch.ones(100, 1), torch.randn(100, 2)], 1)
Y = torch.mv(X, w_true) + torch.randn(100) * 0.5
w = torch.randn(3, requires_grad=True)
lr = 0.1
@hccho2
hccho2 / conv2d.py
Last active September 11, 2019 03:24
conv2d_transpose vs conv2d
# coding: utf-8
'''
conv2d: weight(kernel_size,kernel_size,in_channel,out_channel)
conv2d_transpose: weight(kernel_size,kernel_size,out_channel,in_channel)
'''
import numpy as np
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
tf.compat.v1.reset_default_graph()
x = np.sin(np.linspace(0,3*np.pi,30))+ np.random.randn(30)*0.1
filter = np.sin(np.linspace(np.pi/2-np.pi/6,np.pi/2+np.pi/6,5))
x = np.array([ 0.15056179, 0.30541199, 0.72807816, 0.940651 , 0.97211703,
1.01255197, 0.86129876, 0.71344136, 0.56986685, 0.24477384,