GeezerVison is showing github.com — use borrow=True when making shared variable to make the example work … · alabarga/DeepLearningTutorials@8596e69
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: code/cA.py +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,16 +133,18 @@ def __init__(self, numpy_rng, input=None, n_visible=784, n_hidden=100, | 133 | 133 | high=4 * numpy.sqrt(6. / (n_hidden + n_visible)), | 134 | 134 | size=(n_visible, n_hidden)), | 135 | 135 | dtype=theano.config.floatX) | 136 | | - W = theano.shared(value=initial_W, name='W') | | 136 | + W = theano.shared(value=initial_W, name='W', borrow=True) | 137 | 137 |
| 138 | 138 | if not bvis: | 139 | 139 | bvis = theano.shared(value=numpy.zeros(n_visible, | 140 | | - dtype=theano.config.floatX)) | | 140 | + dtype=theano.config.floatX), | | 141 | + borrow=True) | 141 | 142 |
| 142 | 143 | if not bhid: | 143 | 144 | bhid = theano.shared(value=numpy.zeros(n_hidden, | 144 | 145 | dtype=theano.config.floatX), | 145 | | - name='b') | | 146 | + name='b', | | 147 | + borrow=True) | 146 | 148 |
| 147 | 149 | self.W = W | 148 | 150 | # b corresponds to the bias of the hidden | |
Collapse file Copy file name to clipboardExpand all lines: code/dA.py +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -139,15 +139,18 @@ def __init__(self, numpy_rng, theano_rng=None, input=None, | 139 | 139 | low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)), | 140 | 140 | high=4 * numpy.sqrt(6. / (n_hidden + n_visible)), | 141 | 141 | size=(n_visible, n_hidden)), dtype=theano.config.floatX) | 142 | | - W = theano.shared(value=initial_W, name='W') | | 142 | + W = theano.shared(value=initial_W, name='W', borrow=True) | 143 | 143 |
| 144 | 144 | if not bvis: | 145 | 145 | bvis = theano.shared(value=numpy.zeros(n_visible, | 146 | | - dtype=theano.config.floatX)) | | 146 | + dtype=theano.config.floatX), | | 147 | + borrow=True) | 147 | 148 |
| 148 | 149 | if not bhid: | 149 | 150 | bhid = theano.shared(value=numpy.zeros(n_hidden, | 150 | | - dtype=theano.config.floatX), name='b') | | 151 | + dtype=theano.config.floatX), | | 152 | + name='b', | | 153 | + borrow=True) | 151 | 154 |
| 152 | 155 | self.W = W | 153 | 156 | # b corresponds to the bias of the hidden | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_cg.py +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,7 +80,8 @@ def __init__(self, input, n_in, n_out): | 80 | 80 | # n_in*n_out + n_out elements | 81 | 81 | self.theta = theano.shared(value=numpy.zeros(n_in * n_out + n_out, | 82 | 82 | dtype=theano.config.floatX), | 83 | | - name='theta') | | 83 | + name='theta', | | 84 | + borrow=True) | 84 | 85 | # W is represented by the fisr n_in*n_out elements of theta | 85 | 86 | self.W = self.theta[0:n_in * n_out].reshape((n_in, n_out)) | 86 | 87 | # b is the rest (last n_out elements) | @@ -154,7 +155,7 @@ def cg_optimization_mnist(n_epochs=50, mnist_pkl_gz='../data/mnist.pkl.gz'): | 154 | 155 | train_set, valid_set, test_set = cPickle.load(f) | 155 | 156 | f.close() | 156 | 157 |
| 157 | | - def shared_dataset(data_xy): | | 158 | + def shared_dataset(data_xy, borrow=True): | 158 | 159 | """ Function that loads the dataset into shared variables | 159 | 160 |
| 160 | 161 | The reason we store our dataset in shared variables is to allow | @@ -165,9 +166,11 @@ def shared_dataset(data_xy): | 165 | 166 | """ | 166 | 167 | data_x, data_y = data_xy | 167 | 168 | shared_x = theano.shared(numpy.asarray(data_x, | 168 | | - dtype=theano.config.floatX)) | | 169 | + dtype=theano.config.floatX), | | 170 | + borrow=borrow) | 169 | 171 | shared_y = theano.shared(numpy.asarray(data_y, | 170 | | - dtype=theano.config.floatX)) | | 172 | + dtype=theano.config.floatX), | | 173 | + borrow=borrow) | 171 | 174 | # When storing data on the GPU it has to be stored as floats | 172 | 175 | # therefore we will store the labels as ``floatX`` as well | 173 | 176 | # (``shared_y`` does exactly that). But during our computations | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_sgd.py +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,11 +76,11 @@ def __init__(self, input, n_in, n_out): | 76 | 76 | # initialize with 0 the weights W as a matrix of shape (n_in, n_out) | 77 | 77 | self.W = theano.shared(value=numpy.zeros((n_in, n_out), | 78 | 78 | dtype=theano.config.floatX), | 79 | | - name='W') | | 79 | + name='W', borrow=True) | 80 | 80 | # initialize the baises b as a vector of n_out 0s | 81 | 81 | self.b = theano.shared(value=numpy.zeros((n_out,), | 82 | 82 | dtype=theano.config.floatX), | 83 | | - name='b') | | 83 | + name='b', borrow=True) | 84 | 84 |
| 85 | 85 | # compute vector of class-membership probabilities in symbolic form | 86 | 86 | self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b) | @@ -176,7 +176,7 @@ def load_data(dataset): | 176 | 176 | #the number of rows in the input. It should give the target | 177 | 177 | #target to the example with the same index in the input. | 178 | 178 |
| 179 | | - def shared_dataset(data_xy): | | 179 | + def shared_dataset(data_xy, borrow=True): | 180 | 180 | """ Function that loads the dataset into shared variables | 181 | 181 |
| 182 | 182 | The reason we store our dataset in shared variables is to allow | @@ -187,9 +187,11 @@ def shared_dataset(data_xy): | 187 | 187 | """ | 188 | 188 | data_x, data_y = data_xy | 189 | 189 | shared_x = theano.shared(numpy.asarray(data_x, | 190 | | - dtype=theano.config.floatX)) | | 190 | + dtype=theano.config.floatX), | | 191 | + borrow=borrow) | 191 | 192 | shared_y = theano.shared(numpy.asarray(data_y, | 192 | | - dtype=theano.config.floatX)) | | 193 | + dtype=theano.config.floatX), | | 194 | + borrow=borrow) | 193 | 195 | # When storing data on the GPU it has to be stored as floats | 194 | 196 | # therefore we will store the labels as ``floatX`` as well | 195 | 197 | # (``shared_y`` does exactly that). But during our computations | |
Collapse file Copy file name to clipboardExpand all lines: code/mlp.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,11 +86,11 @@ def __init__(self, rng, input, n_in, n_out, W=None, b=None, | 86 | 86 | if activation == theano.tensor.nnet.sigmoid: | 87 | 87 | W_values *= 4 | 88 | 88 |
| 89 | | - W = theano.shared(value=W_values, name='W') | | 89 | + W = theano.shared(value=W_values, name='W', borrow=True) | 90 | 90 |
| 91 | 91 | if b is None: | 92 | 92 | b_values = numpy.zeros((n_out,), dtype=theano.config.floatX) | 93 | | - b = theano.shared(value=b_values, name='b') | | 93 | + b = theano.shared(value=b_values, name='b', borrow=True) | 94 | 94 |
| 95 | 95 | self.W = W | 96 | 96 | self.b = b | |
Collapse file Copy file name to clipboardExpand all lines: code/rbm.py +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,17 +72,19 @@ def __init__(self, input=None, n_visible=784, n_hidden=500, \ | 72 | 72 | size=(n_visible, n_hidden)), | 73 | 73 | dtype=theano.config.floatX) | 74 | 74 | # theano shared variables for weights and biases | 75 | | - W = theano.shared(value=initial_W, name='W') | | 75 | + W = theano.shared(value=initial_W, name='W', borrow=True) | 76 | 76 |
| 77 | 77 | if hbias is None: | 78 | 78 | # create shared variable for hidden units bias | 79 | 79 | hbias = theano.shared(value=numpy.zeros(n_hidden, | 80 | | - dtype=theano.config.floatX), name='hbias') | | 80 | + dtype=theano.config.floatX), | | 81 | + name='hbias', borrow=True) | 81 | 82 |
| 82 | 83 | if vbias is None: | 83 | 84 | # create shared variable for visible units bias | 84 | 85 | vbias = theano.shared(value=numpy.zeros(n_visible, | 85 | | - dtype=theano.config.floatX), name='vbias') | | 86 | + dtype=theano.config.floatX), | | 87 | + name='vbias', borrow=True) | 86 | 88 |
| 87 | 89 | # initialize input layer for standalone RBM or layer0 of DBN | 88 | 90 | self.input = input | @@ -352,7 +354,8 @@ def test_rbm(learning_rate=0.1, training_epochs=15, | 352 | 354 | # initialize storage for the persistent chain (state = hidden | 353 | 355 | # layer of chain) | 354 | 356 | persistent_chain = theano.shared(numpy.zeros((batch_size, n_hidden), | 355 | | - dtype=theano.config.floatX)) | | 357 | + dtype=theano.config.floatX), | | 358 | + borrow=True) | 356 | 359 |
| 357 | 360 | # construct the RBM class | 358 | 361 | rbm = RBM(input=x, n_visible=28 * 28, | |
|