AttributeError:“顺序”对象没有属性“ total_loss”

时间:2019-03-11 16:35:32

标签: python tensorflow tf.keras

几天来,我一直在努力梳理头发。我正在使用tensorflow-gpu v1.13.1,即使提到类似错误也只能找到2个其他线程。

重新创建的错误:

import numpy as np
import tensorflow as tf
from tensorflow import keras
def createModel():
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(5, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(5, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))
    model.compile(optimizer='sgd',
                  loss='mean_squared_error')
    return model

def array_generator():
    yield np.array([0.1,0.2,0.3,0.4,0.5]), np.array([1])

model=createModel()
model.fit_generator(array_generator(), epochs=5, steps_per_epoch=5)

我正在尝试使用神经网络将文件分类为恶意或非恶意。原始源代码X_train,y_train,X_test和y_test都是numpy数组。

import tensorflow as tf
import numpy as np
import ember
import random
X_train, y_train, X_test, y_test = ember.read_vectorized_features("C:\\Users\Cody\Desktop\synopsys\data\ember")
metadata_dataframe = ember.read_metadata("C:\\Users\Cody\Desktop\synopsys\data\ember")


#load testing set
def loadTestSet():
    X_test_tf = tf.convert_to_tensor(X_test, np.float32)
    y_test_tf = tf.convert_to_tensor(y_test, np.float32)
    return X_test_tf, y_test_tf

#create compiled keras model
def createModel():
    model = tf.keras.models.Sequential()
    #ADD L2 REGULARIZATION LATER
    model.add(tf.keras.layers.Dense(7351, activation=tf.nn.relu))
    '''model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(4096, activation=tf.nn.relu))'''
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(4096, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(4096, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(2048, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(2048, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(2048, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(1024, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(1024, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(1024, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(1024, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))
    #adam metrhod for stochastic gradient descent
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model

def generate_arrays(features, labels, batch_size):
    batch_features=np.zeros((batch_size, 7351), dtype=np.float32)
    batch_labels=np.zeros((batch_size, 1), dtype=np.float32)
    while True:
        for i in range(batch_size):
            index=random.choice(900000,1)
            batch_features=X_train[index]
            batch_labels=y_train[index]
        yield batch_features, batch_labels

print('creating model')
model=createModel()
print('training model')
model.fit_generator(generate_arrays(X_train, y_train, 500), epochs=10, steps_per_epoch=1800)
print('testing model')
X_test_tf, y_test_tf = loadTestSet()
model.evaluate(X_test_tf, y_test_tf)

这是我的错误:

  

回溯(最近通话最近):文件   “ C:/Users/Cody/Desktop/synopsys/train.py”,第76行,在       model.fit_generator(generate_arrays(X_train,y_train,500),历元= 10,steps_per_epoch = 1800)文件   “ C:\ Users \ Cody \ AppData \ Local \ conda \ conda \ envs \ emberenv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training.py”,   在fit_generator中的第1426行       initial_epoch = initial_epoch)文件“ C:\ Users \ Cody \ AppData \ Local \ conda \ conda \ envs \ emberenv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training_generator.py”,   第125行,在model_iteration中       型号,模式,class_weight = class_weight)文件“ C:\ Users \ Cody \ AppData \ Local \ conda \ conda \ envs \ emberenv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training_generator.py”,   _make_execution_function中的第427行       model._make_fit_function()文件“ C:\ Users \ Cody \ AppData \ Local \ conda \ conda \ envs \ emberenv \ lib \ site-packages \ tensorflow \ python \ keras \ engine \ training.py”,   _make_fit_function中的第1926行       '_fit_function',[self.total_loss] + metrics_tensors)AttributeError:“顺序”对象没有属性“ total_loss”

任何帮助将不胜感激,我在此问题上坚持了太久了。

1 个答案:

答案 0 :(得分:0)

我正在帮助一个朋友解决类似的问题(AttributeError:“ Sequential”对象没有属性“ total_loss”)。经过数小时的故障排除,我们通过将tensorflow升级到2.0.0-alpha0克服了它。我们还必须做一个“点子安装枕头”。