question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TensorFlow Implementation

See original GitHub issue

First of all, I want to thank @wuyifan18 for this awesome code. I am trying to translate this Pytorch code into Tensorflow and I am having some trouble. I am wondering if anyone here has done it or can take a look at my implementation.

I have reproduced the Model class on something that I think is the equivalent:

window_size = 10
input_size = 1
hidden_size = 64
num_layers = 2
num_classes = 28
num_epochs = 20
num_candidates = 9
batch_size = 2048
model_path = 'model/'
log = 'TFAdam_batch_size=' + str(batch_size) + ';epoch=' + str(num_epochs)


modeltf = keras.models.Sequential([
  keras.layers.LSTM(hidden_size, return_sequences=True,
                         batch_input_shape=(batch_size, 10, 1)),
  keras.layers.LSTM(hidden_size, return_sequences=False),
  keras.layers.Dense(num_classes),
])

modeltf.compile(optimizer=keras.optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=['sparse_categorical_accuracy'])

#slight modification on generate function, for keeping np arrays instead of pytorch tensors
def generate(name):
    num_sessions = 0
    inputs = []
    outputs = []
    with open('data/' + name, 'r') as f:
        for line in f.readlines():
            num_sessions += 1
            line = tuple(map(lambda n: n - 1, map(int, line.strip().split())))
            for i in range(len(line) - window_size):
                inputs.append(line[i:i + window_size])
                outputs.append(line[i + window_size])
    print('Number of sessions({}): {}'.format(name, num_sessions))
    print('Number of seqs({}): {}'.format(name, len(inputs)))
    #dataset = [tf.convert_to_tensor(inputs), tf.convert_to_tensor(outputs)]
    dataset = [np.array(inputs), np.array(outputs)]
    return dataset

seq_dataset = generate('hdfs_train')
trainX = seq_dataset[0]
trainY = seq_dataset[1]

#I am letting down the last sessions as tf only accepts a multiple of the batch size as input size
trainX = trainX[:45056]
trainY = trainY[:45056]

trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))

modeltf.fit(trainX, trainY, epochs=num_epochs, batch_size=batch_size)

The problem is that my results are not good, they do not resemble at all what the Pytorch code achieves. This can be seen directly from the loss and the accuracy the model displays when training. It gets stuck at around 3. I have tried batch normalization without much success and it is really bugging me because in pytorch it works perfectly. I think that it might be something subtle but I am not able to find it.

If anyone here has done it or want to take time to take a look I will be very grateful 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
saucer-mancommented, Apr 15, 2020

I took a closer look. During the detection, for each task flow, as long as a sequence is abnormal, the task flow is determined to be abnormal. Therefore, each task stream needs to be detected separately. In the version of @ wuyifan18, the two generate functions are different. Below is my simple d code

import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.callbacks import ModelCheckpoint
import time

def generate(name, window_size):
    num_sessions = 0
    inputs = []
    outputs = []
    with open('data/' + name, 'r') as f:
        for line in f.readlines():
            num_sessions += 1
            line = tuple(map(lambda n: n - 1, map(int, line.strip().split())))
            for i in range(len(line) - window_size):
                inputs.append(line[i:i + window_size])
                outputs.append(line[i + window_size])
    print('Number of sessions({}): {}'.format(name, num_sessions))
    print('Number of seqs({}): {}'.format(name, len(inputs)))
    return inputs, outputs


def lstm_train(x, y, num_epochs, batch_size):
    model = Sequential()
    model.add(LSTM(64, activation='relu', return_sequences=True, input_shape=(x.shape[1], x.shape[2])))
    model.add(LSTM(64, return_sequences=False))  
    model.add(Dense(y.shape[1], activation='softmax'))
    model.compile(loss="categorical_crossentropy", optimizer='adam', metrics=['accuracy'])
    model.fit(x, y, epochs=num_epochs, batch_size=batch_size, shuffle=True) 
    return model


if __name__ == "__main__":
    start_time = time.time()
    num_classes = 28 
    num_epochs = 50  
    batch_size = 2048  
    window_size = 10  

    TP = 0  
    FP = 0  
    n_candidates = 9  # top n probability of the next tag


    X, Y = generate('hdfs_train', window_size)
    X = np.reshape(X, (len(X), window_size, 1)) 
    X = X / float(num_classes)
    Y = np_utils.to_categorical(Y, num_classes)  
    model = lstm_train(X, Y, num_epochs, batch_size)


    def generate1(name, window_size):
        hdfs = set()
        with open('data/' + name, 'r') as f:
            for ln in f.readlines():
                ln = list(map(lambda n: n - 1, map(int, ln.strip().split())))
                ln = ln + [-1] * (window_size + 1 - len(ln))
                hdfs.add(tuple(ln))
        print('Number of sessions({}): {}'.format(name, len(hdfs)))
        return hdfs

    test_normal_loader = generate1('hdfs_test_normal', window_size)
    test_abnormal_loader = generate1('hdfs_test_abnormal', window_size)

    for line in test_abnormal_loader:
        for i in range(len(line) - window_size):
            seq = line[i: i + window_size]
            label = line[i + window_size]
            X = np.reshape(seq, (1, window_size, 1)) 
            X = X / float(num_classes)
            Y = np_utils.to_categorical(label, num_classes)  
            prediction = model.predict(X, verbose=0) 
            if np.argmax(Y) not in prediction.argsort()[0][::-1][: n_candidates]:

                TP += 1
                break

    for line in test_normal_loader:
        for i in range(len(line) - window_size):
            seq = line[i:i + window_size]
            label = line[i + window_size]
            X = np.reshape(seq, (1, window_size, 1)) 
            X = X / float(num_classes)
            Y = np_utils.to_categorical(label, num_classes) 
            prediction = model.predict(X, verbose=0)
            if np.argmax(Y) not in prediction.argsort()[0][::-1][: n_candidates]:
                FP += 1
                break

    elapsed_time = time.time() - start_time
    print('elapsed_time: {:.3f}s'.format(elapsed_time))


    # Compute precision, recall and F1-measure
    FN = len(test_abnormal_loader) - TP
    TN = len(test_normal_loader) - FP
    P = 100 * TP / (TP + FP)
    R = 100 * TP / (TP + FN)
    F1 = 2 * P * R / (P + R)

    print(f"FP:{FP}")
    print(f"FN: {FN}")
    print(f"TP: {TP}")
    print(f"TN: {TN}")

    print('false positive (FP): {}, false negative (FN): {}, Precision: {:.3f}%, Recall: {:.3f}%, F1-measure: {:.3f}%'.format(FP, FN, P, R, F1))
    print('Finished Predicting')

the result is false positive (FP): 908, false negative (FN): 144, Precision: 81.420%, Recall: 96.507%, F1-measure: 88.324%

0reactions
inesanicommented, Apr 14, 2020

Unfortunately I didn’t…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorials | TensorFlow Core
Complete, end-to-end examples to learn how to use TensorFlow for ML beginners and experts. Try tutorials in Google Colab - no setup ...
Read more >
TensorFlow for Beginners With Examples and Python ...
TensorFlow is an end-to-end open-source machine learning platform with ... TensorFlow for Beginners With Examples and Python Implementation.
Read more >
tensorflow/tensorflow: An Open Source Machine Learning ...
TensorFlow was originally developed by researchers and engineers working on the Google Brain team within Google's Machine Intelligence Research organization to ...
Read more >
Deep Learning From Scratch - Theory and Implementation
TensorFlow comes with a high-level API called Keras that allows us to build neural network architectures way easier than by defining the computational...
Read more >
Implementing Neural Networks Using TensorFlow
Implementing Neural Networks Using TensorFlow · Install Tensorflow · Download and Read the Data · Python3 · Data Preprocessing/ Splitting into Train ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found