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)
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) predictions = predictions.reshape(-1, num_words + 1) # Reshape into #samples x #words. n_pred = predictions.shape[0] for row in range(n_pred): # Sample a word from output probabilities. sample = np.random.multinomial(n=1, pvals=predictions[row,:]).argmax() line = X_test['X'][row] # Get the input line. line = line[X_test['X_mask'][row].astype('bool')] # Apply mask. print 'Input: %r' %([word2vec_vocab_rev[w] for w in line]) # Print words in line. print 'Guess:: %r' %(word2vec_vocab_rev[sample]) # Print predicted word. print 'Output: %r' %(word2vec_vocab_rev[y_test[row]]) # Print the correct word.