python - 如何从具有 tensorflow neural network 的一组输入 values 中预测一组输出 values?

上下文是通过读取 values 来确定摩托车部件的健康状况,例如加速度、冷却水温度、转速、油耗、制动力……

输入数据:

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

neural network的预测应该是1分钟后摩托车各部分的剩余健康度,例如11500转一分钟,发动机损失1%的健康度。

所需的输出数据:

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

目标是将整个过程转换为neural network。

到目前为止,我有这个代码:

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]))

这会导致以下错误:

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

回答1

您需要将张量的第一个维度设置为 1,以获得 (1,16) 的输入形状:

import numpy as np

print(motorbike_model.predict(np.expand_dims(motorbike_features.loc[0], axis=0)))

不能使用 sigmoid 作为神经网络的输出,否则每个输出 value 将介于 0 和 1 之间。

编辑:尝试以下

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))))

相似文章

最新文章