I have sensor data like this and it's unsupervised and it has almost 800 features. my use-case is for anomaly detection. I applied Principal component analysis (PCA) for dimensionality reduction. now i got 20 Principal component which i am going to use as features to fit autoencoder. enter image description here

I am trying to fit autoencoder for anomaly detection use case following this tutorial https://engineering.taboola.com/anomaly-detection-using-lstm-autoencoder/ here is my code. But getting error Nameerror: name 'self' is not defined and please instead of downgrading try to help someone. As I am new to deep learning. I want to know as according to my use case i am i right direction and i need help to fit autoencoder to this is what i tried .

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense
import pandas as pd
import random
from sklearn.preprocessing import StandardScaler
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

df = pd.read_csv('C:/Users/user/Downloads/un-auto/mydata.csv')

df = df.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(df)
self.model.add(LSTM(units=64, input_shape=(self.train_X.shape[1],self.train_X.shape[2])))
self.model.add(LSTM(units=64, return_sequences=True))
self.model.compile(optimizer='adam', loss='mae')
self.history = self.model.fit(self.train_X, self.train_y, epochs=50, batch_size=72, validation_split=0.1, shuffle=False)
# Python
X_test_pred = self.model.predict(self.test_X)
test_mae_loss = np.mean(np.abs(X_test_pred - self.test_X), axis=1)
test_mae_loss_avg_vector = np.mean(test_mae_loss, axis=1) 

self is used in python class methods, to denote an instance of the class in python. For example in this snippet self.name will assign the given name to the instance.

class Dog:
    def __init__(self, name):
        self.name = name

Here, as you're not subclassing, you should remove the self and just use model.

self.model. -> model.

You'll also need to add a model declaration before using .add

from tensorflow.keras.models import Model

model = Model()

  • $\begingroup$ model = Model() model.add(LSTM(units=64, input_shape=(self.train_X.shape[1],self.train_X.shape[2]))) model.add(Dropout(rate=0.2)) model.add(RepeatVector(n=self.train_X.shape[1])) model.add(LSTM(units=64, return_sequences=True)) model.add(Dropout(rate=0.2)) model.add(TimeDistributed(Dense(self.train_X.shape[2]))) model.compile(optimizer='adam', loss='mae') $\endgroup$ – Alex May 27 at 3:49
  • $\begingroup$ I added like this now i got invalid syntax error model = Model() $\endgroup$ – Alex May 27 at 3:56
  • 1
    $\begingroup$ model = tf.keras.Model() and removing all references of self should solve this issue $\endgroup$ – Saurav Maheshkar May 28 at 6:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.