Merge pull request #67 from CSTR-Edinburgh/mlp2017-8/coursework_2

Fix bug that rendered StochasticLayerWithParameters un-updatable
This commit is contained in:
AntreasAntoniou 2017-11-25 19:30:54 +00:00 committed by GitHub
commit d14e05706f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -80,7 +80,7 @@ class MultipleLayerModel(object):
"""A list of all of the parameters of the model.""" """A list of all of the parameters of the model."""
params = [] params = []
for layer in self.layers: for layer in self.layers:
if isinstance(layer, LayerWithParameters): if isinstance(layer, LayerWithParameters) or isinstance(layer, StochasticLayerWithParameters):
params += layer.params params += layer.params
return params return params
@ -131,7 +131,7 @@ class MultipleLayerModel(object):
inputs = activations[-i - 2] inputs = activations[-i - 2]
outputs = activations[-i - 1] outputs = activations[-i - 1]
grads_wrt_inputs = layer.bprop(inputs, outputs, grads_wrt_outputs) grads_wrt_inputs = layer.bprop(inputs, outputs, grads_wrt_outputs)
if isinstance(layer, LayerWithParameters): if isinstance(layer, LayerWithParameters) or isinstance(layer, StochasticLayerWithParameters):
grads_wrt_params += layer.grads_wrt_params( grads_wrt_params += layer.grads_wrt_params(
inputs, grads_wrt_outputs)[::-1] inputs, grads_wrt_outputs)[::-1]
grads_wrt_outputs = grads_wrt_inputs grads_wrt_outputs = grads_wrt_inputs

View File

@ -19,8 +19,6 @@ def generate_inputs(student_id):
tests[0, 1, :, :] = float(student_number[7]) / 10 - 5 tests[0, 1, :, :] = float(student_number[7]) / 10 - 5
return tests return tests
test_inputs = generate_inputs(student_id) test_inputs = generate_inputs(student_id)
test_grads_wrt_outputs = np.arange(-20, 16).reshape((2, 2, 3, 3)) test_grads_wrt_outputs = np.arange(-20, 16).reshape((2, 2, 3, 3))
inputs = np.arange(96).reshape((2, 3, 4, 4)) inputs = np.arange(96).reshape((2, 3, 4, 4))
@ -36,11 +34,26 @@ conv_bprop = activation_layer.bprop(
test_inputs, conv_fprop, test_grads_wrt_outputs) test_inputs, conv_fprop, test_grads_wrt_outputs)
conv_grads_wrt_params = activation_layer.grads_wrt_params(test_inputs, conv_grads_wrt_params = activation_layer.grads_wrt_params(test_inputs,
test_grads_wrt_outputs) test_grads_wrt_outputs)
test_output = "ConvolutionalLayer:\nFprop: {}\nBprop: {}\n" \ test_output = "ConvolutionalLayer:\nFprop: {}\nBprop: {}\n" \
"Grads_wrt_params: {}\n".format(conv_fprop, "Grads_wrt_params: {}\n".format(conv_fprop,
conv_bprop, conv_bprop,
conv_grads_wrt_params) conv_grads_wrt_params)
cross_correlation_kernels = kernels[:, :, ::-1, ::-1]
activation_layer = ConvolutionalLayer(num_input_channels=3, num_output_channels=2, input_dim_1=4, input_dim_2=4,
kernel_dim_1=2, kernel_dim_2=2)
activation_layer.params = [cross_correlation_kernels, biases]
conv_fprop = activation_layer.fprop(test_inputs)
conv_bprop = activation_layer.bprop(
test_inputs, conv_fprop, test_grads_wrt_outputs)
conv_grads_wrt_params = activation_layer.grads_wrt_params(test_inputs,
test_grads_wrt_outputs)
test_cross_correlation_output = "Cross_Correlation_ConvolutionalLayer:\nFprop: {}\nBprop: {}\n" \
"Grads_wrt_params: {}\n".format(conv_fprop,
conv_bprop,
conv_grads_wrt_params)
test_output = test_output + "\n" + test_cross_correlation_output
with open("{}_conv_test_file.txt".format(student_id), "w+") as out_file: with open("{}_conv_test_file.txt".format(student_id), "w+") as out_file:
out_file.write(test_output) out_file.write(test_output)