如何使用经过训练的神经网络(Tensorflow 2.0,回归分析)预测新数据? [英] How to predict new data with a trained neural network (Tensorflow 2.0, regression analysis)?

查看:53
本文介绍了如何使用经过训练的神经网络(Tensorflow 2.0,回归分析)预测新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习和使用Tensorflow的新手.根据Tensorflow网站上的教程,我已经训练了用于回归的神经网络.我有3个输入列和2个输出列,它们已标记为标签".在使用测试数据时,网络似乎可以很好地预测数据,但是当我尝试在测试和训练集之外预测数据时,通过仅导入具有3个输入列的文件,这会给我一个错误,提示"expected density_input具有形状(5,),但数组的形状为(3,)".我了解它是因为模型是在5列数据集上训练的,但是我想从模型中预测未知值(一旦训练),并且不知道输出.当我仅知道输入(三列)时,如何预测结果??如果我也必须知道输出(我肯定不知道),那么回归分析的意义何在?

I am new to machine learning and using Tensorflow. I have trained a neural network for regression following the tutorial on the Tensorflow website. I have 3 input columns and 2 output columns which I have marked as "labels". The network seemingly predicts data fine when using testing data, but when I try to predict data outside the testing and training set, by importing a file with 3 input columns only, it gives me an error saying "expected dense_input to have shape (5,) but got array with shape (3,)". I understand its because the model was trained on a 5 column data set, but I want to predict unknown values from the model (once trained) and don't know the output. How do I predict the results when I only know the input (3 columns)? And if I must know the output as well (which I am sure I mustn't), what's the point of this regression analysis?

我的数据如下:

我正在尝试使神经网络以这种方式执行:

And I am trying to make the neural network perform in this manner:

我想预测我没有数据的输出(例如,RE = 25,BR = 0.5,PR = 0.25),并且我不能使用线性回归,因为输入与输出之间的关系不是线性的.我已经尝试将训练后的输入预测为5列文件,最后两列为垃圾(零),但是模型尝试预测零.据我了解,一旦模型经过训练,权重和偏差应该是固定的,无论最后两列(输出)列中的内容如何,​​模型都应该给我预期的输出.我究竟做错了什么?任何帮助表示赞赏.我已将代码中使用的文件上传到此处 https://drive.google.com/open?id = 1HoMID_razNHcXEGIgvnL8WG3H5WRTl3B .此外,有时我的MSE(均方误差)会收敛,而其他时候则不会.我不确定这可能与将数据随机输入模型有关,尽管我不确定.

I want to predict outputs for which I don't have the data (say, RE = 25, BR=0.5, PR=0.25), and I can't use linear regression because the relation between inputs and outputs is not linear. I have tried predicting the input as a 5 column file after training, with the last two columns being junk (zeros), but the model tries to predict the zeros. As I understand, once the model has been trained, the weights and biases should be fixed and the model should, regardless of what is in the last two (output) columns, give me the expected output. What am I doing wrong? Any help is appreciated. I have uploaded the files used in the code here https://drive.google.com/open?id=1HoMID_razNHcXEGIgvnL8WG3H5WRTl3B . Additionally, sometimes my MSE (mean square error) converges, other times it does not. I suspect it may have something to do with randomly feeding the data to the model, although I am not sure.

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)

推荐答案

因此,重申一下您想要的内容,您有一个总共包含5个要素的数据集.并且您需要将前三个功能用作输入,而将后两个功能用作目标.为此,需要进行更改.

So to reiterate what you want, you have a dataset with 5 features in total. And you need to use the first three features as inputs and the last two as targets. Here's what needs to changed to achieve that.

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自动添加.

Model building

Your model should have only three features. Therefore, the input_shape should be 3 (not 5). Batch shape will be automatically added by 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='')

训练模型

训练模型时,您仅将前三个特征作为输入,而将后两个特征作为标签/目标.现在,您应该了解如何帮助您解决问题.现在,您只需使用已知的三个功能就可以安全地预测两个未知变量.

Training the model

When training the model you only give the first three features as inputs and the last two features as labels/targets. Now you should see how that helps to solve your problem. Now you can safely predict the two unknown variables by only using the known three features.

因此,当训练作为输入时,我们仅给出前三列,目标将是后两列.

So when training as input we only give the first three columns and targets will be the last two.

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

测试时间

测试和中提琴也遵循相同的规则!您在测试时并未使用未知的两个功能进行预测(即,我们仅使用前三个功能).

Testing time

Same rule goes for the testing and viola! you are not using the unknown two features at the testing time to predict (i.e. we're only using the first three features).

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)

这篇关于如何使用经过训练的神经网络(Tensorflow 2.0,回归分析)预测新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆