从tflearn转换为keras

2024-04-27 02:31:57 发布

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

我遵循了本教程,该教程有点过时,不适用于tf2,因此我决定从TFlearn学习到Keras

https://pythonprogramming.net/openai-cartpole-neural-network-example-machine-learning-tutorial/

我最终得到了这个

import gym
import random
import numpy as np
from statistics import median, mean
from collections import Counter

from keras.models import Sequential
from keras.layers import MaxPooling2D, ZeroPadding2D
from keras.layers import Dense, Dropout, Flatten
from keras.optimizers import rmsprop

LR = 1e-3
env = gym.make("CartPole-v0")
env.reset()
goal_steps = 500
score_requirement = 50
initial_games = 10000

def some_random_games_first():
    # Each of these is its own game.
    for episode in range(5):
        env.reset()
        # this is each frame, up to 200...but we wont make it that far.
        for t in range(goal_steps):
            # This will display the environment
            # Only display if you really want to see it.
            # Takes much longer to display it.
            env.render()

            # This will just create a sample action in any environment.
            # In this environment, the action can be 0 or 1, which is left or right
            action = env.action_space.sample()

            # this executes the environment with an action,
            # and returns the observation of the environment,
            # the reward, if the env is over, and other info.
            observation, reward, done, info = env.step(action)
            if done:
                break

#some_random_games_first()


def initial_population():
    # [OBS, MOVES]
    training_data = []
    # all scores:
    scores = []
    # just the scores that met our threshold:
    accepted_scores = []
    # iterate through however many games we want:
    for _ in range(initial_games):
        score = 0
        # moves specifically from this environment:
        game_memory = []
        # previous observation that we saw
        prev_observation = []
        # for each frame in 200
        for _ in range(goal_steps):
            # choose random action (0 or 1)
            action = random.randrange(0,2)
            # do it!
            observation, reward, done, info = env.step(action)

            # notice that the observation is returned FROM the action
            # so we'll store the previous observation here, pairing
            # the prev observation to the action we'll take.
            if len(prev_observation) > 0 :
                game_memory.append([prev_observation, action])
            prev_observation = observation
            score+=reward
            if done: break

        # IF our score is higher than our threshold, we'd like to save
        # every move we made
        # NOTE the reinforcement methodology here.
        # all we're doing is reinforcing the score, we're not trying
        # to influence the machine in any way as to HOW that score is
        # reached.
        if score >= score_requirement:
            accepted_scores.append(score)
            for data in game_memory:
                # convert to one-hot (this is the output layer for our neural network)
                if data[1] == 1:
                    output = [0,1]
                elif data[1] == 0:
                    output = [1,0]

                # saving our training data
                training_data.append([data[0], output])

        # reset env to play again
        env.reset()
        # save overall scores
        scores.append(score)

    # just in case you wanted to reference later
    training_data_save = np.array(training_data)
    np.save('saved.npy',training_data_save)

    # some stats here, to further illustrate the neural network magic!
    print('Average accepted score:',mean(accepted_scores))
    print('Median score for accepted scores:',median(accepted_scores))
    print(Counter(accepted_scores))

    return training_data



def neural_network_model(input_size):

    network = Sequential()

    # Note, the most confusing thing here is that the shape of the input to the model is defined as an argument on the first hidden layer. This means that the line of code that adds the first Dense layer is doing 2 things, defining the input or visible layer and the first hidden layer.
    network.add(Flatten())

    network.add(Dense(128, activation='relu'))
    network.add(Dropout(rate=0.2))

    network.add(Dense(256, activation='relu'))
    network.add(Dropout(rate=0.2))

    network.add(Dense(512, activation='relu'))
    network.add(Dropout(rate=0.2))

    network.add(Dense(256, activation='relu'))
    network.add(Dropout(rate=0.2))

    network.add(Dense(128, activation='relu'))
    network.add(Dropout(rate=0.2))

    network.add(Dense(2, activation='softmax'))

    opt = rmsprop()
    network.compile(loss='categorical_crossentropy',
         optimizer = opt,
         metrics = ['accuracy'])


    # network = regression(network, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

    return network


def train_model(training_data, model=False):

    X = np.array([i[0] for i in training_data]).reshape(-1,len(training_data[0][0]),1)
    y = [i[1] for i in training_data]

    if not model:
        #print("hi",len(X[0]))
        model = neural_network_model(input_size = len(X[0]))

    model.fit({'input': X}, {'targets': y}, epochs=5)
    # model.fit({'input': X}, {'targets': y}, n_epoch=5, snapshot_step=500, show_metric=True, run_id='openai_learning')
    return model


training_data = initial_population()
model = train_model(training_data)

我得到以下错误:

Using TensorFlow backend.
Average accepted score: 61.975675675675674
Median score for accepted scores: 57.0
Counter({50.0: 35, 51.0: 28, 55.0: 28, 54.0: 22, 52.0: 20, 53.0: 20, 56.0: 18, 62.0: 17, 57.0: 16, 59.0: 15, 67.0: 12, 60.0: 10, 61.0: 10, 58.0: 9, 64.0: 9, 66.0: 8, 74.0: 6, 76.0: 5, 69.0: 5, 81.0: 5, 63.0: 5, 89.0: 5, 73.0: 5, 77.0: 4, 68.0: 4, 71.0: 4, 75.0: 4, 70.0: 4, 78.0: 4, 95.0: 3, 87.0: 3, 65.0: 3, 79.0: 3, 97.0: 2, 80.0: 2, 72.0: 2, 106.0: 2, 128.0: 1, 101.0: 1, 90.0: 1, 82.0: 1, 109.0: 1, 115.0: 1, 85.0: 1, 88.0: 1, 99.0: 1, 84.0: 1, 112.0: 1, 86.0: 1, 104.0: 1})
2020-05-14 23:34:06.986505: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-05-14 23:34:06.986541: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-05-14 23:34:06.986560: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (etherboy): /proc/driver/nvidia/version does not exist
2020-05-14 23:34:06.986770: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-05-14 23:34:07.015567: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2693780000 Hz
2020-05-14 23:34:07.016047: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f4434000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-14 23:34:07.016100: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "gym-test.py", line 160, in <module>
    model = train_model(training_data)
  File "gym-test.py", line 154, in train_model
    model.fit({'input': X}, {'targets': y}, epochs=5)
  File "/home/etherboy/.local/lib/python3.8/site-packages/keras/engine/training.py", line 1150, in fit
    x, y, sample_weights = self._standardize_user_data(
  File "/home/etherboy/.local/lib/python3.8/site-packages/keras/engine/training.py", line 490, in _standardize_user_data
    raise ValueError('Please do not pass a dictionary '
ValueError: Please do not pass a dictionary as model inputs.

Tags: thetoinenvaddfordatamodel