【问题标题】:Keras Dense Layer Error: TypeError: 'int' object is not callableKeras 密集层错误:TypeError:“int”对象不可调用
【发布时间】:2017-01-30 21:32:41
【问题描述】:

我正在尝试通过以下链接可视化 keras 中每个卷积层的输出:MNIST Visualisation。我已经修改了一些层以消除错误,但现在我遇到了密集层错误。

np.set_printoptions(precision=5, suppress=True)
np.random.seed(1337) # for reproducibility

nb_classes = 10

# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data("mnist.pkl")

X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

i = 4600
pl.imshow(X_train[i, 0], interpolation='nearest', cmap=cm.binary)
print("label : ", Y_train[i,:])

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape = (1,28,28))) #changed border_mode from full -> valid
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(32, 32, 3))

convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(32*196, 128)) #ERROR HERE

高度赞赏任何意见或建议。谢谢。

【问题讨论】:

    标签: neural-network keras conv-neural-network keras-layer


    【解决方案1】:

    如果您查看Dense 层的文档,您会注意到它接受的第一个参数是输出的形状,第二个参数是init,它描述了层的权重是如何启动的。在您的情况下,您提供了 int 作为第二个位置参数,这导致了错误。您应该将代码更改为(假设您想要 128 维向量形式的输出):

    model.add(Dense(128))
    

    【讨论】:

    • 感谢您的评论 Marcin,我将其更改为 model.add(Dense(128)),但收到“溢出错误:范围超出有效范围”错误。你可能知道为什么吗?
    • 出现错误的行。我将它从 model.add(Dense(32*196, 128)) 更改为 model.add(Dense(128))
    • Marcin,除了你的代码,我还需要添加“from keras import backend as K.set_image_dim_ordering('th')”并更改 input_shape = (28,28,1) 的顺序而不是(1,28,28)。所以,你的回答确实有效 - 谢谢:)
    猜你喜欢
    • 2020-06-24
    • 2018-03-14
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2013-04-03
    相关资源
    最近更新 更多