Exemple #1
0
 def init_hidden_layers(self):
     if self.num_of_hidden_layers != 0:
         # First hidden layer
         self.hidden_layers.append(
             nn(self.hidden_layer_neurons_num[0], self.num_of_inputs))
         for i in range(self.num_of_hidden_layers - 1):
             # Other hidden layers
             self.hidden_layers.append(
                 nn(self.hidden_layer_neurons_num[i + 1],
                    self.hidden_layer_neurons_num[i]))
Exemple #2
0
    def __init__(self, alpha=0.3, gamma=1):
        """
            Args:
            alpha:  <float>  learning rate      (default = 0.3)
            gamma:  <float>  value decay rate   (default = 1)
            n:      <int>    number of back steps to update (default = 1)
        """

        self._counter = 0
        # Possibility of taking a random action instead of the best one
        self.epsilon = 0.30

        # Store a neural network attribute to the agent for its Q-value predictions
        self.gamma = gamma
        self.nn = nn(gamma)
        self.alpha = alpha

        # Collections of the previous state's information
        self.previous_life = 0.0
        self.previous_possable_actions = []
        self.previous_state = None
        self.previous_action = None
        self.previous_two_states = None
        self.previous_closest_enemy = 0
        self.previous_closest_wall = 0

        self.action_map = {'left': 0, 'right': 1, 'up': 2, 'down': 3}

        self.mse = []
        self.predict_value = 0.0
Exemple #3
0
 def post(self):
     args = parser.parse_args()
     data = args['data']
     data = eval(data)
     dataset = data['value']
     grey_model = gy()  # 实例化灰色预测类
     grey_result = grey_model.start(data=dataset)  # 启动灰色预测进行预测
     dataset = np.asarray(dataset)  # ? 为什么不能 ndarray
     error = dataset - grey_result  # 得到残差
     neuralnetwork_model = nn()  # 神经网络进行实例化
     correction_error, error_min, error_max = neuralnetwork_model.launch(
         1, 10, dataset, error)  # 启动神经网络残差补偿,得到最终预测结果
     predict = dataset - (error * (error_max - error_min) + error_min)
     my_dict = {
         "message": "success",
         "result": predict.tolist(),
         "date": datetime.now()
     }
     return jsonify(my_dict)
Exemple #4
0
 def init_output_layer(self):
     if self.num_of_hidden_layers != 0:
         self.output_layer = nn(self.num_of_outputs,
                                self.hidden_layer_neurons_num[-1])
     else:
         self.output_layer = nn(self.num_of_outputs, self.num_of_inputs)
Exemple #5
0
def orient(name, filename, model_file, model):

    if name == 'train':

        if model == 'nearest' or model == 'best':
            train = pd.read_csv(filename, sep=' ', header=None)
            filename_knn = model_file
            file = open(filename_knn, 'wb')
            pickle.dump(train, file)

        if model == 'nnet':
            train = pd.read_csv(filename, sep=' ', header=None)
            x_train = train.drop(columns=[0, 1], axis=1)
            y_train = train[1]
            y_train = pd.get_dummies(y_train)
            y_columns = y_train.columns
            x_train = x_train.to_numpy()
            y_train = y_train.to_numpy()
            print(x_train.shape[0], 'train samples')
            a = nn(25, 0.001, 0.9)
            (w1, w2, w3, b1, b2, b3) = a.fit(x_train, y_train)

            weights = {
                'w1': w1,
                'w2': w2,
                'w3': w3,
                'b1': b1,
                'b2': b2,
                'b3': b3,
                'y_columns': y_columns
            }
            filename_nn = model_file
            file = open(filename_nn, 'wb')
            pickle.dump(weights, file)

        if model == 'tree':
            dtreemain(name, filename, model_file)

    if name == 'test':

        if model == 'nearest' or model == 'best':
            file = open(model_file, 'rb')
            train = pickle.load(file)
            test = pd.read_csv(filename, sep=' ', header=None)
            X_test = test.drop(columns=[0, 1], axis=1)
            y_filenames = test[0]
            y_test = test[1]
            X_test = X_test.to_numpy()
            y_test = y_test.to_numpy()
            obj = knn(10)
            ypred = obj.predict(train, X_test)
            f = open("output.txt", "w")
            for i in range(len(X_test)):
                with open('output.txt', 'a') as f:
                    f.write(str(y_filenames[i]) + ' ' + str(ypred[i]) + '\n')
        if model == 'tree':
            dtreemain(name, filename, model_file)

        if model == 'nnet':

            test = pd.read_csv(filename, sep=' ', header=None)
            x_test = test.drop(columns=[0, 1], axis=1)
            y_test = test[1]
            y_filenames = test[0]
            y_test = pd.get_dummies(y_test)
            x_test = x_test.to_numpy()
            y_test = y_test.to_numpy()
            print(x_test.shape[0], 'test samples')
            file = open(model_file, 'rb')
            new_weights = pickle.load(file)
            w1f = new_weights['w1']
            w2f = new_weights['w2']
            w3f = new_weights['w3']
            b1f = new_weights['b1']
            b2f = new_weights['b2']
            b3f = new_weights['b3']
            y_columns = new_weights['y_columns']
            a = nn(25, 0.001, 0.9)
            y_test_predicted = a.predict(x_test, w1f, w2f, w3f, b1f, b2f, b3f)
            zero_one = (y_test_predicted == y_test_predicted.max(
                axis=1)[:, None]).astype(int)
            diff = (y_test == zero_one).sum(axis=1)
            accuracy = np.count_nonzero(diff == y_test.shape[1])
            print('accuracy ', accuracy / diff.shape[0] * 100)
            f = open("Output.txt", "w")
            for i in range(len(x_test)):
                with open('Output.txt', 'a') as f:
                    f.write(
                        str(y_filenames[i]) + ' ' +
                        str(y_columns[np.argmax(y_test_predicted[i])]) + '\n')