Keras的model.fit_generator()返回的结果与model.fit()不同。

时间:2019-07-17 13:43:33

标签: python machine-learning keras

我需要使用model.fit_generator()避免将大量数据馈入RAM内存。但是,使用model.fit_generator()时的行为与model.fit()完全不同。

library(dplyr)
library(tidyr)
df1 %>%
      separate(ID, into = c("NO.", "type", "treatment"), 
                 sep="\\.", remove = FALSE, convert = TRUE)

所以上面的代码的行为如下所示

Image 1

def generator(inputs, targets,batch_size=128,window=300):

    num_of_steps = int((len(inputs)-window+1) / batch_size)# - 1
    indexes=list(range(num_of_steps))

    np.random.shuffle(indexes)


    while True:
        for ei, e in enumerate(indexes):
          offset = e * batch_size

          mainpart = inputs
          meterpart = targets
          mainpart = np.array(mainpart)
          indexer = np.arange(window)[None, :] + np.arange(len(mainpart) window+1)[offset:offset + batch_size, None]

          mainpart = mainpart[indexer]
          meterpart = meterpart[indexer]

          X = np.reshape(mainpart, (batch_size, window, 1))
          Y = np.reshape(meterpart, (batch_size,window))

          yield X,Y


t = generator(train_x[0], train_y[0])
model.fit_generator(t, 
                    steps_per_epoch = int((len(train_x[0])-window+1) / batch_size), 
                    epochs=num_epochs)

而model.fit()则以这种方式运行

enter image description here

请,有人可以帮忙吗?

0 个答案:

没有答案