I've been tearing my hair out trying to figure this out for days. I'm using tensorflow-gpu v1.13.1 and I could only find 2 other threads even mentioning a similar error.
Recreated error:
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)
I'm trying to make a neural net to classify a file as malicious or nonmalicious. Original source code, the X_train, y_train, X_test, and y_test are all numpy arrays.
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)
This is my error:
Traceback (most recent call last): File "C:/Users/Cody/Desktop/synopsys/train.py", line 76, in model.fit_generator(generate_arrays(X_train, y_train, 500), epochs=10, steps_per_epoch=1800) File "C:\Users\Cody\AppData\Local\conda\conda\envs\emberenv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1426, in fit_generator initial_epoch=initial_epoch) File "C:\Users\Cody\AppData\Local\conda\conda\envs\emberenv\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 125, in model_iteration model, mode, class_weight=class_weight) File "C:\Users\Cody\AppData\Local\conda\conda\envs\emberenv\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 427, in _make_execution_function model._make_fit_function() File "C:\Users\Cody\AppData\Local\conda\conda\envs\emberenv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1926, in _make_fit_function '_fit_function', [self.total_loss] + metrics_tensors) AttributeError: 'Sequential' object has no attribute 'total_loss'
Any help is greatly appreciated, I've been stuck on this for way too long.