如何循环通过Keras拟合功能?

时间:2018-05-16 15:50:17

标签: python tensorflow machine-learning keras models

不必写这500次:

model.fit(
      x[i],
      y[i],
      batch_size=batch,
      epochs=epochs,
      validation_split=0.05)

我想通过I'拟合的数据集来循环这个。但是,当我这样做时,第一个适合度量标准如下:

loss: nan - acc: 7.4635e-04 - val_loss: nan - val_acc: 0.0000e+00

在此之后的每一次运行,其指标都是:

loss: nan - acc: 0.0000e+00 - val_loss: nan - val_acc: 0.0000e+00

现在我没有val_lossval_acc 0和nan是好的,但至少在那里acc但是在第一次适合之后,所有指标基本上都是空的。

编辑:

这是我格式化数据的代码。 x[i]是所有数据,其中x[0]将是第一个完整数据集。

    x_train = [None] * len(os.listdir('/'))
    y_train = [None] * len(os.listdir('/'))
    x_test = [None] * len(os.listdir('/'))
    y_test = [None] * len(os.listdir('/'))


def format_data(data, seq_len, normalise_window):
    sequence_length = seq_len + 1
    result = []
    for index in range(len(data) - sequence_length):
        result.append(data[index: index + sequence_length])

    if normalise_window:
        result = normalise_windows(result)

    result = np.array(result)


    row = round(0.85 * result.shape[0])
    train = result[:int(row), :]
    np.random.shuffle(train)
    x_train = train[:, :-1]
    y_train = train[:, -1]
    x_test = result[int(row):, :-1]
    y_test = result[int(row):, -1]

    x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
    x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

    return [x_train, y_train, x_test, y_test]

for i, filename in enumerate(os.listdir('/directory')):
        try:
          print"starting file :",filename
          data = pd.read_csv('directory/{0}'.format(filename), index_col=0, header=0)
          data['open'].replace('nan',np.nan)
          data.dropna()
          new = data['open'].tolist()


          x_train[i], y_train[i], x_test[i], y_test[i] = format_data(new, seq_len, True)
        except:
          pass

for i in range(len(x_train)):
        print"starting fit :",i,"of length",len(x_train)

        try:
          model.fit(
            x_train[i],
            y_train[i],
            batch_size=batch,
            epochs=epochs,
            validation_split=0.05)
        except:
          pass

我如何实现我想要做的事情?

1 个答案:

答案 0 :(得分:-1)

尝试删除for块并尝试插入此代码

model.fit(
        x_train,
        y_train,
        batch_size=batch,
        epochs=epochs,
        validation_split=0.05)

我希望这对你有所帮助。 我想" nan"因为你将一个值传递给模型,所以尝试传递张量本身(希望你在损失n val_loss中获得一些价值)。