我从openai-gym
游戏cartpole-v0
中获得了一些预先保存的数据,这就是我打算用来训练模型的数据。 github
创建初始数据
import gym
from gym import wrappers
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
from tqdm import tqdm
from file_creator import new_filename
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 tqdm(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 tqdm(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 tqdm(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)
filename = new_filename('data/saved.npy')
np.save(filename, 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
initial_population()
import numpy as np
training_data = np.load('data/saved.npy')
def x_y(training_data):
X = np.array([i[0] for i in training_data])
y = np.array([i[1] for i in training_data])
return X, y
x, y = x_y(training_data)
y.shape
>>> (270057, 2)
x.shape
>>> (270057, 4)
x[1]
>>> array([-1.98146801e-02, 4.37329833e-01, 2.21470899e-04, -5.45477988e-01])
y[1]
>>> array([0, 1])
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.20, shuffle=False)
这是模型的代码
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
def keras_model(x_train, y_train, x_test, y_test):
model = Sequential()
model.add(Dense(128, input_dim=x_train.shape[1], activation='relu'))
model.add(Dense(256,activation='relu'))
model.add(Dense(512,activation='relu'))
model.add(Dense(256,activation='relu'))
model.add(Dense(128,activation='relu'))
#model.add(Dense(10,activation='relu'))
model.add(Dense(y_train.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto')
checkpointer = ModelCheckpoint(filepath="best_weights.hdf5", verbose=0, save_best_only=True) # save best model
model.fit(x_train ,y_train, validation_data=(x_test, y_test),callbacks=[monitor,checkpointer], verbose=2,epochs=1000)
model.load_weights('best_weights.hdf5') # load weights from best model
return model
model = keras_model(x_train, y_train, x_test, y_test)
我执行了代码,并且在它们之间抛出了输入形状错误的错误
muiruri_samuel@vuejs:~/data-science/openai gym$ python3 cartpole2.py
Using TensorFlow backend.
WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
Train on 216045 samples, validate on 54012 samples
Epoch 1/1000
2018-08-17 11:40:33.501571: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
- 66s - loss: 0.6502 - val_loss: 0.6459
Epoch 2/1000
- 69s - loss: 0.6456 - val_loss: 0.6469
Epoch 3/1000
- 69s - loss: 0.6450 - val_loss: 0.6457
Epoch 4/1000
- 67s - loss: 0.6445 - val_loss: 0.6439
Epoch 5/1000
- 62s - loss: 0.6444 - val_loss: 0.6439
Epoch 6/1000
- 63s - loss: 0.6440 - val_loss: 0.6442
Epoch 7/1000
- 63s - loss: 0.6438 - val_loss: 0.6451
Epoch 8/1000
- 69s - loss: 0.6435 - val_loss: 0.6430
Epoch 9/1000
- 68s - loss: 0.6435 - val_loss: 0.6436
Epoch 00009: early stopping
0%| | 0/500 [00:00<?, ?it/s]
Traceback (most recent call last):
File "cartpole2.py", line 196, in <module>
action = np.argmax(model.predict(prev_obs.reshape(-1,len(prev_obs),1))[0])
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1147, in predict
x, _, _ = self._standardize_user_data(x)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 749, in _standardize_user_data
exception_prefix='input')
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py", line 127, in standardize_input_d
ata
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (1,
4, 1)
最后一行
ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (1,
4, 1)
建议X之间可能存在一个维,而不是一维维。
相信解决方案将是重塑X
并可能修复此错误。
编辑
基于当前建议的证明此错误的
muiruri_samuel@vuejs:~/data-science/openai gym$ python3 cartpole2.py
Using TensorFlow backend.
WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
Train on 216045 samples, validate on 54012 samples
Epoch 1/1000
2018-08-17 15:40:15.992262: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
- 62s - loss: 0.6510 - val_loss: 0.6455
Epoch 2/1000
- 62s - loss: 0.6458 - val_loss: 0.6449
Epoch 3/1000
- 62s - loss: 0.6451 - val_loss: 0.6445
Epoch 4/1000
- 62s - loss: 0.6445 - val_loss: 0.6477
Epoch 5/1000
- 56s - loss: 0.6442 - val_loss: 0.6451
Epoch 6/1000
- 57s - loss: 0.6440 - val_loss: 0.6432
Epoch 7/1000
- 56s - loss: 0.6439 - val_loss: 0.6436
- 56s - loss: 0.6442 - val_loss: 0.6451
Epoch 8/1000
- 56s - loss: 0.6435 - val_loss: 0.6449
Epoch 9/1000
- 61s - loss: 0.6434 - val_loss: 0.6435
Epoch 10/1000
- 61s - loss: 0.6434 - val_loss: 0.6432
Epoch 11/1000
- 64s - loss: 0.6434 - val_loss: 0.6432
Epoch 00011: early stopping
0%| | 0/500 [00:00<?, ?it/s]
Traceback (most recent call last):
File "cartpole2.py", line 201, in <module>
action = np.argmax(model.predict(prev_obs.reshape(-1,len(prev_obs),1))[0])
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1147, in predict
x, _, _ = self._standardize_user_data(x)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 749, in _standardize_user_data
exception_prefix='input')
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py", line 127, in standardize_input_d
ata
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (1,
4, 1)
形成的时间比上一个形成的时间晚。
检查了异常值,没有发现异常
counter = 0
for i in x:
if i.shape != (4,):
print(counter)
counter += 1
#no output
counter = 0
for i in y:
if i.shape != (2,):
print(counter)
counter += 1
#no output