def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01): self.params = {} self.params['W1'] = np.arange([ 0.1 * random.randint(1, 9) for _ in range(input_size * hidden_size) ]).reshape(input_size, hidden_size) self.params['b1'] = np.arange([ 0.1 * random.randint(1, 9) for _ in range(hidden_size) ]).reshape(1, hidden_size) self.params['W2'] = np.arange([ 0.1 * random.randint(1, 9) for _ in range(hidden_size * output_size) ]).reshape(hidden_size, output_size) self.params['b2'] = np.arange([ 0.1 * random.randint(1, 9) for _ in range(output_size) ]).reshape(1, output_size) self.layers = collections.OrderedDict() self.layers['Affine1'] = ln.Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = ln.Relu() self.layers['Affine2'] = ln.Affine(self.params['W2'], self.params['b2']) self.lastlayer = ln.SoftmaxWithLoss()
def most_similar(query, word_to_id, id_to_word, word_matrix, top=5): if query not in word_to_id: print "did not find" return query_id = word_to_id[query] query_vec = np.arange(word_matrix.get_origin_list()[query_id]) vocab_size = len(id_to_word) similarity_lst = [0.0 for _ in range(vocab_size)] for i in range(vocab_size): similarity_lst[i] = cos_similarity( np.arange(word_matrix.get_origin_list()[i]), query_vec).get_origin_list() similarity = np.arange(similarity_lst) return similarity
def preprocess(text): text = text.lower() text = text.replace('.', ' .') words = text.split(' ') word_to_id = {} id_to_word = {} for word in words: if word not in word_to_id: new_id = len(word_to_id) word_to_id[word] = new_id id_to_word[new_id] = word corpus = np.arange([word_to_id[w] for w in words]) return corpus, word_to_id, id_to_word
def init_network(): network = {} network['W1'] = np.arange([0.1, 0.3, 0.5, 0.2, 0.4, 0.6]).reshape(2, 3) network['b1'] = np.arange([0, 0, 0]) network['W2'] = np.arange([0.1, 0.4, 0.2, 0.5, 0.3, 0.6]).reshape(3, 2) network['b2'] = np.arange([0, 0]) network['W3'] = np.arange([0.1, 0.3, 0.2, 0.4]).reshape(2, 2) network['b3'] = np.arange([0, 0]) layers = collections.OrderedDict() layers['Affine1'] = ln.Affine(network['W1'], network['b1']) layers['Sigmoid1'] = ln.Sigmoid() layers['Affine2'] = ln.Affine(network['W2'], network['b2']) layers['Sigmoid2'] = ln.Sigmoid() layers['Affine3'] = ln.Affine(network['W3'], network['b3']) return layers
network['W2'] = np.arange([0.1, 0.4, 0.2, 0.5, 0.3, 0.6]).reshape(3, 2) network['b2'] = np.arange([0, 0]) network['W3'] = np.arange([0.1, 0.3, 0.2, 0.4]).reshape(2, 2) network['b3'] = np.arange([0, 0]) layers = collections.OrderedDict() layers['Affine1'] = ln.Affine(network['W1'], network['b1']) layers['Sigmoid1'] = ln.Sigmoid() layers['Affine2'] = ln.Affine(network['W2'], network['b2']) layers['Sigmoid2'] = ln.Sigmoid() layers['Affine3'] = ln.Affine(network['W3'], network['b3']) return layers b = np.arange([2, 8, 9, 16]).reshape(2, 2) a = np.arange([1, 2, 3, 4, 5, 6, 7, 8]).reshape(2, 4) print a.ndim a.reshape(2, 2, 2) print a.ndim print "1111111111111" print a print a.ndim print "3333333333333" #print a.sum(axis=0) print np.sum(a, axis=0) print a.ndim print "2222222222222" print a
def loss(network, x, t): for layer in network: x = layer.forward(x) return ln.SoftmaxWithLoss().forward(x, t) # MAIN print "++++++++++++++++ FORWARD +++++++++++++++++++++++" forward_layer = init_network() forward_network = list(forward_layer.values()) _in = np.arange([1.5, 0.5]) _result = np.arange([1.0, 0.0]) #_result = np.arange([1.0, 0.0, 0.0]) loss_W = lambda W: loss(forward_network, _in, _result) fw_affine1_W = pe._numerical_gradient(loss_W, forward_layer['Affine1'].W) fw_affine1_b = pe._numerical_gradient(loss_W, forward_layer['Affine1'].b) fw_affine2_W = pe._numerical_gradient(loss_W, forward_layer['Affine2'].W) fw_affine2_b = pe._numerical_gradient(loss_W, forward_layer['Affine2'].b) fw_affine3_W = pe._numerical_gradient(loss_W, forward_layer['Affine3'].W) fw_affine3_b = pe._numerical_gradient(loss_W, forward_layer['Affine3'].b) print "+++++++++++++++++ BACKWARD ++++++++++++++++++++++" backward_layer = init_network() backward_network = list(backward_layer.values())
import two_layer_net import random from lib import vector as ve x_lst = [random.uniform(0, 100) for i in range(100 * 20)] t_lst = [random.uniform(0, 1) for i in range(100 * 2)] x = ve.arange(x_lst).reshape(100, 20) t = ve.arange(t_lst).reshape(100, 2) iters_num = 1 learning_rate = 0.01 train_size = x.get_demension()[0] network = two_layer_net.TwoLayerNet(20, 10, 2, learning_rate) for i in range(iters_num): batch_mask = random.randint(0, 99) print batch_mask input_lst = x.get_origin_list()[batch_mask] print "input" print input_lst print "====================================" output_lst = t.get_origin_list()[batch_mask] print "output" print output_lst print "===================================" input = ve.arange(input_lst).reshape(1, 20)
from backward import two_layer_net import random from lib import vector as ve iters_num = 1 network = two_layer_net.TwoLayerNet(input_size=4, hidden_size=4, output_size=2) x_train = [int(random.uniform(0, 100)) for i in range(4 * 3)] t_train = [0, 1, 1, 0, 0, 1] x_batch = ve.arange(x_train).reshape(3, 4) t_batch = ve.arange(t_train).reshape(3, 2) for i in range(iters_num): print "=========================" print i print "=========================" input = x_batch.get_origin_list()[i] output = t_batch.get_origin_list()[i] x_vector = ve.arange(input) t_vector = ve.arange(output) print "**********FORWARD***************" print x_vector print t_vector grad_numerical = network.numerical_gradient(x_vector, t_vector) print "**********BACKWARD***************"