【发布时间】:2019-03-26 08:17:12
【问题描述】:
我一直试图在下面运行这段代码,但它一直抛出一个错误。
这是我试图在 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.
先谢谢了。
【问题讨论】:
-
不应该是 b x = Dense(k, activation='softmax')(x) 代表 x = Dense(k, activation='softmax')(k) 吗?
-
非常感谢@JérémyBlain :)
标签: python neural-network keras recurrent-neural-network