I want to train my data with a conventional neural network (CNN),I start with reshaping my data than creating my model:
model = Sequential()
input_traces = Input(shape=(3253,))
model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same',
activation='relu',input_dim=input_traces))
model.add(MaxPooling1D(pool_length=2))
model.add(Flatten())
model.add(Dense(250, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=
['accuracy'])
print(model.summary())
model.fit(x_train, y_train, batch_size=15, nb_epoch=30, show_accuracy=True,
validation_data=(x_test, y_test))
But this code gives me this error:
CNN_Based_Attack.py:139: UserWarning: Update your `Conv1D` call to the Keras 2 API: `Conv1D(activation="relu", input_shape=(None, /in..., padding="same", filters=32, kernel_size=3)`
model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu',input_dim=input_traces))
Traceback (most recent call last):
File "CNN_Based_Attack.py", line 139, in <module>
model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu',input_dim=input_traces))
File "/home/.local/lib/python2.7/site-packages/keras/models.py", line 430, in add
layer(x)
File "/home/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 557, in __call__
self.build(input_shapes[0])
File "/home/.local/lib/python2.7/site-packages/keras/layers/convolutional.py", line 134, in build
constraint=self.kernel_constraint)
File "/home/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File "/home/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 390, in add_weight
weight = K.variable(initializer(shape), dtype=dtype, name=name)
File "/home/.local/lib/python2.7/site-packages/keras/initializers.py", line 200, in __call__
scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: float() argument must be a string or a number.
I really don't understand this error. Could you please help me.
You're trying to mix the Functional API and the Sequential API.
If you want to use the Sequential API, do not use an Input layer. Please see the documentation
https://keras.io/getting-started/sequential-model-guide/
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Hello,
I am facing same issue, though I AM not mixing APIs, I am using functional API. Please help!
Hello
I also have the same problem.
And, I also use functional API only.
Traceback (most recent call last):
File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 158, in <module>
main()
File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 155, in main
eval.calculate_DNN()
File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 149, in calculate_DNN
self.CNN_with_Keras(mel,chr)
File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 117, in CNN_with_Keras
x = GRU(200, return_sequences=True, dropout=0.5)(x)
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 532, in __call__
return super(RNN, self).__call__(inputs, **kwargs)
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 493, in build
self.cell.build(step_input_shape)
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 1273, in build
constraint=self.kernel_constraint)
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/engine/base_layer.py", line 249, in add_weight
weight = K.variable(initializer(shape),
File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/initializers.py", line 209, in __call__
scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: float() argument must be a string or a number, not 'Dimension'
Hello
I also have the same problem.
And, I also use functional API only.
Traceback (most recent call last): File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 158, in <module> main() File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 155, in main eval.calculate_DNN() File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 149, in calculate_DNN self.CNN_with_Keras(mel,chr) File "/Volumes/disk2s1/experiment/Script/Python/Behavior_analysis/DNN.py", line 117, in CNN_with_Keras x = GRU(200, return_sequences=True, dropout=0.5)(x) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 532, in __call__ return super(RNN, self).__call__(inputs, **kwargs) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/engine/base_layer.py", line 431, in __call__ self.build(unpack_singleton(input_shapes)) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 493, in build self.cell.build(step_input_shape) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/layers/recurrent.py", line 1273, in build constraint=self.kernel_constraint) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/engine/base_layer.py", line 249, in add_weight weight = K.variable(initializer(shape), File "/Users/masaru/.pyenv/versions/3.7.2/lib/python3.7/site-packages/keras/initializers.py", line 209, in __call__ scale /= max(1., float(fan_in + fan_out) / 2) TypeError: float() argument must be a string or a number, not 'Dimension'
Have you solved?
I am having the same problem.
self.encoded2 = Dense(nodes, activation=act)(self.encoded2)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/layers/core.py", line 866, in build
constraint=self.kernel_constraint)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(args, *kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/base_layer.py", line 249, in add_weight
weight = K.variable(initializer(shape),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/initializers.py", line 209, in __call__
scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: float() argument must be a string or a number, not 'Dimension'
Any advice?
I encountered this issue while stacking GRU layer in a functional API fashion and solved the problem by making a minor change to the Keras source code.
The problem of this issue is that the type of a tensor shape isn't properly cast to int type recognized by float function.
In my case, I add a line of code, which transform Dimension type of a shape to int type, below the build function of recurrent.py at line 440. The code is as follow, and my Keras version is 2.1.5.
def build(self, input_shape):
# Note input_shape will be list of shapes of initial states and
# constants if these are passed in __call__.
input_shape = tuple([i if (isinstance(i, int) or i is None else i.value for i in input_shape)]) # casting "Dimension" type of input_shape's elements to "int" type
if self._num_constants is not None:
constants_shape = input_shape[-self._num_constants:]
else:
constants_shape = None
if isinstance(input_shape, list):
input_shape = input_shape[0]
If you encountered the issue when building another layer, you may find the build function in related file and modify the code like above.
I encountered this issue while stacking GRU layer in a functional API fashion and solved the problem by making a minor change to the Keras source code.
The problem of this issue is that the type of a tensor shape isn't properly cast to int type recognized by float function.In my case, I add a line of code, which transform _Dimension_ type of a shape to _int_ type, below the build function of recurrent.py at line 440. The code is as follow, and my Keras version is 2.1.5.
def build(self, input_shape): # Note input_shape will be list of shapes of initial states and # constants if these are passed in __call__. input_shape = tuple([i if (isinstance(i, int) or i is None else i.value for i in input_shape)]) # casting "Dimension" type of input_shape's elements to "int" type if self._num_constants is not None: constants_shape = input_shape[-self._num_constants:] else: constants_shape = None if isinstance(input_shape, list): input_shape = input_shape[0]If you encountered the issue when building another layer, you may find the build function in related file and modify the code like above.
Or if you use Reshape layer with tensor shape as argument, like tensor.shape[1], you can just change tensor.shape[1] to tensor.shape[1].value that is int instead of 'tensorflow.python.framework.tensor_shape.Dimension'
Most helpful comment
Or if you use Reshape layer with tensor shape as argument, like tensor.shape[1], you can just change tensor.shape[1] to tensor.shape[1].value that is int instead of 'tensorflow.python.framework.tensor_shape.Dimension'