Вот код из моего проекта. Я продолжаю получать эту ошибку, и я понятия не имею, почему. Ошибка в названии, и это нейронная net для открытого тренажерного зала AI "CartPole-v1". Испытано почти все, что я знаю.
X имеет 2700 выборок данных, и каждая выборка представляет собой массив 4 наблюдения, а y - список действий для получения указанного наблюдения.
import gym
import time
import numpy as np
import tensorflow as tf
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.callbacks import TensorBoard
from keras.optimizers import Adam
env = gym.make("CartPole-v1")
def initial_population(threshold,n_samples):
trainingData = []
accepted_scores = []
while True:
score = 0
game_memory = []
prev_observation =[]
prev_state = env.reset()
while True:
action = np.random.randint(0,2)
observation, reward, done, _ =env.step(action)
score += reward
if len(prev_observation) == 0:
game_memory.append([prev_state,action])
else:
game_memory.append([prev_observation,action])
prev_observation = observation
if done: break
if score >= threshold:
accepted_scores.append(score)
for data in game_memory:
if data[1] == 1:
output = [0,1]
if data[1] == 0:
output = [1,0]
trainingData.append([data[0],output])
if len(accepted_scores) >= n_samples:
break
return trainingData,accepted_scores
# trainingData, acceptedScores = initial_population(100,1000)
# np.save("saved.npy",trainingData)
trainingData = np.load("saved.npy",allow_pickle = True)
def create_model(input_size):
model = Sequential()
model.add(Dense(128,activation="relu",input_shape = (input_size,)))
model.add(Dropout(0.2))
model.add(Dense(256,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(512,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(256,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(128,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(64,activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(2,activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.001), metrics=['accuracy'])
return model
tensorboard = TensorBoard(log_dir=f"logs/Cartpole_Bot-{int(time.time())}")
def train_model(training_data,tensorboard,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:
model = create_model(len(X[0]))
else:
model = tf.keras.models.load_model(f"models/{model}")
model.fit(X,y,batch_size = 100,epochs=5,validation_split =0.2, shuffle=True, callbacks=[tensorboard])
return model
model = train_model(trainingData,tensorboard,False)