AttributeError: ‘NoneType’ object has no attribute ‘fit_generator’

代码 :

import numpy as np import pandas as pdimport osfrom tqdm import tqdm# 固定种子from numpy.random import seedseed(639)from tensorflow import set_random_seedset_random_seed(5944)# 导入float_data = pd.read_csv("train.csv", dtype={"acoustic_data": np.float32, "time_to_failure": np.float32}).values# 数据生成器的辅助函数。每时间步提取均值、标准差和分位数。# 可以轻松扩展。期望是一个二维数组。def extract_features(z):     return np.c_[z.mean(axis=1),                   z.min(axis=1),                  z.max(axis=1),                  z.std(axis=1)]# 对于给定的结束位置"last_index",我们将"x"的最后150,000个值# 分为150个长度为1000的片段。因此n_steps * step_length应等于150,000。# 从每个片段中提取一组特征。这将生成一个特征矩阵,# 其维度为(150个时间步 x 特征)。 def create_X(x, last_index=None, n_steps=150, step_length=1000):    if last_index == None:        last_index=len(x)    assert last_index - n_steps * step_length >= 0    # 重塑和近似标准化,均值为5,标准差为3。    temp = (x[(last_index - n_steps * step_length):last_index].reshape(n_steps, -1) - 5 ) / 3    # 提取完整长度为1000的序列的特征,最后100个值的特征,以及最后10个观测值的特征。     return np.c_[extract_features(temp),                 extract_features(temp[:, -step_length // 10:]),                 extract_features(temp[:, -step_length // 100:])]# 查询"create_X"以确定特征数量n_features = create_X(float_data[0:150000]).shape[1]print("我们的RNN基于%i个特征"% n_features)# 生成器无休止地选择"batch_size"个子时间序列的结束位置。对于每个结束位置,# "time_to_failure"作为目标,而特征由函数"create_X"创建。def generator(data, min_index=0, max_index=None, batch_size=16, n_steps=150, step_length=1000):    if max_index is None:        max_index = len(data) - 1    while True:        # 选择结束位置的索引        rows = np.random.randint(min_index + n_steps * step_length, max_index, size=batch_size)        # 初始化特征矩阵和目标        samples = np.zeros((batch_size, n_steps, n_features))        targets = np.zeros(batch_size, )        for j, row in enumerate(rows):            samples[j] = create_X(data[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)            targets[j] = data[row - 1, 1]        yield samples, targetsbatch_size = 64# 第二个(共16个)地震的位置。用于在训练和验证之间进行清晰的分割# 第二个地震second_earthquake = 50085877float_data[second_earthquake, 1]# 初始化生成器strain_gen = generator(float_data, batch_size=batch_size) # 用于获得更好的分数# train_gen = generator(float_data, batch_size=batch_size, min_index=second_earthquake + 1)valid_gen = generator(float_data, batch_size=batch_size, max_index=second_earthquake)# 定义模型from keras.models import Sequentialfrom keras.layers import Densefrom keras.optimizers import adamfrom keras.callbacks import ModelCheckpointfrom keras.models import Modelfrom keras.callbacks import ModelCheckpointfrom keras.models import Sequentialfrom keras.layers import Dense, Activation, Flattenfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.metrics import mean_absolute_error from matplotlib import pyplot as pltimport matplotlib.pyplot as pltimport pandas as pdimport numpy as npimport warnings model = Sequential()model.add(Dense(10, activation='relu'))model.add(Dense(1))cb = [ModelCheckpoint("model.hdf5", save_best_only=True, period=3)]# 编译并拟合模型model = model.compile(optimizer=adam(lr=0.0005), loss="mae")history = model.fit_generator(train_gen,                              steps_per_epoch=1000,                              epochs=30,                              verbose=0,                              callbacks=cb,                              validation_data=valid_gen,                              validation_steps=200)model.summary()# 可视化准确率import matplotlib.pyplot as pltdef perf_plot(history, what = 'loss'):    x = history.history[what]    val_x = history.history['val_' + what]    epochs = np.asarray(history.epoch) + 1    plt.plot(epochs, x, 'bo', label = "训练 " + what)    plt.plot(epochs, val_x, 'b', label = "验证 " + what)    plt.title("训练和验证 " + what)    plt.xlabel("轮次")    plt.legend()    plt.show()    return Noneperf_plot(history)# 加载提交文件submission = pd.read_csv('sample_submission.csv', index_col='seg_id', dtype={"time_to_failure": np.float32})# 加载每个测试数据,创建特征矩阵,获取数值预测for i, seg_id in enumerate(tqdm(submission.index)):  #  print(i)    seg = pd.read_csv('../test/' + seg_id + '.csv')    x = seg['acoustic_data'].values    submission.time_to_failure[i] = model.predict(np.expand_dims(create_X(x), 0))submission.head()# 保存submission.to_csv('submissionearth.csv')

我遇到的错误 :

Traceback (most recent call last):

File “”, line 1, in model.fit_generator(train_gen,

AttributeError: ‘NoneType’ object has no attribute ‘fit_generator’

我还导入了Keras.models,其中包含fit_generator,并尝试使用fit代替fit_generator,但仍然无法解决问题。

期待一些帮助!


回答:

你的问题在这里:

model = model.compile(optimizer=adam(lr=0.0005), loss="mae")history = model.fit_generator(train_gen,                          steps_per_epoch=1000,                          epochs=30,                          verbose=0,                          callbacks=cb,                          validation_data=valid_gen,                          validation_steps=200)

你不应该将model.compile(..)赋值给model,因为它不返回任何东西,取而代之的是那行代码应该只写成model.compile(optimizer=adam(lr=0.0005), loss="mae"),所以就改成这样

model.compile(optimizer=adam(lr=0.0005), loss="mae")history = model.fit_generator(train_gen,                          steps_per_epoch=1000,                          epochs=30,                          verbose=0,                          callbacks=cb,                          validation_data=valid_gen,                          validation_steps=200)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注