神经网络回归案例(python

简介: 神经网络回归案例(python


1.代码:

import numpy as np
import tensorflow as tf
# 自变量和目标值
X = np.array([[1, 2, 3, 4, 5, 6]])  # 自变量需要是二维数组形式
Y = np.array([4531.575371])
# 转换为TensorFlow张量
X = tf.constant(X, dtype=tf.float32)
Y = tf.constant(Y, dtype=tf.float32)
# 定义模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[6], use_bias=False)  # 不使用偏置项
])
# 设置初始权重
initial_weights = np.array([[700], [800], [600], [650], [750], [850]], dtype=np.float32)
model.layers[0].set_weights([initial_weights])
# 编译模型
model.compile(optimizer='adam', loss='mse')
# 训练模型
history = model.fit(X, Y, epochs=10000, verbose=0)  # 使用较多的迭代次数以尝试获得更好的结果
# 获取训练后的权重
trained_weights = model.layers[0].get_weights()[0]
# 打印结果
print("训练后的权重:")
print(trained_weights)

2.效果:

目录
相关文章
|
5天前
|
机器学习/深度学习 数据采集 数据可视化
跟着penguins案例学Seaborn之Pairplot
跟着penguins案例学Seaborn之Pairplot
13 1
|
5天前
|
Linux
跟着mpg案例学Seaborn之Jointplot
跟着mpg案例学Seaborn之Jointplot
16 1
|
5天前
|
数据可视化 Linux
跟着mpg案例学Seaborn之KDE
跟着mpg案例学Seaborn之KDE
12 1
|
5天前
|
数据挖掘
跟着mpg案例学Seaborn之Heatmap
跟着mpg案例学Seaborn之Heatmap
10 1
|
5天前
|
数据可视化
跟着exercise案例学Seaborn之FacetGrid
跟着exercise案例学Seaborn之FacetGrid
10 0
|
5天前
|
数据可视化 数据挖掘
跟着mpg案例学Seaborn之Scatter
跟着mpg案例学Seaborn之Scatter
14 0
|
5天前
跟着Tips案例学Seaborn之Catplot
跟着Tips案例学Seaborn之Catplot
|
5天前
|
算法 Linux
跟着Iris案例学Seaborn之Histplot
跟着Iris案例学Seaborn之Histplot
10 0
|
5天前
|
数据可视化 Python
跟着Titanic案例学Seaborn之Countplot
跟着Titanic案例学Seaborn之Countplot
15 0
|
5天前
|
数据采集 数据挖掘 Linux
跟着Titanic案例学Seaborn之Barplot
跟着Titanic案例学Seaborn之Barplot
13 0
下一篇
无影云桌面