TensorFlow Implementation
See original GitHub issueFirst 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:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
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
the result is false positive (FP): 908, false negative (FN): 144, Precision: 81.420%, Recall: 96.507%, F1-measure: 88.324%
Unfortunately I didn’t…