2

我目前正在为 openai 开发 AI,我正在尝试传递收集到的随机数据以制作神经网络模型,然后使用该模型创建新数据。当我尝试使用新的训练数据制作另一个模型时,它不会让 e 创建一个新模型并给出一个

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1/X' with dtype float
 [[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]].

我的代码:

import gym
import random
import numpy as np
import tensorflow as tf
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 matplotlib.pyplot as plt


env = gym.make("CartPole-v1")
env.reset()#restarts the enviroment


epoch = 5
LR = 2e-4
max_score = 500
number_of_training_games = 100
generations = 3
training_scores = []
random_gen_score = []


def create_random_training_data():
    x = 0
    accepted_training_data = []
    scores_and_data = [] 
    array_of_scores = []     

    for i in range(number_of_training_games):
        score = 0
        prev_observation = []
        training_data = []
        for _ in range(max_score):
            action = env.action_space.sample()
            observation, reward, done, info = env.step(action)
            if len(prev_observation) > 0:       
                training_data.append([prev_observation, action]) 

            prev_observation = observation
            score += reward

            if done:
                array_of_scores.append(score)
                break

        for i in training_data:
            scores_and_data.append([score,i[0],i[1]])

        # reset enviroment
        env.reset()

    training_scores = array_of_scores

    for data in scores_and_data:

        if data[0] > median(array_of_scores):

            if data[2] == 1:
                output = [0,1]
            elif data[2] == 0:
                output = [1,0]

            accepted_training_data.append([data[1], output])

    return accepted_training_data

def training_model(sample_data):

    inputs = np.array([i[0] for i in sample_data]).reshape(-1,4,1)

    correct_output = [i[1] for i in sample_data]

    model = neural_network(input_size = len(inputs[0]))

    model.fit({'input': inputs}, {'targets': correct_output}, n_epoch=epoch , snapshot_step=500, show_metric=True, run_id='openai_learning')
    print(input)
    return model


def neural_network(input_size):

    # this is where our observation data will go
    network = input_data(shape=[None, input_size, 1], name = 'input')

    # our neural networks

    network = fully_connected(network, 128, activation = 'relu')

    #dropout is used to drop randon nodes inorder to reduce over training 
    network = dropout(network, 0.8)

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

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

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

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

    # this is the output 
    network = fully_connected(network, 2, activation = 'softmax')

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

    return model



def run_generation():

    random_sample_data = []
    trained_data = []


    for i in range(generations):


        if len(random_sample_data) ==0:

            random_sample_data = create_random_training_data()
            model1 = training_model(random_sample_data)

        else:

            trained_data = one_generation(model1)
            model2 = training_model(trained_data)


    return model2


def one_generation(model):
    accepted_training_data = []
    scores_and_data = [] 
    array_of_scores = []     



    for i in range(number_of_training_games):

        score = 0
        prev_observation = []
        training_data = []

        for _ in range(max_score):

            if len(prev_observation) == 0:
                action = env.action_space.sample()

            else:
                action = np.argmax(model.predict(prev_observation.reshape(-1,len(prev_observation),1))[0])

            observation, reward, done, info = env.step(action)

            if len(prev_observation) > 0:

                training_data.append([prev_observation, action]) 

            prev_observation = observation
            score += reward

            if done:
                array_of_scores.append(score)
                break

        for i in training_data:
            scores_and_data.append([score,i[0],i[1]])

        # reset enviroment
        env.reset()

    for data in scores_and_data:

        if data[0] > median(array_of_scores):

            if data[2] == 1:
                output = [0,1]
            elif data[2] == 0:
                output = [1,0]

            accepted_training_data.append([data[1], output])



    return accepted_training_data


def testing():
    scores = []
    model = run_generation()
    for _ in range(100):
        score = 0

        game_memory = []
        prev_obs = []
        env.reset()

        for _ in range(max_score):
            env.render()


            #first move is going to be random 
            if len(prev_obs)==0:
                action = random.randrange(0,2)

            else:

                action = np.argmax(model.predict(prev_obs.reshape(-1,len(prev_obs),1))[0])

            #records actions     
            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 training Score:',sum(training_scores)/len(training_scores))
print('Average Score:',sum(scores)/len(scores))
print (scores)
testing()

错误:

Traceback (most recent call last):
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1039, in _do_call
    return fn(*args)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1021, in _run_fn
    status, run_metadata)
  File "/anaconda/lib/python3.6/contextlib.py", line 89, in __exit__
    next(self.gen)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1/X' with dtype float
     [[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 259, in <module>
    testing()
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 219, in testing
    model = run_generation()
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 148, in run_generation
    model2 = training_model(trained_data)
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 92, in training_model
    model.fit({'input': inputs}, {'targets': correct_output}, n_epoch=epoch , snapshot_step=500, show_metric=True, run_id='openai_learning')
  File "/anaconda/lib/python3.6/site-packages/tflearn/models/dnn.py", line 215, in fit
    callbacks=callbacks)
  File "/anaconda/lib/python3.6/site-packages/tflearn/helpers/trainer.py", line 336, in fit
    show_metric)
  File "/anaconda/lib/python3.6/site-packages/tflearn/helpers/trainer.py", line 777, in _train
    feed_batch)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1/X' with dtype float
     [[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'input_1/X', defined at:
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 259, in <module>
    testing()
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 219, in testing
    model = run_generation()
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 148, in run_generation
    model2 = training_model(trained_data)
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 90, in training_model
    model = neural_network(input_size = len(inputs[0]))
  File "/Users/Duncan/Desktop/ai projects/evolutionDNN.py", line 100, in neural_network
    network = input_data(shape=[None, input_size, 1], name = 'input')
  File "/anaconda/lib/python3.6/site-packages/tflearn/layers/core.py", line 81, in input_data
    placeholder = tf.placeholder(shape=shape, dtype=dtype, name="X")
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
    name=name)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
    name=name)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1/X' with dtype float
     [[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
4

1 回答 1

0

这一行:

network = input_data(shape=[None, input_size, 1], name = 'input')

应该

network = input_data(shape=[None, input_size,input_size , 1], name = 'input')

首先应该有 4 个参数作为占位符。尝试这个。

于 2020-11-22T22:31:46.240 回答