# Word to predict. pred_word = word2vec_vocab[words[i]] target_vals.append(pred_word) if i - 1 == MAX_SEQ_LEN: break encoded_sequences = np.vstack(encoded_sequences).astype('int32') masks = np.vstack(masks).astype('float32') y = np.vstack(target_vals).astype('int32') output_layer, train_func, test_func, predict_func, get_hidden_func = word_prediction_network(BATCH_SIZE, word_embedding_size, num_words, MAX_SEQ_LEN, WEIGHTS, NUM_UNITS_GRU, learning_rate) estimator = LasagneNet(output_layer, train_func, test_func, predict_func, get_hidden_func, on_epoch_finished=[SaveParams('save_params','word_embedding', save_interval = 1)]) # estimator.draw_network() # requires networkx package X_train = {'X': encoded_sequences[:train_split], 'X_mask': masks[:train_split]} y_train = y[:train_split] X_test = {'X': encoded_sequences[train_split:test_split], 'X_mask': masks[train_split:test_split]} y_test = y[train_split:test_split] train = False if train: estimator.fit(X_train, y_train) else: estimator.load_weights_from('saved_params') word2vec_vocab_rev = dict(zip(word2vec_vocab.values(), word2vec_vocab.keys())) # Maps indeces to words. predictions = estimator.predict(X_test)
return train_func(X, y.reshape((-1,1))) def test_function(X, y): return test_func(X, y.reshape((-1,1))) def predict_function(X): return predict_func(X) return l_out, train_function, test_function, predict_function, learning_rate from sklearn import datasets boston = datasets.load_boston() num_epochs = 20 train_x, test_x, train_y, test_y = train_test_split(boston['data'], boston['target'], test_size=0.05) l_output, train_function, test_function, predict_function, learning_rate = create_lasagne_network(train_x.shape[1]) estimator = LasagneNet(l_output, train_function, test_function, predict_function,is_regression=True,batch_iterator_train=BatchIterator(256),max_epochs=num_epochs, on_epoch_finished=[AdjustVariable('learning_rate',start=0.03, stop=0.000001,end_epoch=num_epochs), SaveParams('save_params','C:/params/rossman/', save_interval = 50)]) X = {'X' : train_x.astype('float32')} estimator.fit(X, train_y.astype('float32')) X = {'X' : test_x.astype('float32')} pred = estimator.predict(X).reshape((-1,1)) print np.mean((pred-test_y)**2)
# TODO: use response also, so I can see the difference between predicted and actual. encoded_sequences = np.vstack(encoded_sequences).astype("int32") masks = np.vstack(masks).astype("float32") X_test = {"X": encoded_sequences, "X_mask": masks} output_layer, train_func, test_func, predict_func = word_prediction_network( BATCH_SIZE, word_embedding_size, num_words, MAX_SEQ_LEN, WEIGHTS, NUM_UNITS_GRU, learning_rate ) estimator = LasagneNet( output_layer, train_func, test_func, predict_func, on_epoch_finished=[SaveParams("save_params", "word_embedding", save_interval=1)], ) # estimator.draw_network() # requires networkx package train = True if train: estimator.fit(X_train, y_train) else: estimator.load_weights_from("word_embedding/saved_params_3") pred_sents = [] # For each test example, predict the response. for idx in xrange(X_test["X"].shape[0]): pred_words = []