various fixes

This commit is contained in:
Pawel Swietojanski 2015-10-07 01:09:01 +01:00
parent 56aabeaa51
commit 05f826d22d
3 changed files with 45 additions and 93 deletions

File diff suppressed because one or more lines are too long

View File

@ -93,7 +93,7 @@
"w_{21} & w_{22} & w_{23} \\\\\n",
"w_{31} & w_{32} & w_{33} \\\\\n",
"w_{41} & w_{42} & w_{43} \\\\\n",
"w_{51} & x_{52} & 2_{53} \\\\ \\end{array} \\right]$, bias vector $\\mathbf{b} = (b_1, b_2, b_3)$ and outputs $\\mathbf{y} = (y_1, y_2, y_3)$, one can write the transformation as follows:\n",
"w_{51} & w_{52} & w_{53} \\\\ \\end{array} \\right]$ (note, this picture was taken from the lecture slides and presents a transposed variant), bias vector $\\mathbf{b} = (b_1, b_2, b_3)$ and outputs $\\mathbf{y} = (y_1, y_2, y_3)$, one can write the transformation as follows:\n",
"\n",
"(for the $i$-th output)\n",
"\n",
@ -103,7 +103,7 @@
"\\end{equation}\n",
"$\n",
"\n",
"or the equivalent vector form (where $\\mathbf w_i$ is the $i$-th column of $\\mathbf W$):\n",
"or the equivalent vector form (where $\\mathbf w_i$ is the $i$-th column of $\\mathbf W$, but note, when we **slice** the $i$th column we will get a **vector** $\\mathbf w_i = (w_{1i}, w_{2i}, w_{3i}, w_{4i}, w_{5i})$, hence the transpose of $\\mathbf w_i$ in the below equation):\n",
"\n",
"(2) $\n",
"\\begin{equation}\n",
@ -237,7 +237,7 @@
"source": [
"## Exercise 2\n",
"\n",
"Modify the examples from Exercise 1 to perform **backward** propagation, that is, given $\\mathbf{y}$ (obtained in the previous step) and weight matrix $\\mathbf{W}$, project $\\mathbf{y}$ onto the input space $\\mathbf{x}$ (ignore or set to zero the biases towards $\\mathbf{x}$ in backward pass). Mathematically, we are interested in the following transformation: $\\mathbf{z}=\\mathbf{y}\\mathbf{W}^T$"
"Modify the examples from Exercise 1 to perform **backward** propagation, that is, given $\\mathbf{y}$ (obtained in the previous step) and weight matrix $\\mathbf{W}$, project $\\mathbf{y}$ onto the input space $\\mathbf{x}$ (ignore or set to zero the biases towards $\\mathbf{x}$ in backward pass, and note, we are **not** trying to reconstruct the original $\\mathbf{x}$). Mathematically, we are interested in the following transformation: $\\mathbf{z}=\\mathbf{y}\\mathbf{W}^T$"
]
},
{
@ -490,7 +490,7 @@
"source": [
"from mlp.dataset import MetOfficeDataProvider\n",
"\n",
"modp = MetOfficeDataProvider(10, batch_size=10, max_num_batches=2, randomize=False)\n",
"modp = MetOfficeDataProvider(10, batch_size=10, max_num_batches=3, randomize=False)\n",
"\n",
"%precision 2\n",
"for x, t in modp:\n",
@ -668,7 +668,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
"version": "2.7.10"
}
},
"nbformat": 4,

View File

@ -6,6 +6,10 @@ import cPickle
import gzip
import numpy
import os
import logging
logger = logging.getLogger(__name__)
class DataProvider(object):
@ -65,6 +69,7 @@ class MNISTDataProvider(DataProvider):
def __init__(self, dset,
batch_size=10,
max_num_batches=-1,
max_num_examples=-1,
randomize=True):
super(MNISTDataProvider, self).\
@ -78,6 +83,11 @@ class MNISTDataProvider(DataProvider):
assert max_num_batches != 0, (
"max_num_batches should be != 0"
)
if max_num_batches > 0 and max_num_examples > 0:
logger.warning("You have specified both 'max_num_batches' and " \
"a deprecead 'max_num_examples' arguments. We will " \
"use the former over the latter.")
dset_path = './data/mnist_%s.pkl.gz' % dset
assert os.path.isfile(dset_path), (
@ -88,6 +98,11 @@ class MNISTDataProvider(DataProvider):
x, t = cPickle.load(f)
self._max_num_batches = max_num_batches
#max_num_examples arg was provided for backward compatibility
#but it maps us to the max_num_batches anyway
if max_num_examples > 0 and max_num_batches < 0:
self._max_num_batches = max_num_examples / self.batch_size
self.x = x
self.t = t
self.num_classes = 10
@ -106,9 +121,9 @@ class MNISTDataProvider(DataProvider):
return numpy.random.permutation(numpy.arange(0, self.x.shape[0]))
def next(self):
print (self._curr_idx / self.batch_size)
has_enough = (self._curr_idx + self.batch_size) <= self.x.shape[0]
presented_max = (0 < self._max_num_batches < (self._curr_idx / self.batch_size))
presented_max = (0 < self._max_num_batches <= (self._curr_idx / self.batch_size))
if not has_enough or presented_max:
raise StopIteration()
@ -142,6 +157,7 @@ class MetOfficeDataProvider(DataProvider):
def __init__(self, window_size,
batch_size=10,
max_num_batches=-1,
max_num_examples=-1,
randomize=True):
super(MetOfficeDataProvider, self).\
@ -152,10 +168,20 @@ class MetOfficeDataProvider(DataProvider):
"File %s was expected to exist!." % dset_path
)
if max_num_batches > 0 and max_num_examples > 0:
logger.warning("You have specified both 'max_num_batches' and " \
"a deprecead 'max_num_examples' arguments. We will " \
"use the former over the latter.")
raw = numpy.loadtxt(dset_path, skiprows=3, usecols=range(2, 32))
self.window_size = window_size
self._max_num_batches = max_num_batches
#max_num_examples arg was provided for backward compatibility
#but it maps us to the max_num_batches anyway
if max_num_examples > 0 and max_num_batches < 0:
self._max_num_batches = max_num_examples / self.batch_size
#filter out all missing datapoints and
#flatten a matrix to a vector, so we will get
#a time preserving representation of measurments
@ -190,9 +216,9 @@ class MetOfficeDataProvider(DataProvider):
return numpy.random.permutation(numpy.arange(self.window_size, self.x.shape[0]))
def next(self):
has_enough = (self._curr_idx + self.batch_size) <= self.x.shape[0]
presented_max = (0 < self._max_num_batches < (self._curr_idx / self.batch_size))
has_enough = (self.window_size + self._curr_idx + self.batch_size) <= self.x.shape[0]
presented_max = (0 < self._max_num_batches <= (self._curr_idx / self.batch_size))
if not has_enough or presented_max:
raise StopIteration()
@ -202,7 +228,8 @@ class MetOfficeDataProvider(DataProvider):
self._rand_idx[self._curr_idx:self._curr_idx + self.batch_size]
else:
range_idx = \
numpy.arange(self._curr_idx, self._curr_idx + self.batch_size)
numpy.arange(self.window_size + self._curr_idx,
self.window_size + self._curr_idx + self.batch_size)
#build slicing matrix of size minibatch, which will contain batch_size
#rows, each keeping indexes that selects windows_size+1 [for (x,t)] elements
@ -215,7 +242,7 @@ class MetOfficeDataProvider(DataProvider):
range_idx[i] - self.window_size - 1,
-1,
dtype=numpy.int32)[::-1]
#here we use advanced indexing to select slices from observation vector
#last column of rval_x makes our targets t (as we splice window_size + 1
tmp_x = self.x[range_slices]