我正在尝试在small data set上画出学习曲线 完整代码here
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
import keras.backend as K
K.clear_session()
model = Sequential()
model.add(Dense(1, input_shape=(1,)))
model.compile(Adam(lr=0.2), "mean_squared_error")
model.fit(x,y,epochs=50)
iw = model.get_weights()
from keras.utils import to_categorical
yc= to_categorical(y)
from sklearn.model_selection import train_test_split
xtr, xts, ytr, yts = train_test_split(x,yc, test_size=0.3)
train_sizes = (len(xtr) * np.linspace(0.1, 0.99999999, 4)).astype(int)
test_scores = []
for i in train_sizes :
xtrfr, _, yrtfr, _ = train_test_split(xtr,ytr,train_size=i)
model.set_weights(iw)
res = model.fit(xtrfr, yrtfr, epochs=600)
e = model.evaluate(xts,yts)
test_scores.append(e[-1])
plt.plot(train_sizes, test_scores, label="Learning Curve")
plt.legend()
plt.show()
但我收到此错误
ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (270,)
我猜测to_categorical
出了点问题,但我无法弄清楚“:)
答案 0 :(得分:0)
查看x和y的形状表明它们是一维数组:
>>> x.shape
(10000,)
>>> y.shape
(10000,)
然而,您的模型期望一个带有input_shape=(1,)
的数组,所以首先您必须像这样重塑数据:
>>> x = np.array(x, np.float32).reshape((-1, 1))
>>> y = np.array(y, np.float32).reshape((-1, 1))
它们现在将具有以下形状:
>>> x.shape
(10000, 1)
>>> y.shape
(10000, 1)
>>> x
看起来像这样:
>>> x
array([[73.847015],
[68.781906],
[74.11011 ],
...,
[63.867992],
[69.03424 ],
[61.944244]], dtype=float32)
>>> y
array([[241.89357],
[162.31047],
[212.74086],
...,
[128.47531],
[163.85246],
[113.6491 ]], dtype=float32)
一个只有一个元素的数组