changeset 432:8e4d2ebd816a

added a test for LinearRegression
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Tue, 29 Jul 2008 11:16:05 -0400
parents 0f8c81b0776d
children 200a5b0e24ea
files _test_linear_regression.py linear_regression.py make_test_datasets.py
diffstat 3 files changed, 64 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_test_linear_regression.py	Tue Jul 29 11:16:05 2008 -0400
@@ -0,0 +1,25 @@
+
+import unittest
+from linear_regression import *
+from make_test_datasets import *
+import numpy
+
+class test_linear_regression(unittest.TestCase):
+
+    def test1(self):
+        trainset,testset,theta=make_artificial_datasets_from_function(n_inputs=3,
+                                                                      n_targets=2,
+                                                                      n_examples=100,
+                                                                      f=linear_predictor)
+        
+        assert trainset.fields()['input'].shape==(50,3)
+        assert testset.fields()['target'].shape==(50,2)
+        regressor = LinearRegression(L2_regularizer=0.1)
+        predictor = regressor(trainset)
+        test_data = testset.fields()
+        mse = predictor.compute_mse(test_data['input'],test_data['target'])
+        print 'mse = ',mse
+        
+if __name__ == '__main__':
+    unittest.main()
+        
--- a/linear_regression.py	Tue Jul 29 10:19:25 2008 -0400
+++ b/linear_regression.py	Tue Jul 29 11:16:05 2008 -0400
@@ -4,7 +4,7 @@
 the use of theano.
 """
 
-from pylearn.learner import OfflineLearningAlgorithm
+from pylearn.learner import OfflineLearningAlgorithm,OnlineLearningAlgorithm
 from theano import tensor as T
 from nnet_ops import prepend_1_to_each_row
 from theano.scalar import as_scalar
--- a/make_test_datasets.py	Tue Jul 29 10:19:25 2008 -0400
+++ b/make_test_datasets.py	Tue Jul 29 11:16:05 2008 -0400
@@ -1,4 +1,8 @@
 from pylearn.dataset import ArrayDataSet
+from shapeset.dset import Polygons
+from linear_regression import linear_predictor
+from kernel_regression import kernel_predictor
+from numpy import *
 
 """
 General-purpose code to generate artificial datasets that can be used
@@ -47,37 +51,37 @@
     Return a training set and a test set, by splitting the generated n_examples
     according to the 'train_frac'tion.
     """
-  n_train=int(train_frac*n_examples)
-  n_test=n_examples-n_train
-  if n_inputs==1:
-    delta1=2./n_train
-    delta2=2./n_test
-    inputs = vstack((array(zip(range(n_train)))*delta1-1,
-                     0.5*delta2+array(zip(range(n_test)))*delta2-1))
-  else:
-    inputs = random.normal(size=(n_examples,n_inputs))
-  if not f:
-    f = linear_predictor
-  if f==kernel_predictor and not otherargs[1]:
-    otherargs=(otherargs[0],inputs[0:n_train])
-  if not params_shape:
-    if f==linear_predictor:
-      params_shape = (n_inputs+1,n_targets)
-    elif f==kernel_predictor:
-      params_shape = (otherargs[1].shape[0]+1,n_targets)
-  theta = random.normal(size=params_shape) if params_shape else None
-  if b:
-    theta[0]=b
-  outputs = f(inputs,theta,otherargs)
-  targets = outputs + random.normal(scale=noise_level,size=(n_examples,n_targets))
-# the | stacking creates a strange bug in LookupList constructor:  
-#  trainset = ArrayDataSet(inputs[0:n_examples/2],{'input':slice(0,n_inputs)}) | \
-#             ArrayDataSet(targets[0:n_examples/2],{'target':slice(0,n_targets)}) 
-#  testset = ArrayDataSet(inputs[n_examples/2:],{'input':slice(0,n_inputs)}) | \
-#            ArrayDataSet(targets[n_examples/2:],{'target':slice(0,n_targets)})
-  data = hstack((inputs,targets))
-  trainset = ArrayDataSet(data[0:n_train],
-                          {'input':slice(0,n_inputs),'target':slice(n_inputs,n_inputs+n_targets)})
-  testset = ArrayDataSet(data[n_train:],
-                          {'input':slice(0,n_inputs),'target':slice(n_inputs,n_inputs+n_targets)})
-  return trainset,testset,theta
+    n_train=int(train_frac*n_examples)
+    n_test=n_examples-n_train
+    if n_inputs==1:
+        delta1=2./n_train
+        delta2=2./n_test
+        inputs = vstack((array(zip(range(n_train)))*delta1-1,
+                         0.5*delta2+array(zip(range(n_test)))*delta2-1))
+    else:
+        inputs = random.normal(size=(n_examples,n_inputs))
+    if not f:
+        f = linear_predictor
+    if f==kernel_predictor and not otherargs[1]:
+        otherargs=(otherargs[0],inputs[0:n_train])
+    if not params_shape:
+        if f==linear_predictor:
+            params_shape = (n_inputs+1,n_targets)
+        elif f==kernel_predictor:
+            params_shape = (otherargs[1].shape[0]+1,n_targets)
+    theta = random.normal(size=params_shape) if params_shape else None
+    if b:
+        theta[0]=b
+    outputs = f(inputs,theta,otherargs)
+    targets = outputs + random.normal(scale=noise_level,size=(n_examples,n_targets))
+    # the | stacking creates a strange bug in LookupList constructor:  
+    #  trainset = ArrayDataSet(inputs[0:n_examples/2],{'input':slice(0,n_inputs)}) | \
+    #             ArrayDataSet(targets[0:n_examples/2],{'target':slice(0,n_targets)}) 
+    #  testset = ArrayDataSet(inputs[n_examples/2:],{'input':slice(0,n_inputs)}) | \
+    #            ArrayDataSet(targets[n_examples/2:],{'target':slice(0,n_targets)})
+    data = hstack((inputs,targets))
+    trainset = ArrayDataSet(data[0:n_train],
+                            {'input':slice(0,n_inputs),'target':slice(n_inputs,n_inputs+n_targets)})
+    testset = ArrayDataSet(data[n_train:],
+                           {'input':slice(0,n_inputs),'target':slice(n_inputs,n_inputs+n_targets)})
+    return trainset,testset,theta