The context is determining the health of components of a motorbike by reading values like acceleration, cooling water temperature, rpm, fuel consumption, braking force, ...
Input Data:
water_temperature rpm federeindrückung in mm Spritverbrauch/100km Bremsverzögerung [m/s²] ... Lichtanlage Reifen Rahmen Bordelektronik Auspuffanlage
0 99.995 6000 150 5.0 2 ... 99.995 99.995 99.995 99.995 99.995
1 80.000 11500 150 5.0 2 ... 99.995 99.995 99.995 99.995 99.995
2 80.000 6000 0 5.0 2 ... 99.995 99.000 99.000 99.995 99.000
3 80.000 6000 150 10.0 2 ... 99.995 99.995 99.995 99.995 99.995
4 80.000 8000 150 6.0 2 ... 99.995 99.995 99.995 99.995 99.995
5 75.000 8000 150 4.9 2 ... 99.995 99.995 99.995 99.995 99.995
6 75.000 8000 150 4.9 8 ... 99.995 99.000 99.000 99.995 99.995
7 80.000 8000 150 5.5 2 ... 99.995 99.995 99.995 99.995 99.995
8 83.000 8000 150 5.0 2 ... 99.995 99.995 99.995 99.995 99.995
9 79.000 8000 150 5.0 2 ... 99.000 99.995 99.000 98.000 99.000
10 80.000 11000 150 5.0 2 ... 99.995 99.995 99.995 99.995 99.995
The prediction of the neural network should be the remaining health of each part of the motorcycle after 1 minute, for example, if RPM is 11500 for a minute, the engine loses 1% of health.
Desired Output Data:
Motor Federung Getriebe Bremsen Lichtanlage Reifen Rahmen Bordelektronik Auspuffanlage
0 99.000 99.995 99.000 99.995 99.995 99.995 99.995 99.995 99.995
1 98.000 99.995 99.000 99.995 99.995 99.995 99.995 99.995 99.995
2 99.995 97.000 99.995 99.995 99.995 99.000 99.000 99.995 99.000
3 98.000 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
4 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
5 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
6 99.995 99.995 99.995 99.000 99.995 99.000 99.000 99.995 99.995
7 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
8 98.000 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
9 99.995 99.995 99.995 99.000 99.000 99.995 99.000 98.000 99.000
10 98.000 99.995 99.995 99.995 99.995 99.995 99.995 99.995 99.995
The goal is to convert this whole process into a neural network.
So far I have this code:
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
names = open("./training_data_header.txt", "r").read().split(",")
motorbike_train = pd.read_csv("training_data.csv", names=names)
motorbike_features = motorbike_train.copy()
outputs = pd.DataFrame()
for i in range(7, len(names)):
outputs = pd.concat([outputs, motorbike_train.pop(names[i])], axis=1)
motorbike_model = tf.keras.Sequential(
[layers.Dense(16), layers.Dense(9, activation="sigmoid")]
)
motorbike_model.compile(
loss=tf.keras.losses.MeanSquaredError(), optimizer=tf.optimizers.Adam()
)
motorbike_model.fit(motorbike_features, outputs, epochs=10)
# print(motorbike_features)
# print(outputs)
print(motorbike_model.predict(motorbike_features.loc[0]))
This leads to the following error:
Traceback (most recent call last):
File "/Users/tobiaswimmer/Documents/Development/atos-ktm-hackathon/ai/create-model.py", line 26, in <module>
print(motorbike_model.predict(motorbike_features.loc[0]))
File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/var/folders/1r/2f_4g23d3zx5lb8m2_s28qt00000gn/T/__autograph_generated_filekm9z4tos.py", line 15, in tf__predict_function
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:
File "/usr/local/lib/python3.9/site-packages/keras/engine/training.py", line 1845, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.9/site-packages/keras/engine/training.py", line 1834, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.9/site-packages/keras/engine/training.py", line 1823, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.9/site-packages/keras/engine/training.py", line 1791, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.9/site-packages/keras/engine/input_spec.py", line 248, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 16, but received input with shape (None, 1)
Call arguments received by layer "sequential" (type Sequential):
• inputs=tf.Tensor(shape=(None, 1), dtype=float32)
• training=False
• mask=None
CodePudding user response:
You need to set up the first dimension of the tensor to 1, to get an input shape of (1,16):
import numpy as np
print(motorbike_model.predict(np.expand_dims(motorbike_features.loc[0], axis=0)))
You can't use sigmoid as output of the neural net, otherwise each output value will be between 0 and 1.
Edit: try the following
motorbike_model = tf.keras.Sequential(
[layers.Conv2D(32,1,padding='same',activation='relu',input_shape=(1,16,1)),
layers.Conv2D(64,1,padding='same',activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(9)])
motorbike_model.summary()
motorbike_model.compile(
loss=tf.keras.losses.MeanSquaredError(),
optimizer=tf.optimizers.Adam()
)
motorbike_model.fit((np.expand_dims(motorbike_features, axis= (0,3))), outputs, epochs=150)
print(motorbike_model.predict(np.expand_dims(motorbike_features.loc[0], axis=(0,1,3))))