神经网络建模预测

1 我们用tensorflow去构建神经网络

import tensorflow as tf
tf.enable_eager_execution()
# 导入库
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline

2 数据

data = pd.read_csv(r'/home//data.csv')

3 神经网络建模

# 数据划分
x = data.iloc[:, 3:]
y = data.iloc[:, 2]
# 搭建神经网络
model = tf.keras.Sequential([tf.keras.layers.Dense(600, input_shape=(12,), activation='tanh'), # tanh
                             tf.keras.layers.Dense(300, activation='relu'),
                             tf.keras.layers.Dense(150, activation='relu'),
                             tf.keras.layers.Dense(50, activation='relu'),
                             tf.keras.layers.Dense(25, activation='relu'),
                             tf.keras.layers.Dense(1)])
print(model.summary())
# 设置优化模型、损失函数
model.compile(optimizer='adam', loss='mse',metrics=['mape'])   
# 模型训练,迭代次数为150
history = model.fit(x, y, epochs=150,batch_size=10,validation_split=0.1) # ,varbose=0

模型训练层数情况:
Colocations handled automatically by placer.


Layer (type) Output Shape Param #

dense (Dense) (None, 600) 7800


dense_1 (Dense) (None, 300) 180300


dense_2 (Dense) (None, 150) 45150


dense_3 (Dense) (None, 50) 7550


dense_4 (Dense) (None, 25) 1275


dense_5 (Dense) (None, 1) 26

Total params: 242,101
Trainable params: 242,101
Non-trainable params: 0

网络迭代以及指标:
Epoch 1/150
18/18 [] - 1s 57ms/sample - loss: 6848.0991 - mean_absolute_percentage_error: 97.4036 - val_loss: 8904.9209 - val_mean_absolute_percentage_error: 94.7421
Epoch 2/150
18/18 [
] - 0s 445us/sample - loss: 6045.1309 - mean_absolute_percentage_error: 87.6766 - val_loss: 7586.9141 - val_mean_absolute_percentage_error: 87.4439
Epoch 3/150
18/18 [] - 0s 403us/sample - loss: 4934.8857 - mean_absolute_percentage_error: 76.1649 - val_loss: 5794.1787 - val_mean_absolute_percentage_error: 76.4067



Epoch 147/150
18/18 [
] - 0s 4ms/sample - loss: 60.0847 - mean_absolute_percentage_error: 7.7134 - val_loss: 3.8309 - val_mean_absolute_percentage_error: 1.9660
Epoch 148/150
18/18 [] - 0s 395us/sample - loss: 59.0289 - mean_absolute_percentage_error: 7.8850 - val_loss: 4.7361 - val_mean_absolute_percentage_error: 1.9454
Epoch 149/150
18/18 [
] - 0s 416us/sample - loss: 60.0656 - mean_absolute_percentage_error: 7.9295 - val_loss: 5.1167 - val_mean_absolute_percentage_error: 1.9418
Epoch 150/150
18/18 [==============================] - 0s 413us/sample - loss: 58.7168 - mean_absolute_percentage_error: 8.0490 - val_loss: 4.7343 - val_mean_absolute_percentage_error: 1.9454

4 查看过拟情况

def plot_history(history):
    hist = pd.DataFrame(history.history)
    hist['epoch'] = history.epoch
    plt.figure(figsize=(25,8))
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.plot(hist['epoch'], hist['loss'],
             label='Train Error')
    plt.plot(hist['epoch'], hist['val_loss'],
             label = 'Val Error')
    plt.legend()
    plt.show()
plot_history(history)

神经网络建模预测_第1张图片
注: 训练集和测试集指标查看,不存在过拟合和欠拟合问题,所以这个模型是可以用来预测的。

5 预测数据

prediction_data = model.predict(data_.iloc[:,1:])

你可能感兴趣的:(神经网络,人工智能,深度学习)