We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
YAML serialization throws exception with Regularizers
If a model contains a regularizer, YAML serialization throws an exception. Using latest Keras 1.0.3.

Compare:

``` python
>>> model = Sequential([LSTM(20, input_shape=(2,3))])
>>> model.to_yaml()
'class_name: Sequential\nconfig:\n- class_name: LSTM\n  config:\n    U_regularizer: null\n    W_regularizer: null\n    activation: tanh\n    b_regularizer: null\n    batch_input_shape: !!python/tuple [null, 2, 3]\n    consume_less: cpu\n    dropout_U: 0.0\n    dropout_W: 0.0\n    forget_bias_init: one\n    go_backwards: false\n    init: glorot_uniform\n    inner_activation: hard_sigmoid\n    inner_init: orthogonal\n    input_dim: 3\n    input_dtype: float32\n    input_length: null\n    name: lstm_3\n    output_dim: 20\n    return_sequences: false\n    stateful: false\n    trainable: true\n    unroll: false\n'
>>> model = Sequential([LSTM(20, input_shape=(2,3), W_regularizer=l2())])
>>> model.to_yaml()
/usr/lib64/python3.4/site-packages/yaml/representer.py:135: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if data in [None, ()]:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.4/site-packages/keras/engine/topology.py", line 2391, in to_yaml
    return yaml.dump(model_config, **kwargs)
  File "/usr/lib64/python3.4/site-packages/yaml/__init__.py", line 200, in dump
    return dump_all([data], stream, Dumper=Dumper, **kwds)
  File "/usr/lib64/python3.4/site-packages/yaml/__init__.py", line 188, in dump_all
    dumper.represent(data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 26, in represent                                                                                                                                                       
    node = self.represent_data(data)                                                                                                                                                                                                         
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 203, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 116, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 195, in represent_list
    return self.represent_sequence('tag:yaml.org,2002:seq', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 91, in represent_sequence
    node_item = self.represent_data(item)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 203, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 116, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 203, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 116, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 203, in represent_dict
    return self.represent_mapping('tag:yaml.org,2002:map', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 116, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 51, in represent_data
    node = self.yaml_multi_representers[data_type](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 352, in represent_object
    return self.represent_mapping(tag+function_name, value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 116, in represent_mapping
    node_value = self.represent_data(item_value)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 47, in represent_data
    node = self.yaml_representers[data_types[0]](self, data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 282, in represent_tuple
    return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 91, in represent_sequence
    node_item = self.represent_data(item)
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 33, in represent_data
    if self.ignore_aliases(data):
  File "/usr/lib64/python3.4/site-packages/yaml/representer.py", line 135, in ignore_aliases
    if data in [None, ()]:
TypeError: data type not understood
```

Tested with LSTM & Embedding layer, W_regularizer and U_regularizer, l1 and l2. Always throws. `.to_json()` works fine. Is there some serialization code missing for l1/l2?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `keras/regularizers.py`
Content:
```
1 from __future__ import absolute_import
2 from . import backend as K
3 
4 
5 class Regularizer(object):
6     def set_param(self, p):
7         self.p = p
8 
9     def set_layer(self, layer):
10         self.layer = layer
11 
12     def __call__(self, loss):
13         return loss
14 
15     def get_config(self):
16         return {'name': self.__class__.__name__}
17 
18 
19 class WeightRegularizer(Regularizer):
20     def __init__(self, l1=0., l2=0.):
21         self.l1 = K.cast_to_floatx(l1)
22         self.l2 = K.cast_to_floatx(l2)
23         self.uses_learning_phase = True
24 
25     def set_param(self, p):
26         self.p = p
27 
28     def __call__(self, loss):
29         if not hasattr(self, 'p'):
30             raise Exception('Need to call `set_param` on '
31                             'WeightRegularizer instance '
32                             'before calling the instance. '
33                             'Check that you are not passing '
34                             'a WeightRegularizer instead of an '
35                             'ActivityRegularizer '
36                             '(i.e. activity_regularizer="l2" instead '
37                             'of activity_regularizer="activity_l2".')
38         regularized_loss = loss + K.sum(K.abs(self.p)) * self.l1
39         regularized_loss += K.sum(K.square(self.p)) * self.l2
40         return K.in_train_phase(regularized_loss, loss)
41 
42     def get_config(self):
43         return {'name': self.__class__.__name__,
44                 'l1': self.l1,
45                 'l2': self.l2}
46 
47 
48 class ActivityRegularizer(Regularizer):
49     def __init__(self, l1=0., l2=0.):
50         self.l1 = K.cast_to_floatx(l1)
51         self.l2 = K.cast_to_floatx(l2)
52         self.uses_learning_phase = True
53 
54     def set_layer(self, layer):
55         self.layer = layer
56 
57     def __call__(self, loss):
58         if not hasattr(self, 'layer'):
59             raise Exception('Need to call `set_layer` on '
60                             'ActivityRegularizer instance '
61                             'before calling the instance.')
62         regularized_loss = loss
63         for i in range(len(self.layer.inbound_nodes)):
64             output = self.layer.get_output_at(i)
65             regularized_loss += self.l1 * K.sum(K.mean(K.abs(output), axis=0))
66             regularized_loss += self.l2 * K.sum(K.mean(K.square(output), axis=0))
67         return K.in_train_phase(regularized_loss, loss)
68 
69     def get_config(self):
70         return {'name': self.__class__.__name__,
71                 'l1': self.l1,
72                 'l2': self.l2}
73 
74 
75 def l1(l=0.01):
76     return WeightRegularizer(l1=l)
77 
78 
79 def l2(l=0.01):
80     return WeightRegularizer(l2=l)
81 
82 
83 def l1l2(l1=0.01, l2=0.01):
84     return WeightRegularizer(l1=l1, l2=l2)
85 
86 
87 def activity_l1(l=0.01):
88     return ActivityRegularizer(l1=l)
89 
90 
91 def activity_l2(l=0.01):
92     return ActivityRegularizer(l2=l)
93 
94 
95 def activity_l1l2(l1=0.01, l2=0.01):
96     return ActivityRegularizer(l1=l1, l2=l2)
97 
98 
99 from .utils.generic_utils import get_from_module
100 def get(identifier, kwargs=None):
101     return get_from_module(identifier, globals(), 'regularizer',
102                            instantiate=True, kwargs=kwargs)
103
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/keras/regularizers.py b/keras/regularizers.py
--- a/keras/regularizers.py
+++ b/keras/regularizers.py
@@ -41,8 +41,8 @@
 
     def get_config(self):
         return {'name': self.__class__.__name__,
-                'l1': self.l1,
-                'l2': self.l2}
+                'l1': float(self.l1),
+                'l2': float(self.l2)}
 
 
 class ActivityRegularizer(Regularizer):
@@ -68,8 +68,8 @@
 
     def get_config(self):
         return {'name': self.__class__.__name__,
-                'l1': self.l1,
-                'l2': self.l2}
+                'l1': float(self.l1),
+                'l2': float(self.l2)}
 
 
 def l1(l=0.01):

{"golden_diff": "diff --git a/keras/regularizers.py b/keras/regularizers.py\n--- a/keras/regularizers.py\n+++ b/keras/regularizers.py\n@@ -41,8 +41,8 @@\n \n     def get_config(self):\n         return {'name': self.__class__.__name__,\n-                'l1': self.l1,\n-                'l2': self.l2}\n+                'l1': float(self.l1),\n+                'l2': float(self.l2)}\n \n \n class ActivityRegularizer(Regularizer):\n@@ -68,8 +68,8 @@\n \n     def get_config(self):\n         return {'name': self.__class__.__name__,\n-                'l1': self.l1,\n-                'l2': self.l2}\n+                'l1': float(self.l1),\n+                'l2': float(self.l2)}\n \n \n def l1(l=0.01):\n", "issue": "YAML serialization throws exception with Regularizers\nIf a model contains a regularizer, YAML serialization throws an exception. Using latest Keras 1.0.3.\n\nCompare:\n\n``` python\n>>> model = Sequential([LSTM(20, input_shape=(2,3))])\n>>> model.to_yaml()\n'class_name: Sequential\\nconfig:\\n- class_name: LSTM\\n  config:\\n    U_regularizer: null\\n    W_regularizer: null\\n    activation: tanh\\n    b_regularizer: null\\n    batch_input_shape: !!python/tuple [null, 2, 3]\\n    consume_less: cpu\\n    dropout_U: 0.0\\n    dropout_W: 0.0\\n    forget_bias_init: one\\n    go_backwards: false\\n    init: glorot_uniform\\n    inner_activation: hard_sigmoid\\n    inner_init: orthogonal\\n    input_dim: 3\\n    input_dtype: float32\\n    input_length: null\\n    name: lstm_3\\n    output_dim: 20\\n    return_sequences: false\\n    stateful: false\\n    trainable: true\\n    unroll: false\\n'\n>>> model = Sequential([LSTM(20, input_shape=(2,3), W_regularizer=l2())])\n>>> model.to_yaml()\n/usr/lib64/python3.4/site-packages/yaml/representer.py:135: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n  if data in [None, ()]:\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/usr/lib64/python3.4/site-packages/keras/engine/topology.py\", line 2391, in to_yaml\n    return yaml.dump(model_config, **kwargs)\n  File \"/usr/lib64/python3.4/site-packages/yaml/__init__.py\", line 200, in dump\n    return dump_all([data], stream, Dumper=Dumper, **kwds)\n  File \"/usr/lib64/python3.4/site-packages/yaml/__init__.py\", line 188, in dump_all\n    dumper.represent(data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 26, in represent                                                                                                                                                       \n    node = self.represent_data(data)                                                                                                                                                                                                         \n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 203, in represent_dict\n    return self.represent_mapping('tag:yaml.org,2002:map', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 116, in represent_mapping\n    node_value = self.represent_data(item_value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 195, in represent_list\n    return self.represent_sequence('tag:yaml.org,2002:seq', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 91, in represent_sequence\n    node_item = self.represent_data(item)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 203, in represent_dict\n    return self.represent_mapping('tag:yaml.org,2002:map', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 116, in represent_mapping\n    node_value = self.represent_data(item_value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 203, in represent_dict\n    return self.represent_mapping('tag:yaml.org,2002:map', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 116, in represent_mapping\n    node_value = self.represent_data(item_value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 203, in represent_dict\n    return self.represent_mapping('tag:yaml.org,2002:map', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 116, in represent_mapping\n    node_value = self.represent_data(item_value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 51, in represent_data\n    node = self.yaml_multi_representers[data_type](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 352, in represent_object\n    return self.represent_mapping(tag+function_name, value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 116, in represent_mapping\n    node_value = self.represent_data(item_value)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 47, in represent_data\n    node = self.yaml_representers[data_types[0]](self, data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 282, in represent_tuple\n    return self.represent_sequence('tag:yaml.org,2002:python/tuple', data)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 91, in represent_sequence\n    node_item = self.represent_data(item)\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 33, in represent_data\n    if self.ignore_aliases(data):\n  File \"/usr/lib64/python3.4/site-packages/yaml/representer.py\", line 135, in ignore_aliases\n    if data in [None, ()]:\nTypeError: data type not understood\n```\n\nTested with LSTM & Embedding layer, W_regularizer and U_regularizer, l1 and l2. Always throws. `.to_json()` works fine. Is there some serialization code missing for l1/l2?\n\n", "before_files": [{"content": "from __future__ import absolute_import\nfrom . import backend as K\n\n\nclass Regularizer(object):\n    def set_param(self, p):\n        self.p = p\n\n    def set_layer(self, layer):\n        self.layer = layer\n\n    def __call__(self, loss):\n        return loss\n\n    def get_config(self):\n        return {'name': self.__class__.__name__}\n\n\nclass WeightRegularizer(Regularizer):\n    def __init__(self, l1=0., l2=0.):\n        self.l1 = K.cast_to_floatx(l1)\n        self.l2 = K.cast_to_floatx(l2)\n        self.uses_learning_phase = True\n\n    def set_param(self, p):\n        self.p = p\n\n    def __call__(self, loss):\n        if not hasattr(self, 'p'):\n            raise Exception('Need to call `set_param` on '\n                            'WeightRegularizer instance '\n                            'before calling the instance. '\n                            'Check that you are not passing '\n                            'a WeightRegularizer instead of an '\n                            'ActivityRegularizer '\n                            '(i.e. activity_regularizer=\"l2\" instead '\n                            'of activity_regularizer=\"activity_l2\".')\n        regularized_loss = loss + K.sum(K.abs(self.p)) * self.l1\n        regularized_loss += K.sum(K.square(self.p)) * self.l2\n        return K.in_train_phase(regularized_loss, loss)\n\n    def get_config(self):\n        return {'name': self.__class__.__name__,\n                'l1': self.l1,\n                'l2': self.l2}\n\n\nclass ActivityRegularizer(Regularizer):\n    def __init__(self, l1=0., l2=0.):\n        self.l1 = K.cast_to_floatx(l1)\n        self.l2 = K.cast_to_floatx(l2)\n        self.uses_learning_phase = True\n\n    def set_layer(self, layer):\n        self.layer = layer\n\n    def __call__(self, loss):\n        if not hasattr(self, 'layer'):\n            raise Exception('Need to call `set_layer` on '\n                            'ActivityRegularizer instance '\n                            'before calling the instance.')\n        regularized_loss = loss\n        for i in range(len(self.layer.inbound_nodes)):\n            output = self.layer.get_output_at(i)\n            regularized_loss += self.l1 * K.sum(K.mean(K.abs(output), axis=0))\n            regularized_loss += self.l2 * K.sum(K.mean(K.square(output), axis=0))\n        return K.in_train_phase(regularized_loss, loss)\n\n    def get_config(self):\n        return {'name': self.__class__.__name__,\n                'l1': self.l1,\n                'l2': self.l2}\n\n\ndef l1(l=0.01):\n    return WeightRegularizer(l1=l)\n\n\ndef l2(l=0.01):\n    return WeightRegularizer(l2=l)\n\n\ndef l1l2(l1=0.01, l2=0.01):\n    return WeightRegularizer(l1=l1, l2=l2)\n\n\ndef activity_l1(l=0.01):\n    return ActivityRegularizer(l1=l)\n\n\ndef activity_l2(l=0.01):\n    return ActivityRegularizer(l2=l)\n\n\ndef activity_l1l2(l1=0.01, l2=0.01):\n    return ActivityRegularizer(l1=l1, l2=l2)\n\n\nfrom .utils.generic_utils import get_from_module\ndef get(identifier, kwargs=None):\n    return get_from_module(identifier, globals(), 'regularizer',\n                           instantiate=True, kwargs=kwargs)\n", "path": "keras/regularizers.py"}], "after_files": [{"content": "from __future__ import absolute_import\nfrom . import backend as K\n\n\nclass Regularizer(object):\n    def set_param(self, p):\n        self.p = p\n\n    def set_layer(self, layer):\n        self.layer = layer\n\n    def __call__(self, loss):\n        return loss\n\n    def get_config(self):\n        return {'name': self.__class__.__name__}\n\n\nclass WeightRegularizer(Regularizer):\n    def __init__(self, l1=0., l2=0.):\n        self.l1 = K.cast_to_floatx(l1)\n        self.l2 = K.cast_to_floatx(l2)\n        self.uses_learning_phase = True\n\n    def set_param(self, p):\n        self.p = p\n\n    def __call__(self, loss):\n        if not hasattr(self, 'p'):\n            raise Exception('Need to call `set_param` on '\n                            'WeightRegularizer instance '\n                            'before calling the instance. '\n                            'Check that you are not passing '\n                            'a WeightRegularizer instead of an '\n                            'ActivityRegularizer '\n                            '(i.e. activity_regularizer=\"l2\" instead '\n                            'of activity_regularizer=\"activity_l2\".')\n        regularized_loss = loss + K.sum(K.abs(self.p)) * self.l1\n        regularized_loss += K.sum(K.square(self.p)) * self.l2\n        return K.in_train_phase(regularized_loss, loss)\n\n    def get_config(self):\n        return {'name': self.__class__.__name__,\n                'l1': float(self.l1),\n                'l2': float(self.l2)}\n\n\nclass ActivityRegularizer(Regularizer):\n    def __init__(self, l1=0., l2=0.):\n        self.l1 = K.cast_to_floatx(l1)\n        self.l2 = K.cast_to_floatx(l2)\n        self.uses_learning_phase = True\n\n    def set_layer(self, layer):\n        self.layer = layer\n\n    def __call__(self, loss):\n        if not hasattr(self, 'layer'):\n            raise Exception('Need to call `set_layer` on '\n                            'ActivityRegularizer instance '\n                            'before calling the instance.')\n        regularized_loss = loss\n        for i in range(len(self.layer.inbound_nodes)):\n            output = self.layer.get_output_at(i)\n            regularized_loss += self.l1 * K.sum(K.mean(K.abs(output), axis=0))\n            regularized_loss += self.l2 * K.sum(K.mean(K.square(output), axis=0))\n        return K.in_train_phase(regularized_loss, loss)\n\n    def get_config(self):\n        return {'name': self.__class__.__name__,\n                'l1': float(self.l1),\n                'l2': float(self.l2)}\n\n\ndef l1(l=0.01):\n    return WeightRegularizer(l1=l)\n\n\ndef l2(l=0.01):\n    return WeightRegularizer(l2=l)\n\n\ndef l1l2(l1=0.01, l2=0.01):\n    return WeightRegularizer(l1=l1, l2=l2)\n\n\ndef activity_l1(l=0.01):\n    return ActivityRegularizer(l1=l)\n\n\ndef activity_l2(l=0.01):\n    return ActivityRegularizer(l2=l)\n\n\ndef activity_l1l2(l1=0.01, l2=0.01):\n    return ActivityRegularizer(l1=l1, l2=l2)\n\n\nfrom .utils.generic_utils import get_from_module\ndef get(identifier, kwargs=None):\n    return get_from_module(identifier, globals(), 'regularizer',\n                           instantiate=True, kwargs=kwargs)\n", "path": "keras/regularizers.py"}]}