def check_gradients(self, X, Y): """ Helper function to test the parameter gradients for correctness. """ # Warning: the following is a hack Y_one_hot = one_hot(Y) self._setup(X, Y_one_hot) for l, layer in enumerate(self.layers): if isinstance(layer, ParamMixin): print('layer %d' % l) for p, param in enumerate(layer.params()): param_shape = param.shape def fun(param_new): param[:] = np.reshape(param_new, param_shape) return self._loss(X, Y_one_hot) def grad_fun(param_new): param[:] = np.reshape(param_new, param_shape) # Forward propagation X_next = X for layer in self.layers: X_next = layer.fprop(X_next) Y_pred = X_next # Back-propagation of partial derivatives next_grad = self.layers[-1].input_grad(Y_one_hot, Y_pred) for layer in reversed(self.layers[l:-1]): next_grad = layer.bprop(next_grad) return np.ravel(self.layers[l].param_grads()[p]) param_init = np.ravel(np.copy(param)) err = sp.optimize.check_grad(fun, grad_fun, param_init) print('diff %.2e' % err)
def grad_check(self, X, Y): Y_one_hot = one_hot(Y) self._setup(X,Y_one_hot) for l, layer in enumerate(self.layers): if isinstance(layer, ParamMixin): for p,param in enumerate(layer.params()): #compute correctly X_next = X for layer in self.layers: X_next = layer.fprop(X_next) Y_pred = X_next # Back-propagation of partial derivatives next_grad = self.layers[-1].input_grad(Y_one_hot, Y_pred) for layer in reversed(self.layers[l:-1]): next_grad = layer.bprop(next_grad) #compute my way a,b = param.shape check = np.zeros((a,b)) eps = 1e-5 for i in range(a): for j in range(b): param[i,j] = param[i,j] + eps f = self._loss(X,Y_one_hot) param[i,j] = param[i,j] - 2*eps s = self._loss(X,Y_one_hot) check[i,j] = (f-s)/(2*eps) print np.linalg.norm(layer.param_incs[p]-check)
def domRes(outDom, target, **args): # TODO: make faster again by keeping sparse tensors sparse t = h.one_hot(target.data.long(), outDom.size()[1]).to_dense() tmat = t.unsqueeze(2).matmul(t.unsqueeze(1)) tl = t.unsqueeze(2).expand(-1, -1, tmat.size()[1]) inv_t = h.eye(tmat.size()[1]).expand(tmat.size()[0], -1, -1) inv_t = inv_t - tmat tl = tl.bmm(inv_t) fst = outDom.bmm(tl) snd = outDom.bmm(inv_t) diff = fst - snd return diff.lb() + t
def generate_mini_batch(flist): flist = np.array(flist) np.random.shuffle(flist) X = [] y = [] for f in flist: feat = segment_axis(features[f][TRAINING_FEATURE], frame_length=longest_utt, step_length=1, end='pad', pad_value=0, pad_mode=PAD_MODE) label = int(f.split('_')[0]) X.append(feat) y.append(label) X = np.concatenate(X, axis=0) y = np.array(y) indices = np.random.permutation(X.shape[0]) return X[indices], one_hot(y[indices], nb_classes=nb_classes)
def forward(self, input_sequences, hx): #one hot encode a list of sequences encoded_sequences = [ one_hot(sequence, self.n_chars) for sequence in input_sequences ] #batch has dimensions (n_sequences x batch_size x n_chars) #fixinf a dimension batch = stack(encoded_sequences, dim=0) #pass into the LSTM recurrent_output, hidden = self.lstm(batch, hx) #"dense" layer just projects it back down to the space of available characters linear_output = self.dense(recurrent_output) return linear_output, hidden
def fit(self, X, Y, learning_rate=0.1, max_iter=10, batch_size=64): """ Train network on the given data. """ n_samples = Y.shape[0] n_batches = n_samples // batch_size Y_one_hot = one_hot(Y) self._setup(X, Y_one_hot) iter = 0 # Stochastic gradient descent with mini-batches while iter < max_iter: iter += 1 for b in range(n_batches): batch_begin = b*batch_size batch_end = batch_begin+batch_size X_batch = X[batch_begin:batch_end] Y_batch = Y_one_hot[batch_begin:batch_end] # Forward propagation X_next = X_batch for layer in self.layers: X_next = layer.fprop(X_next) Y_pred = X_next # Back propagation of partial derivatives next_grad = self.layers[-1].input_grad(Y_batch, Y_pred) for layer in reversed(self.layers[:-1]): next_grad = layer.bprop(next_grad) # Update parameters for layer in self.layers: if isinstance(layer, ParamMixin): for param, inc in zip(layer.params(), layer.param_incs()): param -= learning_rate*inc # Output training status loss = self._loss(X, Y_one_hot) error = self.error(X, Y) print('iter %i, loss %.4f, train error %.4f' % (iter, loss, error))
import tensorflow_datasets as tfds from helpers import Word_Index, one_hot, load tokenizer = tfds.features.text.Tokenizer() word_index = Word_Index() model = load() review = "best movie best actors high praise" tokenized_review = tokenizer.tokenize(review) encoded_review = [word_index.get_index(word) for word in tokenized_review] one_hot_review = one_hot([encoded_review]) prediction = model.predict(one_hot_review) print(prediction)
def fit(self, X, Y, learning_rate=0.1, max_iter=10, batch_size=64, name="mnist", load_type=LOAD.NUMERIC): """ Train network on the given data. """ self.name = name try: stamp = str("start stamp -- " + str(datetime.datetime.now())) self.append_status(name=name, message=stamp) except: print("no datetime") n_samples = Y.shape[0] n_batches = n_samples // batch_size Y_one_hot = one_hot(Y, load_type=load_type) self._setup(X, Y_one_hot) self.load_file(name=name) iter = 0 # Stochastic gradient descent with mini-batches while iter < max_iter: iter += 1 for b in range(n_batches): message = (str(b + 1) + " of " + str(n_batches) + " batches (batch size=" + str(batch_size) + "), iter " + str(iter) + " with total of " + str(max_iter)) self.append_status(name=name, message=message) batch_begin = b * batch_size batch_end = batch_begin + batch_size X_batch = X[batch_begin:batch_end] Y_batch = Y_one_hot[batch_begin:batch_end] # Forward propagation X_next = X_batch for layer in self.layers: X_next = layer.fprop(X_next) Y_pred = X_next # Back propagation of partial derivatives next_grad = self.layers[-1].input_grad(Y_batch, Y_pred) for layer in reversed(self.layers[:-1]): next_grad = layer.bprop(next_grad) # Update parameters for layer in self.layers: if isinstance(layer, ParamMixin): for param, inc in zip(layer.params(), layer.param_incs()): param -= learning_rate * inc modconst = 5 modnum = (b + 1) % modconst #print modnum if b + 1 > 1 and modnum == 0: if self.interrupt: message = ("Interrupt for training status...") self.append_status(name=self.name, message=message) self.status(iter, X, Y, Y_one_hot) else: message = ("periodic save...") self.append_status(name=self.name, message=message) self.save_file(name=self.name) self.status(iter, X, Y, Y_one_hot) ##end
def fit(self, X, Y, learning_rate=0.1, max_iter=10, batch_size=64, name="mnist", load_type = LOAD.NUMERIC): """ Train network on the given data. """ self.name = name try: stamp = str("start stamp -- "+str(datetime.datetime.now())) self.append_status(name=name, message=stamp) except: print("no datetime") n_samples = Y.shape[0] n_batches = n_samples // batch_size Y_one_hot = one_hot(Y , load_type=load_type) self._setup(X, Y_one_hot) self.load_file(name=name) iter = 0 # Stochastic gradient descent with mini-batches while iter < max_iter: iter += 1 for b in range(n_batches): message = (str(b + 1) + " of " + str(n_batches) +" batches (batch size="+str(batch_size)+"), iter " + str(iter) + " with total of "+ str(max_iter)) self.append_status(name=name, message=message) batch_begin = b*batch_size batch_end = batch_begin+batch_size X_batch = X[batch_begin:batch_end] Y_batch = Y_one_hot[batch_begin:batch_end] # Forward propagation X_next = X_batch for layer in self.layers: X_next = layer.fprop(X_next) Y_pred = X_next # Back propagation of partial derivatives next_grad = self.layers[-1].input_grad(Y_batch, Y_pred) for layer in reversed(self.layers[:-1]): next_grad = layer.bprop(next_grad) # Update parameters for layer in self.layers: if isinstance(layer, ParamMixin): for param, inc in zip(layer.params(), layer.param_incs()): param -= learning_rate*inc modconst = 5 modnum = (b+1) % modconst #print modnum if b+1 > 1 and modnum == 0 : if self.interrupt: message = ("Interrupt for training status...") self.append_status(name=self.name, message = message) self.status(iter,X,Y,Y_one_hot) else : message = ("periodic save...") self.append_status(name=self.name, message = message) self.save_file(name=self.name) self.status(iter,X,Y,Y_one_hot) ##end
losses = keras.losses metrics = keras.metrics num_words = 10000 word_index = Word_Index() (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=num_words) decoded_review = ' '.join([ word_index.get_word(encoded_word - 3, '?') for encoded_word in train_data[0] ]) x_train = one_hot(train_data, num_words) y_train = np.asarray(train_labels).astype('float32') x_test = one_hot(test_data, num_words) y_test = np.asarray(test_labels).astype('float32') model = Sequential() model.add(Dense(16, activation='relu', input_shape=(num_words, ))) model.add(Dense(16, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer=optimizers.RMSprop(lr=0.001), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy]) partial_x_train = x_train[10000:] x_val = x_train[:10000]
def play(): cur_iters = 0 episode_reward = 0 done = False while not done: cur_iters += 1 print("\rIter:", cur_iters, end="", flush=True) # alive = list(range(gs.num_agents)) # preds = np.zeros((gs.num_agents, gs.num_actions)) states_before = [] states_after = [] actions = [] rewards = [] gs.reset_markers() for agent_idx in range(gs.num_agents): # x, y, t, u, h = gs.agents[agent_idx] agent_state = get_state(gs, agent_idx, MODEL_TYPE) states_before.append(agent_state) # Get predicted action pred = models[agent_idx].get_action(agent_state) if MODEL_TYPE in train_after_episode: pred = one_hot(pred, gs.num_actions, float) if agent_idx in stochastic: action = random.randint(0, gs.num_actions - 1) else: action = np.argmax(pred) # Perform action & get reward reward = gs.move_agent(agent_idx, action) agent_state = get_state(gs, agent_idx, MODEL_TYPE) states_after.append(agent_state) episode_reward += reward rewards.append(reward) if MODEL_TYPE in sac_models: actions.append(pred) else: actions.append(action) gs.update_agent_positions() # Check stop conditions winner = gs.get_winner() if winner is not None: done = True # Add winning reward for agent_idx in range(gs.num_agents): x, y, t, u, h = gs.agents[agent_idx] if t == winner: rewards[agent_idx - gs.num_agents] += 1 elif cur_iters >= MAX_ITERS: done = True if PRINT_VISUALS and cur_iters % 100 == 0: os.system("clear") print(gs.print_game_space()) print() print_state(MODEL_TYPE, cur_iters, episode, episode_reward, gs) # Train train_after_iter(models, gs, states_before, states_after, actions, rewards, done) print() print_state(MODEL_TYPE, cur_iters, episode, episode_reward, gs) return cur_iters, episode_reward, done