【问题标题】:Issue while trying to draw learning curve尝试绘制学习曲线时的问题
【发布时间】:2020-03-27 22:34:35
【问题描述】:

我正在尝试在 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 有问题,但我想不通":)

【问题讨论】:

    标签: python machine-learning keras


    【解决方案1】:

    查看 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)
    

    一个只有一个元素的数组

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2020-09-09
      • 2019-03-14
      • 2016-11-29
      • 2016-10-06
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 2014-03-16
      相关资源
      最近更新 更多