打电话给Dense时Keras出现问题

时间:2018-10-22 09:34:34

标签: python neural-network keras recurrent-neural-network

我一直试图在下面运行此代码,并且不断抛出错误。

这是我试图在ipython上运行的代码。有人可以告诉我问题是什么吗?

import numpy as np
import matplotlib.pyplot as plt
from util import getKaggleMNIST
from keras.models import Model
from keras.layers import Dense, Activation, Input

xtrain, ytrain, xtest, ytest = getKaggleMNIST()

N, D = xtrain.shape
k = len(set(ytrain))

i = Input(shape=(D,))
x = Dense(500, activation='relu')(i)
x = Dense(300, activation='relu')(x)
x = Dense(k, activation='softmax')(k)

model = Model(inputs=i, outputs=x)

model.compile( loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )

r = model.fit(Xtrain, Ytrain, validation_data=(Xtest, Ytest), epochs=15, batch_size=32)

print("Returned:", r)

plt.plot(r.history['loss'], label='loss') 
plt.plot(r.history['val_loss'], label='val_loss') 
plt.legend() 
plt.show()

这是错误:

ValueError: Layer dense_3 was called with an input that isn't a symbolic tensor. Received type: <type 'int'>. Full input: [10]. All inputs to the layer should be tensors.

谢谢。

1 个答案:

答案 0 :(得分:0)

x = Dense(k, activation='softmax')(k)替换为x = Dense(k, activation='softmax')(x)