如何使用已训练好的神经网络(Tensorflow 2.0,回归分析)预测新数据?

4
我是机器学习和Tensorflow的新手。我按照Tensorflow网站上的教程训练了一个用于回归的神经网络。我有3个输入列和2个输出列,我将其标记为“标签”。当使用测试数据时,该网络似乎可以很好地预测数据,但是当我尝试预测测试集和训练集之外的数据时,通过导入仅具有3个输入列的文件,它会给出错误,显示“期望dense_input的形状为(5,),但得到形状为(3,)的数组”。我知道这是因为模型是在一个由5列数据组成的数据集上进行训练的,但我想要从模型中预测未知值(一旦训练完成),并且不知道输出结果。当我只知道输入(3列)时,如何预测结果?如果我必须知道输出结果(我确定我不必知道),那么进行这种回归分析的意义是什么?
我的数据看起来像这样:enter image description here

我正在尝试让神经网络以这种方式运行:输入图像描述

我想预测没有数据的输出(例如,RE = 25,BR = 0.5,PR = 0.25),但我不能使用线性回归,因为输入和输出之间的关系不是线性的。我尝试过在训练后将输入预测为一个5列文件,其中最后两列是垃圾(零),但模型会尝试预测这些零值。据我所知,一旦模型被训练,权重和偏差应该固定,无论最后两列(输出)中的内容是什么,模型都应该给我期望的输出。我做错了什么?任何帮助都将不胜感激。我已经上传了代码中使用的文件https://drive.google.com/open?id=1HoMID_razNHcXEGIgvnL8WG3H5WRTl3B。此外,有时我的MSE(均方误差)会收敛,而有时则不会。我怀疑这可能与随机向模型提供数据有关,但我不确定。
import tensorflow as tf
from tensorflow import keras
import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle

column_names = ['Reynolds Number', 'Blockage Ratio', 'Prandtl Number', 'Nusselt Number', 'Drag Coefficient']        
dataset = pd.read_csv('WW.csv', names=column_names, skipinitialspace=True)      
train_dataset = dataset.sample(frac=0.9,random_state=0)
test_dataset = dataset.drop(train_dataset.index)    
train_labels = train_dataset.iloc[:, 3:].values
test_labels = test_dataset.iloc[:, 3:].values   

print(train_dataset)
print(test_dataset)                         

def build_model():
  model = keras.Sequential([
    keras.layers.Dense(3, activation='relu', input_shape=[len(train_dataset.keys())]),
    keras.layers.Dense(4, activation='relu'),
    keras.layers.Dense(2)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

  model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
  return model

model = build_model()
model.summary()

class PrintDot(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs):
    if epoch % 100 == 0: print('')
    print('.', end='')

EPOCHS = 5000

early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=500)
history = model.fit(train_dataset, train_labels, epochs=EPOCHS, validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])
model.save("model.h5")

hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
print('\n', hist.tail())

def plot_history(history):
  hist = pd.DataFrame(history.history)
  hist['epoch'] = history.epoch

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Abs Error [MPG]')
  plt.plot(hist['epoch'], hist['mae'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mae'],
           label = 'Val Error')
  plt.ylim([0,5])
  plt.legend()

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Square Error [$MPG^2$]')
  plt.plot(hist['epoch'], hist['mse'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mse'],
           label = 'Val Error')
  plt.ylim([0,20])
  plt.legend()
  plt.show()


plot_history(history)

test_predictions = model.predict(test_dataset)
test_dataset['Predicted Nu'], test_dataset['Predicted CD'] = test_predictions[:,0], test_predictions[:,1]
print(test_dataset)

np.savetxt('test_dataset.txt', test_dataset, delimiter=',')

predict = model.predict(train_dataset)
train_dataset['Predicted Nu'], train_dataset['Predicted CD'] = predict[:,0], predict[:,1]
print(train_dataset)

np.savetxt('train_dataset.txt', train_dataset, delimiter=',')

class_names = ['Reynolds Number', 'Blockage Ratio', 'Prandtl Number', 'junk Nusselt Number', 'junk Drag Coefficient']    
all_inputs = pd.read_csv('Predict_Input.csv', names=class_names, skipinitialspace=True)
all_outputs = model.predict(all_inputs)
all_inputs['Predicted Nu'], all_inputs['Predicted CD'] = all_outputs[:,0], all_outputs[:,1]
print(all_inputs)

你能解释一下你在这里想要实现什么吗?从我所看到的,train_inputs是一个(batch_size, 5)的输入,而你正在尝试预测一个(batch_size, 2)的输出,这已经包含在训练数据中了?如果我错了,请纠正我,因为这样做没有太多意义,你正在预测你已经知道的东西。也许你可以用一个例子或图表来解释一下你需要什么? - thushv89
我已经包含了一张图表和我的一些数据。所有的数据都可以在指定的链接中找到。我的网络代码是否按照我的期望进行操作?也就是说,将前三列视为输入,后两列视为输出(指定为“train_labels”)?@thushv89 - H.F.A.
@HassanAhmed,不幸的是,它并没有做你认为它在做的事情。正如我在第一条评论中所说的那样,你正在使用所有五个特征来预测两个已经包含在输入中的特征。这不是正确的做法。我很快会发布一个可能的解决方案。 - thushv89
1个回答

3

请再次确认您的需求:您有一个包含5个特征的数据集。您需要使用前三个特征作为输入,后两个作为目标。下面是需要更改的内容:

import tensorflow as tf
from tensorflow import keras
import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle

column_names = ['Reynolds Number', 'Blockage Ratio', 'Prandtl Number', 'Nusselt Number', 'Drag Coefficient']        
dataset = pd.read_csv('WW.csv', names=column_names, skipinitialspace=True)      
train_dataset = dataset.sample(frac=0.9,random_state=0)
test_dataset = dataset.drop(train_dataset.index)    
train_labels = train_dataset.iloc[:, 3:].values
test_labels = test_dataset.iloc[:, 3:].values   

print(train_dataset)
print(test_dataset)                         

模型构建

您的模型应该只有三个特征。因此,input_shape 应该是3(而不是5)。批量大小将由Keras自动添加。

# We are setting the input size as (None, 3)
def build_model():
  model = keras.Sequential([
    keras.layers.Dense(3, activation='relu', input_shape=(3,)),
    keras.layers.Dense(4, activation='relu'),
    keras.layers.Dense(2)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

  model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
  return model

model = build_model()
model.summary()

class PrintDot(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs):
    if epoch % 100 == 0: print('')
    print('.', end='')

训练模型

在训练模型时,您只需将前三个特征作为输入,将最后两个特征作为标签/目标即可。现在您应该看到这有助于解决您的问题。现在,您可以仅使用已知的三个特征安全地预测两个未知变量。

因此,在训练时,我们只提供前三列作为输入,而目标将是最后两个。

EPOCHS = 5000

early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=500)

# Note that the input only takes the first three columns
history = model.fit(train_dataset.iloc[:,:3], train_labels, epochs=EPOCHS, validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])
model.save("model.h5")

测试时间

测试时同样适用于这个规则!这样,您就不会在测试时使用未知的两个特征进行预测(即仅使用前三个特征)。

test_predictions = model.predict(test_dataset.iloc[:,:3])
print(test_dataset)
test_dataset['Predicted Nu'], test_dataset['Predicted CD'] = test_predictions[:,0], test_predictions[:,1]
print("\nPredicted\n")
print(test_dataset)

是的,那个可以。非常感谢。我有一个后续问题。当输入层只有3个神经元时,我的网络如何能够接受具有5个参数的输入?它们的数量不是必须相同吗? - H.F.A.
实际上,您的网络有5个输入。您设置了input_shape=[len(train_dataset.keys())],它是5。 - thushv89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接