This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
OlderNewer