I am now woriking on a neural network that should predict the next activity and the outcome of a trace (sequence of events, taken from an eventlog). First, I extended every trace with an additional event which indicates, as its activity, the outcome of the trace (e.g.trace t has, as its label, o1, which then becomes the activity of the final just added event of the trace) In particular, I first encoded each unique activity as an integer (each trace is then an array of integers). Each trace is then extended with an additional event which indicates, as its activity, the outcome (e.g.trace t has, as its label, outcome1, which then becomes Each trace is then divided in windows of fixed dimension, which will be the input of the neural network. For example, for a trace [a b b c ... x x y z o] (where o is the outcome) encoded as [1 2 2 3 ... 24 24 25 26 27] (where 27 is the encoding for the outcome) and with the fix dimension of windows equal to 4, the resulting windows will be [0 0 0 a] [0 0 a b] [0 a b b] [a b b c] ... [x x y z] encoded as [0 0 0 1] [0 0 1 2] [0 1 2 2] [1 2 2 3] ... [24 24 25 26]: as you can see, outcomes are not included in the windows, since they have to be predicted.
The target data will then be:
- for the next activity prediction, an array containig the next activity of each window (following the example above, it would be [b b c ... o] encoded as [2 2 3 ... 27]
- for the outcome prediction, an array containing the outcome of each window (in our case it would be an array of o or, encoded, of 27: however, since multiple traces will be considered, multiple windows with different outcomes will be produced.)
Here I show some examples of these data: MAP OF THE ACTIVITIES (the first two activities are the outcomes)
{'regular': 1, 'deviant': 2, 'Round Grinding - Machine 3': 3, 'Round Grinding - Machine 2': 4, 'Grinding Rework - Machine 27': 5, 'Lapping - Machine 1': 6, 'other': 7, 'Turning & Milling Q.C.': 8, 'Laser Marking - Machine 7': 9, 'Round Grinding - Q.C.': 10, 'Turning & Milling - Machine 4': 11, 'Final Inspection Q.C.': 12, 'Packing': 13, 'Turning & Milling - Machine 8': 14, 'Flat Grinding - Machine 11': 15, 'Round Grinding - Manual': 16, 'Wire Cut - Machine 13': 17, 'Turning & Milling - Machine 9': 18, 'Milling - Machine 16': 19, 'Turning - Machine 8': 20, 'Turning Q.C.': 21, 'Turning & Milling - Machine 5': 22, 'Turning & Milling - Machine 10': 23, 'Turning & Milling - Machine 6': 24, 'Round Grinding - Machine 12': 25, 'Turning - Machine 9': 26, 'Milling - Machine 14': 27, 'Turn & Mill. & Screw Assem - Machine 10': 28}
INPUT DATA (variable x_training) (encoded windows of traces) (here there are just 3 traces)
[array([0., 0., 0., 3.]), array([0., 0., 3., 4.]), array([0., 3., 4., 5.]), array([3., 4., 5., 5.]), array([4., 5., 5., 6.]), array([5., 5., 6., 6.]), array([5., 6., 6., 5.]), array([6., 6., 5., 7.]), array([6., 5., 7., 7.]), array([5., 7., 7., 7.]), array([7., 7., 7., 7.]), array([7., 7., 7., 7.]), array([7., 7., 7., 5.]), array([7., 7., 5., 5.]), array([7., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 5.]), array([5., 5., 5., 8.]), array([5., 5., 8., 6.]), array([5., 8., 6., 9.]), array([8., 6., 9., 3.]), array([ 6., 9., 3., 10.]), array([ 0., 0., 0., 11.]), array([ 0., 0., 11., 11.]), array([ 0., 11., 11., 11.]), array([11., 11., 11., 11.]), array([11., 11., 11., 11.]), array([11., 11., 11., 11.]), array([11., 11., 11., 8.]), array([11., 11., 8., 11.]), array([11., 8., 11., 11.]), array([ 8., 11., 11., 8.]), array([11., 11., 8., 11.]), array([11., 8., 11., 11.]), array([ 8., 11., 11., 11.]), array([11., 11., 11., 8.]), array([11., 11., 8., 11.]), array([0., 0., 0., 6.]), array([0., 0., 6., 3.]), array([0., 6., 3., 3.]), array([6., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 6.]), array([3., 3., 6., 3.]), array([3., 6., 3., 3.]), array([6., 3., 3., 3.]), array([3., 3., 3., 3.]), array([3., 3., 3., 3.]), array([ 3., 3., 3., 12.]), array([ 3., 3., 12., 12.]), array([ 3., 12., 12., 12.]), array([12., 12., 12., 12.]), array([12., 12., 12., 13.]), array([12., 12., 13., 12.]), array([12., 13., 12., 12.]), array([13., 12., 12., 3.]), array([12., 12., 3., 12.]), array([12., 3., 12., 12.]), array([ 3., 12., 12., 12.]), array([12., 12., 12., 12.]), ... (and so on) ]
TARGET DATA (NEXT ACTIVITIES) (variable y_training) (consider each integer here as its +1, since i used the label encoder fit_transform).
[ 3 4 4 5 5 4 6 6 6 6 6 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 7 5 8 2 9 0 10 10 10 10 10 7 10 10 7 10 10 10 7 10
1 2 2 2 2 2 2 2 2 2 5 2 2 2 2 2 11 11 11 11 12 11 11 2
11 11 11 11 1 ... (and so on)]
TARGET DATA (OUTCOMES) (variable z_training) (for this given dataset the outcome is binary, but it's not always the case) (here as well, consider each outcome as its +1, since i used the label encoder fit_transform).
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 ... (and so on)]
Also consider that I then make both y_training and z_training to categorical.
Here you can find the neural network I built:
self.x_training = np.asarray(self.x_training)
outsize_act = len(np.unique(self.y_training))
outsize_out= len(np.unique(self.z_training))
self.y_training = to_categorical(self.y_training)
self.z_training = to_categorical(self.z_training)
unique_events = len(self.act_dictionary)
X_train, X_val, Y_train, Y_val, Z_train, Z_val = train_test_split(self.x_training, self.y_training, self.z_training, test_size=0.2,random_state=42, shuffle=True)
size_act = (unique_events + 1) // 2
input_act = Input(shape=(self.example_size,), dtype='int32', name='input_act')
x_act = Embedding(output_dim=size_act, input_dim=unique_events + 1, input_length=self.example_size)(input_act)
l1 = LSTM(16, return_sequences=True, kernel_initializer='glorot_uniform')(x_act)
b1 = BatchNormalization()(l1)
l2_1 = LSTM(16, return_sequences=False, kernel_initializer='glorot_uniform')(b1) # the layer specialized in activity prediction
b2_1 = BatchNormalization()(l2_1)
l2_2 = LSTM(16, return_sequences=False, kernel_initializer='glorot_uniform')(b1) #the layer specialized in outcome prediction
b2_2 = BatchNormalization()(l2_2)
output_l = Dense(outsize_act, activation='softmax', name='act_output')(b2_1)
output_o = Dense(outsize_out, activation='softmax', name='outcome_output')(b2_2)
model = Model(inputs=input_act, outputs=[output_l, output_o])
print(model.summary())
opt = Adam()
model.compile(loss={'act_output':'categorical_crossentropy', 'outcome_output':'categorical_crossentropy'}, optimizer=opt, metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss', patience=42)
model_checkpoint = ModelCheckpoint('output_files/models/model_{epoch:02d}-{val_loss:.2f}.h5', monitor='val_loss', verbose=0, save_best_only=True,save_weights_only=False, mode='auto')
lr_reducer = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
model.fit(X_train, {'act_output':Y_train, 'outcome_output':Z_train}, epochs=200, batch_size=128, verbose=2, callbacks=[early_stopping, model_checkpoint, lr_reducer], validation_data=(X_val, Y_val,Z_val))
model.save("model/generate_" + self.log_name + ".h5")
Here you can find the errors I get:
Epoch 1/200
Traceback (most recent call last):
File "C:\Users\...\manager.py", line 244, in build_neural_network_model
model.fit(X_train, {'act_output':Y_train, 'outcome_output':Z_train}, epochs=200, batch_size=128, verbose=2, callbacks=[early_stopping, model_checkpoint, lr_reducer],
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\func_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1525, in test_function *
return step_function(self, iterator)
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1514, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1507, in run_step **
outputs = model.test_step(data)
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1473, in test_step
self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 918, in compute_loss
return self.compiled_loss(
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\engine\compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\losses.py", line 142, in __call__
return losses_utils.compute_weighted_loss(
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\utils\losses_utils.py", line 321, in compute_weighted_loss
losses, _, sample_weight = squeeze_or_expand_dimensions( # pylint: disable=unbalanced-tuple-unpacking
File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\keras\utils\losses_utils.py", line 211, in squeeze_or_expand_dimensions
sample_weight = tf.squeeze(sample_weight, [-1])
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 2 for '{{node categorical_crossentropy/weighted_loss/Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](IteratorGetNext:2)' with input shapes: [?,2].
I am thereby asking for your help: I already googled and found many similar threads, but none of the solutions works. I guess this is because I am working on a double-output neural network. It's the first time I work on neural networks, maybe there's some evident mistake which I don't get. Thanks for your help.