鱼C论坛

 找回密码
 立即注册
查看: 763|回复: 6

[已解决]qlearning时间问题

[复制链接]
发表于 2023-6-3 21:28:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面的代码使用了qlearning模型,模型较为简单 一般来说十几分钟就能运行出结果 但是现在运行了三个小时还没有结果 有哪些可能的错误?怎么解决(最好能给出修改后的代码 感谢感谢)
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1)
])

# 编译模型
model.compile(optimizer='adam', loss='mse')

# 定义市场价格函数
def market_price(x, y):
    return 80 - (x + y) - 10

# 定义Q-learning算法
def q_learning():
    num_episodes = 1000  # 训练的轮数
    rewards = []  # 收集每个episode的总收益

    for episode in range(num_episodes):
        state = np.random.choice(states)  # 随机选择初始状态
        done = False
        total_reward = 0

        while not done:
            if np.random.uniform(0, 1) < epsilon:
                # 探索:以epsilon的概率随机选择动作
                action = np.random.uniform(0, 40)
            else:
                # 开发:根据模型预测选择最优动作
                action = model.predict(np.array([state]))[0][0]

            # 更新市场价格并计算收益
            x = state
            y = action
            price = market_price(x, y)
            reward = price * x

            # 进行动作并观察新的状态和收益
            new_state = np.random.choice(states)
            new_reward = market_price(new_state, y) * new_state

            # 计算目标Q值
            target = reward + gamma * model.predict(np.array([new_state]))[0][0]

            # 使用样本(state, target)训练神经网络
            model.fit(np.array([state]), np.array([target]), verbose=0)

            state = new_state
            total_reward += reward

            if new_reward == 0:
                done = True

        rewards.append(total_reward)

    return rewards

# 进行Q-learning训练
rewards = q_learning()

# 绘制产量选择和利润的图像
x_values = states
y_values = model.predict(x_values)
rewards = np.array(rewards)

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x_values, y_values)
plt.xlabel('Production of A')
plt.ylabel('Production of B')
plt.title('Production Selection')

plt.subplot(1, 2, 2)
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Total Rewards')

plt.tight_layout()
plt.show()
最佳答案
2023-6-4 09:02:45
这份代码可能陷入了死循环,导致无法得出结果。在Q-Learning算法的主循环中,只有当市场价格收益为0时才会跳出循环。但是,由于随机选择新状态的方式不保证总是能够达到市场价格收益为0的状态,因此可能会导致程序陷入死循环。
为了解决这个问题,可以添加一个最大步数的限制。如果步数达到限制但是没有达到市场价格收益为0的状态,就强制跳出循环并将该次训练视为失败。
修改后的代码如下所示:
python
Copy Code
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 定义模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1)
])

# 编译模型
model.compile(optimizer='adam', loss='mse')

# 定义市场价格函数
def market_price(x, y):
    return 80 - (x + y) - 10

# 定义Q-learning算法
def q_learning():
    num_episodes = 1000  # 训练的轮数
    rewards = []  # 收集每个episode的总收益
    max_steps = 10000  # 每个episode最大的步数

    for episode in range(num_episodes):
        state = np.random.choice(states)  # 随机选择初始状态
        done = False
        total_reward = 0
        step = 0

        while not done and step < max_steps:
            if np.random.uniform(0, 1) < epsilon:
                # 探索:以epsilon的概率随机选择动作
                action = np.random.uniform(0, 40)
            else:
                # 开发:根据模型预测选择最优动作
                action = model.predict(np.array([state]))[0][0]

            # 更新市场价格并计算收益
            x = state
            y = action
            price = market_price(x, y)
            reward = price * x

            # 进行动作并观察新的状态和收益
            new_state = np.random.choice(states)
            new_reward = market_price(new_state, y) * new_state

            # 计算目标Q值
            target = reward + gamma * model.predict(np.array([new_state]))[0][0]

            # 使用样本(state, target)训练神经网络
            model.fit(np.array([state]), np.array([target]), verbose=0)

            state = new_state
            total_reward += reward
            step += 1

            if new_reward == 0:
                done = True

        rewards.append(total_reward)

    return rewards

# 进行Q-learning训练
rewards = q_learning()

# 绘制产量选择和利润的图像
x_values = states
y_values = model.predict(x_values)
rewards = np.array(rewards)

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x_values, y_values)
plt.xlabel('Production of A')
plt.ylabel('Production of B')
plt.title('Production Selection')

plt.subplot(1, 2, 2)
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Total Rewards')

plt.tight_layout()
plt.show()
这里添加了max_steps变量和step计数器,当step达到max_steps时跳出循环。需要注意的是,如果max_steps设置得过小,可能会影响算法的收敛效果。
1685798882967.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-3 21:29:50 | 显示全部楼层
可能的错误有以下几个方面:

1. 模型参数设置不合理,导致训练时间过长。可以尝试调整模型参数,如增大batch_size,减小网络层数等。

2. 数据集过大,导致训练时间过长。可以尝试缩小数据集,或者使用采样的方式进行训练。

3. 训练过程中出现了梯度消失或爆炸等问题,导致训练时间过长。可以尝试使用其他的优化器,或者使用梯度裁剪等方法进行优化。

修改后的代码如下:

# 定义Q-learning算法
def q_learning():
    num_episodes = 1000  # 训练的轮数
    rewards = []  # 收集每个episode的总收益

    for episode in range(num_episodes):
        state = np.random.choice(states)  # 随机选择初始状态
        done = False
        total_reward = 0

        while not done:
            if np.random.uniform(0, 1) < epsilon:
                # 探索:以epsilon的概率随机选择动作
                action = np.random.uniform(0, 40)
            else:
                # 开发:根据模型预测选择最优动作
                action = model.predict(np.array([state]))[0][0]

            # 更新市场价格并计算收益
            x = state
            y = action
            price = market_price(x, y)
            reward = price * x

            # 进行动作并观察新的状态和收益
            new_state = np.random.choice(states)
            new_reward = market_price(new_state, y) * new_state

            # 计算目标Q值
            target = reward + gamma * model.predict(np.array([new_state]))[0][0]

            # 使用样本(state, target)训练神经网络
            model.train_on_batch(np.array([state]), np.array([target]))

            state = new_state
            total_reward += reward

            if new_reward == 0:
                done = True

        rewards.append(total_reward)

    return rewards

# 进行Q-learning训练
rewards = q_learning()

# 绘制产量选择和利润的图像
x_values = states
y_values = model.predict(x_values)
rewards = np.array(rewards)

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x_values, y_values)
plt.xlabel('Production of A')
plt.ylabel('Production of B')
plt.title('Production Selection')

plt.subplot(1, 2, 2)
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Total Rewards')

plt.tight_layout()
plt.show()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-3 21:32:18 | 显示全部楼层
这段代码会运行非常久,可能的原因是模型训练次数太多或者采样数量过大导致计算时间增长。建议使用更小的采样数量和更少的轮数进行训练,并且增加一些早停条件,如连续多次收益不变就停止训练。

另外,这段代码中没有给出states数组的具体定义,需要确保其定义正确。

以下是修改建议(仅供参考):

  1. import tensorflow as tf
  2. import numpy as np
  3. import matplotlib.pyplot as plt

  4. # 定义神经网络模型
  5. model = tf.keras.Sequential([
  6.     tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)),
  7.     tf.keras.layers.Dense(1)
  8. ])

  9. # 编译模型
  10. model.compile(optimizer='adam', loss='mse')

  11. # 定义市场价格函数
  12. def market_price(x, y):
  13.     return 80 - (x + y) - 10

  14. # 定义Q-learning算法
  15. def q_learning():
  16.     states = np.arange(0, 100, 5)
  17.     num_episodes = 100  # 训练的轮数
  18.     rewards = []  # 收集每个episode的总收益
  19.     consecutive_no_gain = 0  # 连续多少次没有增益

  20.     for episode in range(num_episodes):
  21.         state = np.random.choice(states)  # 随机选择初始状态
  22.         done = False
  23.         total_reward = 0

  24.         while not done:
  25.             if np.random.uniform(0, 1) < epsilon:
  26.                 # 探索:以epsilon的概率随机选择动作
  27.                 action = np.random.uniform(0, 40)
  28.             else:
  29.                 # 开发:根据模型预测选择最优动作
  30.                 action = model.predict(np.array([state]))[0][0]

  31.             # 更新市场价格并计算收益
  32.             x = state
  33.             y = action
  34.             price = market_price(x, y)
  35.             reward = price * x

  36.             # 进行动作并观察新的状态和收益
  37.             new_state = np.random.choice(states)
  38.             new_reward = market_price(new_state, y) * new_state

  39.             # 计算目标Q值
  40.             target = reward + gamma * model.predict(np.array([new_state]))[0][0]

  41.             # 使用样本(state, target)训练神经网络
  42.             model.fit(np.array([state]), np.array([target]), verbose=0)

  43.             state = new_state
  44.             total_reward += reward

  45.             if new_reward == 0 or consecutive_no_gain > 10:
  46.                 done = True

  47.             if total_reward == rewards[-1]:
  48.                 consecutive_no_gain += 1
  49.             else:
  50.                 consecutive_no_gain = 0

  51.         rewards.append(total_reward)

  52.     return rewards

  53. # 定义超参数
  54. epsilon = 0.1
  55. gamma = 0.9

  56. # 进行Q-learning训练
  57. rewards = q_learning()

  58. # 绘制产量选择和利润的图像
  59. x_values = np.arange(0, 100, 5)
  60. y_values = model.predict(x_values)
  61. rewards = np.array(rewards)

  62. plt.figure(figsize=(10, 5))
  63. plt.subplot(1, 2, 1)
  64. plt.plot(x_values, y_values)
  65. plt.xlabel('Production of A')
  66. plt.ylabel('Production of B')
  67. plt.title('Production Selection')

  68. plt.subplot(1, 2, 2)
  69. plt.plot(rewards)
  70. plt.xlabel('Episode')
  71. plt.ylabel('Total Reward')
  72. plt.title('Total Rewards')

  73. plt.tight_layout()
  74. plt.show()
复制代码

这里采样数量减少到了20个状态,轮数减少到了100轮,并增加了停止条件。这些超参数可以根据任务具体情况进行调整。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-3 21:55:37 | 显示全部楼层

评分

参与人数 1荣誉 -5 贡献 -3 收起 理由
歌者文明清理员 -5 -3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-4 09:02:45 | 显示全部楼层    本楼为最佳答案   
这份代码可能陷入了死循环,导致无法得出结果。在Q-Learning算法的主循环中,只有当市场价格收益为0时才会跳出循环。但是,由于随机选择新状态的方式不保证总是能够达到市场价格收益为0的状态,因此可能会导致程序陷入死循环。
为了解决这个问题,可以添加一个最大步数的限制。如果步数达到限制但是没有达到市场价格收益为0的状态,就强制跳出循环并将该次训练视为失败。
修改后的代码如下所示:
python
Copy Code
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 定义模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1)
])

# 编译模型
model.compile(optimizer='adam', loss='mse')

# 定义市场价格函数
def market_price(x, y):
    return 80 - (x + y) - 10

# 定义Q-learning算法
def q_learning():
    num_episodes = 1000  # 训练的轮数
    rewards = []  # 收集每个episode的总收益
    max_steps = 10000  # 每个episode最大的步数

    for episode in range(num_episodes):
        state = np.random.choice(states)  # 随机选择初始状态
        done = False
        total_reward = 0
        step = 0

        while not done and step < max_steps:
            if np.random.uniform(0, 1) < epsilon:
                # 探索:以epsilon的概率随机选择动作
                action = np.random.uniform(0, 40)
            else:
                # 开发:根据模型预测选择最优动作
                action = model.predict(np.array([state]))[0][0]

            # 更新市场价格并计算收益
            x = state
            y = action
            price = market_price(x, y)
            reward = price * x

            # 进行动作并观察新的状态和收益
            new_state = np.random.choice(states)
            new_reward = market_price(new_state, y) * new_state

            # 计算目标Q值
            target = reward + gamma * model.predict(np.array([new_state]))[0][0]

            # 使用样本(state, target)训练神经网络
            model.fit(np.array([state]), np.array([target]), verbose=0)

            state = new_state
            total_reward += reward
            step += 1

            if new_reward == 0:
                done = True

        rewards.append(total_reward)

    return rewards

# 进行Q-learning训练
rewards = q_learning()

# 绘制产量选择和利润的图像
x_values = states
y_values = model.predict(x_values)
rewards = np.array(rewards)

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(x_values, y_values)
plt.xlabel('Production of A')
plt.ylabel('Production of B')
plt.title('Production Selection')

plt.subplot(1, 2, 2)
plt.plot(rewards)
plt.xlabel('Episode')
plt.ylabel('Total Reward')
plt.title('Total Rewards')

plt.tight_layout()
plt.show()
这里添加了max_steps变量和step计数器,当step达到max_steps时跳出循环。需要注意的是,如果max_steps设置得过小,可能会影响算法的收敛效果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-8 09:46:39 | 显示全部楼层
歌者文明清理员 发表于 2023-6-4 09:02
这份代码可能陷入了死循环,导致无法得出结果。在Q-Learning算法的主循环中,只有当市场价格收益为0时才会 ...

活跃一下气氛还扣分,什么情况!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-8 16:52:23 | 显示全部楼层
cmu052115 发表于 2023-6-8 09:46
活跃一下气氛还扣分,什么情况!!!

灌水
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 14:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表