braingineer / ikelos Goto Github PK
View Code? Open in Web Editor NEWa keras toolkit
License: MIT License
a keras toolkit
License: MIT License
Hey thanks for implementing the CWRNN! I tried it out with the Theano backend and it worked really well, but when I tried it with the tensorflow backend, it fails with
ValueError: Expected concat_dim in range [0, 0), but got 1
Here's a small example (which is a part of a bigger network) which fails for me.
model = Sequential()
periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 32, 64, 128, 256, 512]
model.add(ClockworkRNN(output_dim=len(periods), period_spec=periods, input_shape=(1000, 1)))
Do you see this too? I'm using Keras 1.1.0 and I tried this with tensorflow 0.8.0, 0.9.0, 1.0.0 and 1.1.0 and got similar errors in all of them.
Full trace:
ValueErrorTraceback (most recent call last)
<ipython-input-12-be0ebdd10677> in <module>()
5
6 periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 32, 64, 128, 256, 512]
----> 7 model.add(ClockworkRNN(output_dim=len(periods), period_spec=periods, input_shape=(1000, 1), stateful = False))
8
9
/home/ubuntu/.local/lib/python2.7/site-packages/keras/models.pyc in add(self, layer)
274 else:
275 input_dtype = None
--> 276 layer.create_input_layer(batch_input_shape, input_dtype)
277
278 if len(layer.inbound_nodes) != 1:
/home/ubuntu/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in create_input_layer(self, batch_input_shape, input_dtype, name)
368 # and create the node connecting the current layer
369 # to the input layer we just created.
--> 370 self(x)
371
372 def assert_input_compatibility(self, input):
/home/ubuntu/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
512 if inbound_layers:
513 # this will call layer.build() if necessary
--> 514 self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
515 input_added = True
516
/home/ubuntu/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
570 # creating the node automatically updates self.inbound_nodes
571 # as well as outbound_nodes on inbound layers.
--> 572 Node.create_node(self, inbound_layers, node_indices, tensor_indices)
573
574 def get_output_shape_for(self, input_shape):
/home/ubuntu/.local/lib/python2.7/site-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
147
148 if len(input_tensors) == 1:
--> 149 output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
150 output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0]))
151 # TODO: try to auto-infer shape if exception is raised by get_output_shape_for
/home/ubuntu/.local/lib/python2.7/site-packages/keras/layers/recurrent.pyc in call(self, x, mask)
213 constants=constants,
214 unroll=self.unroll,
--> 215 input_length=input_shape[1])
216 if self.stateful:
217 self.updates = []
/home/ubuntu/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in rnn(step_function, inputs, initial_states, go_backwards, mask, constants, unroll, input_length)
1177 state = states[0]
1178 else:
-> 1179 state = tf.concat(1, states)
1180
1181 if mask is not None:
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(concat_dim, values, name)
695 return gen_array_ops._concat(concat_dim=concat_dim,
696 values=values,
--> 697 name=name)
698
699
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.pyc in _concat(concat_dim, values, name)
412 """
413 result = _op_def_lib.apply_op("Concat", concat_dim=concat_dim,
--> 414 values=values, name=name)
415 return result
416
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
701 op = g.create_op(op_type_name, inputs, output_types, name=scope,
702 input_types=input_types, attrs=attr_protos,
--> 703 op_def=op_def)
704 outputs = op.outputs
705 return _Restructure(ops.convert_n_to_tensor(outputs),
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
2317 original_op=self._default_original_op, op_def=op_def)
2318 if compute_shapes:
-> 2319 set_shapes_for_outputs(ret)
2320 self._add_op(ret)
2321 self._record_op_seen_by_control_dependencies(ret)
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
1709 raise RuntimeError("No shape function registered for standard op: %s"
1710 % op.type)
-> 1711 shapes = shape_func(op)
1712 if shapes is None:
1713 raise RuntimeError(
/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in _ConcatShape(op)
751 if value_shape.ndims is not None and concat_dim >= value_shape.ndims:
752 raise ValueError("Expected concat_dim in range [0, %d), but got %d" %
--> 753 (value_shape.ndims, concat_dim))
754 before = output_shape[:concat_dim].merge_with(value_shape[:concat_dim])
755 at = output_shape[concat_dim] + value_shape[concat_dim]
ValueError: Expected concat_dim in range [0, 0), but got 1
As far as I can tell this is caused by
initial_states[-1] = K.variable(0.)
,in your get_initial_states function,
K.variable(0.) not having the right shape for tf.concat. Not sure why theano works fine though..
Your code is so awesome.And if it support keras 0.3 or not?
Generates the 'periods' error when we try to save checkpointer of epochs as well as model
Hello @braingineer
Thanks for your code of the CWRNN.I am going to implement CWRNN for my work so I tried it with keras(version 1.2.2)and I found something strange.
''''''''''''''''''''''
model = Sequential()
model.add(SimpleRNN(100,
batch_input_shape=(batch_size,tsteps,1),
return_sequences=True,
stateful=False))
model.add(SimpleRNN(100,
return_sequences=False,
stateful=False))
model.add(Dense(1))
model.compile(loss='mse', optimizer=opt)
'''''''''''''''''''''''
model = Sequential()
model.add(ClockworkRNN(100,period_spec=[1,2,4,8,16],
batch_input_shape=(batch_size,tsteps,1),
return_sequences=True,
stateful=False))
model.add(ClockworkRNN(100,period_spec=[1,2,4,8,16],
return_sequences=False,
stateful=False))
model.add(Dense(1))
model.compile(loss='mse', optimizer=opt)
''''''''''''''''''''''
I got the same loss with these 2 codes.I don't know what's wrong with my code.
Thank you!
This might be a Keras problem but have you tried serializing some of the layers? I tried the following to save a model that contains SoftAttention
:
from keras.engine import Input, Model
from keras.layers import Embedding, SimpleRNN
from keras.models import model_from_json, model_from_yaml
embedding_matrix = np.random.randn(100).reshape((10, 10))
num_vocab, embedding_size = embedding_matrix.shape
input_layer = Input(shape=(10, ), dtype='int32', name="data_padded")
embedding_layer = Embedding(num_vocab, embedding_size, input_length=10, mask_zero=True)(input_layer)
rnn_layer = SimpleRNN(5, return_sequences=True)(embedding_layer)
attention_layer = SoftAttention()(rnn_layer)
loss_layer = Dense(1, activation='sigmoid')(attention_layer)
model = Model(input_layer, loss_layer)
model.summary()
model.compile('adam', loss='binary_crossentropy', metrics=['accuracy'])
x = np.random.randint(0, 10, 10).reshape(1, -1)
y = np.random.randint(0, 2, 1)
model.fit(x, y, nb_epoch=1)
path = ""
# -- save
config_text = model.to_json()
with open(path + '_config', 'w') as f:
f.write(config_text)
model.save_weights(path + '_model', overwrite=True)
# -- load
m = model_from_json(open(path + '_config', 'r').read(), custom_objects={
'SoftAttention': SoftAttention
})
m.load_weights(path + '_model')
And I get this error:
Traceback (most recent call last):
File "~/src/deep_learning/layers/attention.py", line 329, in <module>
'SoftAttention': SoftAttention
File "~/lib/python2.7/site-packages/keras/models.py", line 209, in model_from_json
return layer_from_config(config, custom_objects=custom_objects)
File "~/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 34, in layer_from_config
return layer_class.from_config(config['config'])
File "~/lib/python2.7/site-packages/keras/engine/topology.py", line 2395, in from_config
process_layer(layer_data)
File "~/lib/python2.7/site-packages/keras/engine/topology.py", line 2390, in process_layer
layer(input_tensors[0])
File "~/lib/python2.7/site-packages/keras/engine/topology.py", line 517, in __call__
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "~/lib/python2.7/site-packages/keras/engine/topology.py", line 571, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "~/lib/python2.7/site-packages/keras/engine/topology.py", line 174, in create_node
str(output_shapes))
Exception: The `get_output_shape_for` method of layer "softattention_1"" should return one shape tuple per output tensor of the layer. Found: [(None, 5)]
I think it's related to JSON not serializing optional arguments.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.