conv_layer.params[0].set_value(savedparams[k]) conv_layer.params[1].set_value(savedparams[k + 1]) k = k + 2 test_set_x = datasets[0][:, :img_h] test_set_y = np.asarray(datasets[0][:, -1], "int32") test_pred_layers = [] test_size = 1 test_layer0_input = Words[T.cast(x.flatten(), dtype="int32")].reshape( (test_size, 1, img_h, Words.shape[1])) for conv_layer in conv_layers: test_layer0_output = conv_layer.predict(test_layer0_input, test_size) test_pred_layers.append(test_layer0_output.flatten(2)) test_layer1_input = T.concatenate(test_pred_layers, 1) test_y_pred = classifier.predict_p(test_layer1_input) #test_error = T.mean(T.neq(test_y_pred, y)) pdb.set_trace() model = theano.function([x], test_y_pred, allow_input_downcast=True) currentClass = 0 currentBestScore = 0 currentBestSentence = "" for i in range(0, test_set_x.shape[0]): rev = revsin[i] result = model(test_set_x[i, None]) scoresum = sum(result[0][0:currentClass + 1]) if rev['y'] > currentClass: currentClass = rev['y'] currentBestScore = 0 print "best for class" + str(currentClass) print currentBestSentence
conv_layer.params[1].set_value( savedparams[k+1]) k = k + 2 test_set_x = datasets[0][:,:img_h] test_set_y = np.asarray(datasets[0][:,-1],"int32") test_pred_layers = [] test_size = 1 test_layer0_input = Words[T.cast(x.flatten(),dtype="int32")].reshape((test_size,1,img_h,Words.shape[1])) for conv_layer in conv_layers: test_layer0_output = conv_layer.predict(test_layer0_input, test_size) test_pred_layers.append(test_layer0_output.flatten(2)) test_layer1_input = T.concatenate(test_pred_layers, 1) test_y_pred = classifier.predict_p(test_layer1_input) #test_error = T.mean(T.neq(test_y_pred, y)) pdb.set_trace() model = theano.function([x],test_y_pred,allow_input_downcast=True) currentClass = 0 currentBestScore = 0 currentBestSentence = "" correct = 0 docMean = 0 docSent = 0 with open('output.csv', 'w') as fp: writer = csv.writer(fp, delimiter=',') writer.writerow(['class','score', 'sdscore','sentence']) print "document " + str(currentClass) for i in range(0,test_set_x.shape[0]): rev = revsin[i]
def __init__(self): mrppath = os.path.join(this_dir, "mr.p") x = cPickle.load(open(mrppath,"rb")) revs, W, W2, word_idx_map, vocab = x[0], x[1], x[2], x[3], x[4] self.word_idx_map = word_idx_map U = W classifierpath = os.path.join(this_dir, "classifier.save") savedparams = cPickle.load(open(classifierpath,'rb')) filter_hs=[3,4,5] conv_non_linear="relu" hidden_units=[100,2] dropout_rate=[0.5] activations=[Iden] img_h = 56 + 4 + 4 img_w = 300 rng = np.random.RandomState(3435) batch_size=50 filter_w = img_w feature_maps = hidden_units[0] filter_shapes = [] pool_sizes = [] for filter_h in filter_hs: filter_shapes.append((feature_maps, 1, filter_h, filter_w)) pool_sizes.append((img_h-filter_h+1, img_w-filter_w+1)) #define model architecture x = T.matrix('x') Words = theano.shared(value = U, name = "Words") zero_vec_tensor = T.vector() layer0_input = Words[T.cast(x.flatten(),dtype="int32")].reshape((x.shape[0],1,x.shape[1],Words.shape[1])) conv_layers = [] layer1_inputs = [] for i in xrange(len(filter_hs)): filter_shape = filter_shapes[i] pool_size = pool_sizes[i] conv_layer = LeNetConvPoolLayer(rng, input=layer0_input,image_shape=(batch_size, 1, img_h, img_w), filter_shape=filter_shape, poolsize=pool_size, non_linear=conv_non_linear) layer1_input = conv_layer.output.flatten(2) conv_layers.append(conv_layer) layer1_inputs.append(layer1_input) layer1_input = T.concatenate(layer1_inputs,1) hidden_units[0] = feature_maps*len(filter_hs) classifier = MLPDropout(rng, input=layer1_input, layer_sizes=hidden_units, activations=activations, dropout_rates=dropout_rate) classifier.params[0].set_value(savedparams[0]) classifier.params[1].set_value(savedparams[1]) k = 2 for conv_layer in conv_layers: conv_layer.params[0].set_value( savedparams[k]) conv_layer.params[1].set_value( savedparams[k+1]) k = k + 2 test_pred_layers = [] test_size = 1 test_layer0_input = Words[T.cast(x.flatten(),dtype="int32")].reshape((test_size,1,img_h,Words.shape[1])) for conv_layer in conv_layers: test_layer0_output = conv_layer.predict(test_layer0_input, test_size) test_pred_layers.append(test_layer0_output.flatten(2)) test_layer1_input = T.concatenate(test_pred_layers, 1) test_y_pred = classifier.predict_p(test_layer1_input) #test_error = T.mean(T.neq(test_y_pred, y)) self.model = theano.function([x],test_y_pred,allow_input_downcast=True)