【问题标题】:Calling Keras Convolutional Layer on TensorFlow Tensor Error在 TensorFlow 张量错误上调用 Keras 卷积层
【发布时间】:2016-10-27 01:43:44
【问题描述】:

('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 一起使用。但是我把它分解成一个简单的例子,我不明白为什么我会因为这样一个简单的问题而出错。

【问题讨论】:

    标签: keras


    【解决方案1】:

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

    1. K.set_learning_phase(0) 在一切之前。
    2. Flatten 改为tf.reshape(x, [-1, np.prod(x.get_shape()[1:].as_list())])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多