我有这个数据集
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"test:func": "ng test --config karma.func.conf.js"
"lint": "ng lint",
"e2e": "ng e2e"
}
我把它分成如下:
step pos_x pos_y vel_x vel_y ship_lander_angle ship_lander_angular_vel leg_1_ground_contact leg_2_ground_contact action
0 0 -0.004053 0.937387 -0.410560 -0.215127 0.004703 0.092998 0.0 0.0 3
1 1 -0.008040 0.933774 -0.401600 -0.240878 0.007613 0.058204 0.0 0.0 3
2 2 -0.011951 0.929763 -0.392188 -0.267401 0.008632 0.020372 0.0 0.0 3
3 3 -0.015796 0.925359 -0.383742 -0.293582 0.007955 -0.013536 0.0 0.0 3
4 4 -0.019576 0.920563 -0.375744 -0.319748 0.005674 -0.045625 0.0 0.0 3
我使用神经网络训练我的数据
X = dataset[dataset.columns.difference(["action"])]
Y = dataset["action"]
# Use a range scaling to scale all variables to between 0 and 1
min_max_scaler = preprocessing.MinMaxScaler()
cols = X.columns
X = pd.DataFrame(min_max_scaler.fit_transform(X), columns = cols) # Watch out for putting back in columns here
# Perfrom split to train, validation, test
x_train_plus_valid, x_test, y_train_plus_valid, y_test = train_test_split(X, Y, random_state=0, test_size = 0.30, train_size = 0.7)
x_train, x_valid, y_train, y_valid = train_test_split(x_train_plus_valid, y_train_plus_valid, random_state=0, test_size = 0.199/0.7, train_size = 0.5/0.7)
# convert to numpy arrays
y_train_wide = keras.utils.to_categorical(np.asarray(y_train)) # convert the target classes to binary
y_train_plus_valid_wide = keras.utils.to_categorical(np.asarray(y_train_plus_valid))
y_valid_wide = keras.utils.to_categorical(np.asarray(y_valid))
我几乎达到了93%的准确率。我将模型保存如下
model_mlp = Sequential()
model_mlp.add(Dense(input_dim=9, units=32))
model_mlp.add(Activation('relu'))
model_mlp.add(Dropout(0.2))
model_mlp.add(Dense(32))
model_mlp.add(Activation('relu'))
model_mlp.add(Dropout(0.2))
model_mlp.add(Dense(4))
model_mlp.add(Activation('softmax'))
#model.add(Dense(num_classes, activation='softmax'))
model_mlp.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model_mlp.fit(np.asfarray(x_train), np.asfarray(y_train_wide), \
epochs=20, batch_size=32, verbose=1, \
validation_data=(np.asfarray(x_valid), np.asfarray(y_valid_wide)))
在另一个文件中我需要加载模型并计算我上面提到的奖励
filepath = "first_model.mod"
model_mlp.save(filepath)
错误位于以下行: if __name__=="__main__":
# Load the Lunar Lander environment
env = LunarLander()
s = env.reset()
# Load and initialise the contrll model
ROWS = 64
COLS = 64
CHANNELS = 1
model = keras.models.load_model("first_model.mod")
# Run the game loop
total_reward = 0
steps = 0
while True:
# Get the model to make a prediction
a = model.predict_classes(s)
a = a[0]
# Step on the game
s, r, done, info = env.step(a)
env.render()
total_reward += r
if steps % 20 == 0 or done:
print(["{:+0.2f}".format(x) for x in s])
print("step {} total_reward {:+0.2f}".format(steps, total_reward))
steps += 1
if done: break
答案 0 :(得分:0)
问题出在这一行:
X = dataset[dataset.columns.difference(["action"])]
9
列,而不是8
,这使得网络与env.step
返回的健身房状态不兼容。这会导致形状不匹配错误。接下来,columns.difference
也会对输入列进行混洗(它们按名称排序)。因此列成为:
Index(['leg_1_ground_contact', 'leg_2_ground_contact', 'pos_x', 'pos_y',
'ship_lander_angle', 'ship_lander_angular_vel', 'step', 'vel_x',
'vel_y'],
dtype='object')
分割X
和y
的正确方法是:
X = dataset.iloc[:,1:-1]
Y = dataset.iloc[:,-1]