使用TFlearn训练Tensor Flow模型时的Nan损失值

时间:2018-08-01 03:34:33

标签: python tensorflow tflearn

我正在建立一个与Ai Gyms赛车环境配合使用的神经网络。我已经收集了数据,现在正在使用这些数据来训练我的网络。我得到的问题是我的准确度为100%,损失值为Nan。我怀疑我没有正确格式化数据进行培训。 X数组是(999,96,96,3)y数组是(999,3)。以下是我的代码。

以下是我得到的输出

Training samples: 999
Validation samples: 0
--
Training Step: 16  | total loss: nan | time: 0.871s
| Adam | epoch: 001 | loss: nan - acc: 1.0000 -- iter: 999/999

-我的代码

import gym
import random
import numpy as np
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from statistics import median, mean
from collections import Counter


import gym
import numpy as np

LR = 1e-4
training_data = np.load("test.npy", allow_pickle=True)
#separate the 3d array into the observation and the action
pic = training_data[:,:,0]
control = training_data[:,:,1]

#pic = np.reshape(pic, -1 , 1)
#control = np.reshape(control, -1, 1)

#print(np.array([i[0] for i in pic]).shape)
X = np.array([i[0] for i in pic])
y = np.array([i[0] for i in control])


goal_steps = 1300

env = gym.make("CarRacing-v0")
#env.reset()

def main():
    model = train_model(training_data)


def neural_network_model():

    network = input_data(shape = [None, 96 , 96, 3], name = 'input')

    network = fully_connected(network, 128, activation = 'linear')
    network = dropout(network, 0.8)

    network = fully_connected(network, 256, activation = 'linear')
    network = dropout(network, 0.8)

    network = fully_connected(network, 512, activation = 'linear')
    network = dropout(network, 0.8)

    network = fully_connected(network, 256, activation = 'linear')
    network = dropout(network, 0.8)

    network = fully_connected(network, 128, activation = 'linear')
    network = dropout(network, 0.8)

    network = fully_connected(network, 3, activation = 'linear')
    network = regression(network, optimizer='adam', learning_rate=LR, loss = 'categorical_crossentropy', name = 'targets')
    model = tflearn.DNN(network, tensorboard_dir='log')

    return model

def train_model(training_data, model = False):
    print("We are training")

    if not model:
        model = neural_network_model()

    model.fit(X,y,n_epoch=1, snapshot_step = 500, show_metric = True, run_id = 'openai_learning')
    print("model")
    print(model)
    return model
    print("Trained>>>>")

'''
def playWithModel(model):
    scores = []

    for each_game in range(10):
        score = 0
        game_memory = []
        prev_obs = []
        env.reset()
        for _ in range(goal_steps):
            env.render()
            if len(prev_obs) == 0:
                action = np.array([random.uniform(-1,1),random.uniform(0,1),random.uniform(0,1)])
            else:
                print(prev_obs.shape)
                action = np.argmax([model.predict(prev_obs)])

            new_observation, reward, done, info = env.step(action)
            prev_obs = new_observation
            game_memory.append([new_observation, action])
            score += reward
            if done:
                break
        scores.append(score)
    print("Average Score:", sum(scores)/len(scores))
'''

if __name__ == '__main__':
    main()

0 个答案:

没有答案