def test_nn(): req_data = request.get_json() print("--------- 1.load data ------------") dataTest = load_data( [ req_data['x1'] , req_data['x2'] , req_data['x3'] , req_data['x4'] , req_data['x5'] , req_data['x6'] , req_data['x7'] , req_data['x8'] , req_data['x9'] , req_data['x10'] , req_data['x11'] , req_data['x12'] , req_data['x13'] , req_data['x14'] , req_data['x15'] , req_data['x16'] , req_data['x17'] , req_data['x18'] , req_data['x19'] ] ) print("--------- 2.load model ------------") center, delta, w = load_model("messidor_center.txt", "messidor_delta.txt", "messidor_weight.txt") print("--------- 3.get prediction ------------") result = get_predict(dataTest, center, delta, w) print('result', result) print("--------- 4.save result ------------") res = save_predict(result) return jsonify({ "res": res })
def start_nn(): print("--------- 1.load data ------------") feature, label, n_output = load_data("data.txt") print("--------- 2.training ------------") center, delta, w = bp_train(feature, label, 20, 5000, 0.008, n_output) print("--------- 3.get prediction ------------") result = get_predict(feature, center, delta, w) # print("result:", (1 - err_rate(label, result))) print("--------- 4.save model and result ------------") save_model_result(center, delta, w, result) return jsonify({"res": "success"})
def get_action(state): if np.random.uniform(low=0, high=1) < epsilon: return np.random.choice(env.action_space.n) predict = get_predict(state, center, delta, w) return np.argmax(predict)
steps = 0 center = mat(np.random.rand(n_hidden, n)) delta = mat(np.random.rand(1, n_hidden)) w = mat(np.random.rand(n_hidden, n_output)) while steps <= 3000: env.render() #pos, vel = obs[0],obs[1] state1 = normalize_state(obs) state1 = np.matrix(state1) a = get_action(state1) #print('action',a) obs, reward, terminate, _ = env.step(a) total_reward += abs(obs[0] + 0.5) state2 = normalize_state(obs) state2 = np.matrix(state2) predict = get_predict(state2, center, delta, w) target[0][a] = (1 - alpha) * target[0][a] + alpha * ( reward + gamma * np.max(predict)) center, delta, w, loss = bp_train(state1, np.matrix(target), n_hidden, 1, 0.01, 3, center, delta, w) steps += 1 if terminate: print("Finished after: " + str(episode) + " steps" + str(steps)) print("Cumulated Reward: " + str(total_reward)) print("Complete!") break #while True: #env.render()
delta = get_model(file_delta) w = get_model(file_w) return center, delta, w def save_predict(pre): m = shape(pre)[0] result = [] for i in range(m): if (pre[i, 0] < 0.5): pre[i, 0] = 0 else: pre[i, 0] = 1 result.append(str(pre[i, 0])) print(result) return result if __name__ == "__main__": print("--------- 1.load data ------------") dataTest = load_data(x, y) print(dataTest) print("--------- 2.load model ------------") center, delta, w = load_model("messidor_center.txt", "messidor_delta.txt", "messidor_weight.txt") print("--------- 3.get prediction ------------") result = get_predict(dataTest, center, delta, w) print("--------- 4.save result ------------") save_predict(result)