为什么会无法输出呢?哪里出错了呀?
SystemModel文件下的__init__.py文件
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from Environment import alpha,num_iters
class System_Model():
def __init__(
self,
):
#future是一个矩阵,每一行代表一个样本的特征向量
# self.future.iloc[0, :] = [1,1,1,1] #X的第一列都是1,用于表示截距项
# self.future.iloc[1, :] = [3,4,2,3] #市场需求(供应商数目多少,少-多):分为5档
# self.future.iloc[2, :] = [3,5,2,4] #平均服务质量(坏-好):分为5档
future = np.array([[1, 3, 5], [1, 4, 6], [1, 2, 3], [1, 5, 7]])
goal = np.array([8, 10, 6, 12])
self.alpha = alpha
self.num_iters = num_iters
# 定义梯度下降函数
def Get_S2A_bandwithPrice(future, goal, alpha, num_iters):
m = len(goal) # 样本数量
n = future.shape[1] # 特征数量
theta = np.zeros(n) # 初始化参数向量
J_history = [] # 保存每次迭代的损失函数值
for i in range(num_iters):
# 计算预测值
h = np.dot(future, theta)
# 梯度下降更新参数
theta = theta - alpha * (1 / m) * np.dot(future.T, h - goal)
# 计算损失函数值(均方误差)
J = np.sum((h - goal) ** 2) / (2 * m)
J_history.append(J)
# 绘制损失函数值的变化
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 将迭代次数转换为数组
iterations = np.arange(1, num_iters + 1)
# 将数据转换成网格数据
theta0_vals, theta1_vals = np.meshgrid(np.linspace(-10, 10, 100), np.linspace(-10, 10, 100))
J_vals = np.zeros((len(theta0_vals), len(theta1_vals)))
# 计算不同参数组合下的损失函数值
for i in range(len(theta0_vals)):
for j in range(len(theta1_vals)):
t = [theta0_vals[i, j], theta1_vals[i, j]]
J_vals[i, j] = np.sum((np.dot(future, t) - goal) ** 2) / (2 * m)
return theta, J_history
RunModel文件下的__init__.py文件
from SystemModel import System_Model
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def main():
#调用梯度下降函数进行训练
env = System_Model()
theta, J_history = env.Get_S2A_bandwithPrice(env.future, env.goal, env.alpha, env.num_iters)
print("最优参数:", theta, J_history)
# 绘制损失函数曲面
env.ax.plot_surface(env.theta0_vals, env.theta1_vals, env.J_vals, cmap='viridis')
env.ax.set_xlabel('Theta 0')
env.ax.set_ylabel('Theta 1')
env.ax.set_zlabel('Cost')
# 绘制梯度下降过程中的参数更新点
env.ax.scatter(env.theta[0], env.theta[1], env.J_history[-1], c='r', marker='o')
plt.show()
Environment_init_.py
# 设置学习率和迭代次数
alpha = 0.01
num_iters = 1000
可能是缩进问题导致的错误,代码修改为
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from Environment import alpha, num_iters
class System_Model():
def __init__(self):
# future是一个矩阵,每一行代表一个样本的特征向量
# self.future.iloc[0, :] = [1,1,1,1] #X的第一列都是1,用于表示截距项
# self.future.iloc[1, :] = [3,4,2,3] #市场需求(供应商数目多少,少-多):分为5档
# self.future.iloc[2, :] = [3,5,2,4] #平均服务质量(坏-好):分为5档
future = np.array([[1, 3, 5], [1, 4, 6], [1, 2, 3], [1, 5, 7]])
goal = np.array([8, 10, 6, 12])
self.alpha = alpha
self.num_iters = num_iters
# 定义梯度下降函数
def Get_S2A_bandwithPrice(future, goal, alpha, num_iters):
m = len(goal) # 样本数量
n = future.shape[1] # 特征数量
theta = np.zeros(n) # 初始化参数向量
J_history = [] # 保存每次迭代的损失函数值
for i in range(num_iters):
# 计算预测值
h = np.dot(future, theta)
# 梯度下降更新参数
theta = theta - alpha * (1 / m) * np.dot(future.T, h - goal)
# 计算损失函数值(均方误差)
J = np.sum((h - goal) ** 2) / (2 * m)
J_history.append(J)
# 绘制损失函数值的变化
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 将迭代次数转换为数组
iterations = np.arange(1, num_iters + 1)
# 将数据转换成网格数据
theta0_vals, theta1_vals = np.meshgrid(np.linspace(-10, 10, 100), np.linspace(-10, 10, 100))
J_vals = np.zeros((len(theta0_vals), len(theta1_vals)))
# 计算不同参数组合下的损失函数值
for i in range(len(theta0_vals)):
for j in range(len(theta1_vals)):
t = [theta0_vals[i, j], theta1_vals[i, j]]
J_vals[i, j] = np.sum((np.dot(future, t) - goal) ** 2) / (2 * m)
return theta, J_history
亲,别放截图好吗,把代码贴出来