简体   繁体   English

AttributeError:“ NoneType”对象没有属性“ fit_generator”

[英]AttributeError: 'NoneType' object has no attribute 'fit_generator'

Code : 代码:

import numpy as np 
import pandas as pd
import os
from tqdm import tqdm

# Fix seeds
from numpy.random import seed
seed(639)
from tensorflow import set_random_seed
set_random_seed(5944)

# Import
float_data = pd.read_csv("train.csv", dtype={"acoustic_data": np.float32, "time_to_failure": np.float32}).values

# Helper function for the data generator. Extracts mean, standard deviation, and quantiles per time step.
# Can easily be extended. Expects a two dimensional array.
def extract_features(z):
     return np.c_[z.mean(axis=1), 
                  z.min(axis=1),
                  z.max(axis=1),
                  z.std(axis=1)]

# For a given ending position "last_index", we split the last 150'000 values 
# of "x" into 150 pieces of length 1000 each. So n_steps * step_length should equal 150'000.
# From each piece, a set features are extracted. This results in a feature matrix 
# of dimension (150 time steps x features).  
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

    # Reshaping and approximate standardization with mean 5 and std 3.
    temp = (x[(last_index - n_steps * step_length):last_index].reshape(n_steps, -1) - 5 ) / 3

    # Extracts features of sequences of full length 1000, of the last 100 values and finally also 
    # of the last 10 observations. 
    return np.c_[extract_features(temp),
                 extract_features(temp[:, -step_length // 10:]),
                 extract_features(temp[:, -step_length // 100:])]

# Query "create_X" to figure out the number of features
n_features = create_X(float_data[0:150000]).shape[1]
print("Our RNN is based on %i features"% n_features)

# The generator endlessly selects "batch_size" ending positions of sub-time series. For each ending position,
# the "time_to_failure" serves as target, while the features are created by the function "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:
        # Pick indices of ending positions
        rows = np.random.randint(min_index + n_steps * step_length, max_index, size=batch_size)

        # Initialize feature matrices and targets
        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, targets

batch_size = 64

# Position of second (of 16) earthquake. Used to have a clean split
# between train and validation
second_earthquake = 50085877
float_data[second_earthquake, 1]

# Initialize generators
train_gen = generator(float_data, batch_size=batch_size) # Use this for better score
# 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)

# Define model
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import adam
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error 
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings 



model = Sequential()
model.add(Dense(10, activation='relu'))
model.add(Dense(1))


cb = [ModelCheckpoint("model.hdf5", save_best_only=True, period=3)]
# Compile and fit model
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()
# Visualize accuracies
import matplotlib.pyplot as plt

def 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 = "Training " + what)
    plt.plot(epochs, val_x, 'b', label = "Validation " + what)
    plt.title("Training and validation " + what)
    plt.xlabel("Epochs")
    plt.legend()
    plt.show()
    return None

perf_plot(history)

# Load submission file
submission = pd.read_csv('sample_submission.csv', index_col='seg_id', dtype={"time_to_failure": np.float32})

# Load each test data, create the feature matrix, get numeric prediction
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()

# Save
submission.to_csv('submissionearth.csv')

Error I got : 我得到的错误:

Traceback (most recent call last): 追溯(最近一次通话):

File "", line 1, in model.fit_generator(train_gen, 在model.fit_generator(train_gen,

AttributeError: 'NoneType' object has no attribute 'fit_generator' AttributeError:“ NoneType”对象没有属性“ fit_generator”

I have imported Keras.models also which contain fit_generator and have tried using fit instead of fit_generator but still couldn't solve it. 我已经导入了也包含fit_generator的Keras.models ,并尝试使用fit代替fit_generator,但仍然无法解决。

Looking forward for some help! 期待一些帮助!

The model is None. 该模型为无。 Check this line for the reason: 检查此行的原因:

model = model.compile(optimizer=adam(lr=0.0005), loss="mae")

your issue is here: 您的问题在这里:

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)

you shouldn't assign model.compile(..) as it doesn't return anything, instead that line should read just model.compile(optimizer=adam(lr=0.0005), loss="mae") so just make it look like this 您不应该分配model.compile(..)因为它不会返回任何内容,相反,该行应该只读取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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性错误:'NoneType' object 在使用 tf.keras fit_generator() 时没有属性 'shape' - Attribute error: 'NoneType' object has no attribute 'shape' when using tf.keras fit_generator() “DNN”对象在 ImageDataGenerator()-keras-python 中没有属性“fit_generator” - 'DNN' object has no attribute 'fit_generator' in ImageDataGenerator() - keras - python AttributeError: 'NoneType' object 没有属性 'predict_generator' - AttributeError: 'NoneType' object has no attribute 'predict_generator' AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' fit_generator() 在掩码 R CNN 中返回 NoneType 而不是 History object - fit_generator() returns NoneType instead of History object in Mask R CNN AttributeError:'NoneType'对象没有属性'mention' - AttributeError: 'NoneType' object has no attribute 'mention' Python,AttributeError:“ NoneType”对象没有属性“ show” - Python, AttributeError: 'NoneType' object has no attribute 'show' AttributeError:'NoneType'对象没有属性'_root' - AttributeError: 'NoneType' object has no attribute '_root' AttributeError: 'NoneType' 对象没有属性 'setText' - AttributeError: 'NoneType' object has no attribute 'setText'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM