question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling Keras Convolutional Layer on TensorFlow Tensor Error

See original GitHub issue

Calling Keras Convolutional Layer on TensorFlow Tensor Error.

(‘x_train shape:’, (50000, 32, 32, 3))

 # Basic info
self.batch_num = 50
self.img_row = 32
self.img_col = 32
self.img_channels = 3
self.nb_classes = 10
img = tf.placeholder(tf.float32, shape=(self.batch_num, self.img_col, self.img_row, self.img_channels))
labels = tf.placeholder(tf.float32, shape=(self.batch_num, self.nb_classes))

x = Convolution2D(16, 3, 3, border_mode='same')(img)
x = BatchNormalization(axis=3)(x)
x = Activation('relu')(x)
x = AveragePooling2D(pool_size=(8, 8), strides=None, border_mode='valid')(x)
x = Flatten()(x)

preds = Dense(self.nb_classes, activation='softmax')(x)

I tried using the following and it still gives the same error. It seems we can’t call a Convolution2D layer on an input? @fchollet

If I do not use a placeholder and use inputs = Input(shape=(self.img_rows, self.img_cols, self.img_channels), it works.

I noticed a similar error persisting with https://github.com/fchollet/keras/issues/3450

img = K.placeholder(ndim=4)

I’m having the following error:

Traceback (most recent call last):
  File "cnn.py", line 176, in <module>
    a.step()
  File "cnn.py.py", line 156, in step
    preds = Dense(self.nb_classes, activation='softmax')(x)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 487, in __call__
    self.build(input_shapes[0])
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 695, in build
    name='{}_W'.format(self.name))
  File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 58, in glorot_uniform
    s = np.sqrt(6. / (fan_in + fan_out))
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

I’m using this with TensorFlow due to the flexibility I need. But I broke it down to a simple example and I can’t figure out why I’m getting an error for such a simple problem.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ritchiengcommented, Oct 27, 2016

So I managed to solve this with 2 steps:

  1. K.set_learning_phase(0) before everything.
  2. Instead of Flatten, change to tf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())]).

I gathered this fix from studying a few issues: https://github.com/fchollet/keras/pull/3253 https://github.com/fchollet/keras/issues/3450 https://github.com/fchollet/keras/pull/3253

I personally think this deserves to be updated in Keras 😃

0reactions
stale[bot]commented, May 23, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tf.keras.layers.Conv2D | TensorFlow v2.11.0
This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs. If use_bias is True, ......
Read more >
Keras: Exception encountered when calling layer "conv2d_19 ...
Anyone know how to fix this problem and what cause the problem ? Here are the Model. def create_model(): model = Sequential() model.add(Conv2D( ......
Read more >
Error when speeding up Custom Layers in Keras with @tf ...
The solution here is that your `call` method should look like `call(self, inputs, training=False)`, and every time you use your layer, you ...
Read more >
Keras: the Python deep learning API
Keras is a central part of the tightly-connected TensorFlow 2 ecosystem, covering every step of the machine learning workflow, from data management to ......
Read more >
Machine Learning Glossary - Google Developers
Modern ML APIs like TensorFlow now implement backpropagation for you. Phew! ... A deep model is also called a deep neural network.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found