TensorFlow(Python | Keras)人工神经网络(ANN)回归模型-找出材料获得超导特性的温度和分类模型-区分结合剂/非结合剂分子属性

8 篇文章 0 订阅
3 篇文章 0 订阅

回归任务旨在从输入训练数据中预测连续变量,而分类任务旨在将输入数据分为两个或多个类别。 例如,预测某一天是否会下雨的模型是一项分类任务,因为模型的结果将分为两类——下雨或不下雨。 然而,预测给定日期的降雨量的模型将是回归任务的一个示例,因为模型的输出将是一个连续变量——降雨量。

顺序模型

Python代码示例

顺序模型适用于简单的层堆栈,其中每一层都有一个输入张量和一个输出张量。

例如,以下顺序模型:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Define Sequential model with 3 layers
model = keras.Sequential(
    [
        layers.Dense(2, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(4, name="layer3"),
    ]
)
# Call model on a test input
x = tf.ones((3, 3))
y = model(x)

相当于这个函数:

# Create 3 layers
layer1 = layers.Dense(2, activation="relu", name="layer1")
layer2 = layers.Dense(3, activation="relu", name="layer2")
layer3 = layers.Dense(4, name="layer3")

# Call layers on a test input
x = tf.ones((3, 3))
y = layer3(layer2(layer1(x)))

顺序模型用于构建回归和分类模型。 在顺序模型中,信息通过网络从开始的输入层传播到最后的输出层。 层按顺序堆叠在模型中,每一层都有一个输入和一个输出。

使用 TensorFlow 创建 ANN

目标预测示例:

数据准备

#importing the libraries
import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.datasets import load_boston
dataset = load_boston()

target = pd.Series(dataset['target'],name='price')
data = pd.concat([data,target],axis=1)
data.head()

data.drop(['TAX','DIS'],axis=1,inplace=True)

训练测试拆分


from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(data.drop(['price'],axis=1),data['price'],test_size=0.15)

x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = np.array(x_test)
y_test = np.array(y_test)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

Keras 层


i = tf.keras.layers.Input(shape=(x_train.shape[1]))
fc1 = tf.keras.layers.Dense(10,activation=tf.keras.activations.relu)(i)
fc2 = tf.keras.layers.Dense(12,activation=tf.keras.activations.relu)(fc1)
fc3 = tf.keras.layers.Dense(20,activation=tf.keras.activations.relu)(fc2)
out = tf.keras.layers.Dense(1)(fc3)

model = tf.keras.models.Model(i,out)

训练神经网络


model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss=tf.keras.losses.mse)
#fitting the model to the data train = model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=100,batch_size=128)

模型结果显示

plt.figure(figsize=(10,8))
plt.plot(train.history['loss'],label="Training set loss")
plt.plot(train.history['val_loss'],label="Validation set loss")
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
#predictions vs actual
plt.figure(figsize=(10,8))
plt.plot(y_test,label="original targets")
plt.plot(y_pred,label="predicted targets")
plt.legend()
plt.xlabel('examples')
plt.ylabel('predictions')

本文模型拟合

  • 使用 TensorFlow 创建线性回归模型ANN
  • 使用 TensorFlow 创建多层 ANN

案例:人工神经网络(ANN)找出材料获得超导特性的温度

分类模型

  • 使用 TensorFlow 创建逻辑回归模型ANN

案例:人工神经网络(ANN)区分结合剂/非结合剂分子属性

详情参阅 - 亚图跨际
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值