在TensorFlow张量误差上调用Keras卷积层

时间:2016-10-27 01:43:44

标签: keras

('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)

我遇到以下错误:

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'

由于我需要的灵活性,我在TensorFlow中使用它。但我把它分解为一个简单的例子,我无法弄清楚为什么我会因为这么简单的问题而出错。

1 个答案:

答案 0 :(得分:0)

所以我设法通过两个步骤来解决这个问题:

    一切之前
  1. K.set_learning_phase(0)
  2. 而不是Flatten,请更改为tf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())])