"tflearn损失始终为0.0,同时训练强化学习代理"

2024-04-27 07:51:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用以下代码,通过gym和tflearn培训强化学习代理:

from tflearn import *
import gym
import numpy as np

env = gym.make('CartPole-v0')
x = []
y = []
max_reward = 0

for i in range(1000):
    env.reset()
    while True:
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            break
        if reward >= max_reward:
            x.append(observation)
            y.append(np.array([action]))
x = np.asarray(x)
y = np.asarray(y)

net = input_data((None,4))
net = fully_connected(net,8,'softmax')
net = fully_connected(net,16,'softmax')
net = fully_connected(net,32,'softmax')
net = fully_connected(net,64,'softmax')
net = fully_connected(net,128,'softmax')
net = fully_connected(net,64,'softmax')
net = fully_connected(net,32,'softmax')
net = fully_connected(net,16,'softmax')
net = fully_connected(net,8,'softmax')
net = fully_connected(net,4,'softmax')
net = fully_connected(net,2,'softmax')
net = fully_connected(net,1)
net = regression(net,optimizer='adam',learning_rate=0.01,loss='categorical_crossentropy',batch_size=1)
model = DNN(net)

model.fit(x,y,10)
model.save('saved/model.tflearn')

问题是,当模型训练时,损失总是0.0。 有人能帮我解决这个问题吗?


Tags: importenvnetmodelnpactionmaxgym
1条回答
网友
1楼 · 发布于 2024-04-27 07:51:25

不确定你的目标是什么,但是categorical_crossentropy是一个用于多类分类的损失函数,但是你的网络的输出只是一个具有线性激活的单位fully_connected(net,1),这就是为什么你得到的损失为0。在

尝试使用mean_square甚至binary_crossentropy,您将看到不同的损失值。在

我将在最后一层使用sigmoid激活,在其余层使用relus。在

相关问题 更多 >