Esempio n. 1
0
def pricepre():
    result = {"success": 'False'}

    class LSTM(nn.Module):
        def __init__(self, input_size=1, hidden_layer_size=6, output_size=1):
            super().__init__()
            self.hidden_layer_size = hidden_layer_size

            self.lstm = nn.LSTM(input_size, hidden_layer_size)

            self.linear = nn.Linear(hidden_layer_size, output_size)

            self.hidden_cell = (
                torch.zeros(1, 1, self.hidden_layer_size),
                torch.zeros(1, 1, self.hidden_layer_size)
            )  # (num_layers * num_directions, batch_size, hidden_size)

        def forward(self, input_seq):
            lstm_out, self.hidden_cell = self.lstm(
                input_seq.view(len(input_seq), 1, -1), self.hidden_cell)
            predictions = self.linear(lstm_out.view(len(input_seq), -1))
            return predictions[-1]

    #model = LSTM()
    model = LSTM(1, 6)
    model.load_state_dict(
        torch.load('model_parameters.pkl'))  # 提取net1的状态参数,将状态参数给net3

    #单维最大最小归一化和反归一化函数
    data_csv = pd.read_csv('price.csv', usecols=[1])  #导入第一列,即价格那一列的数据
    data_csv = data_csv.dropna()  #去掉na数据
    dataset = data_csv.values  #dataframe 转为 ndarray
    dataset = dataset.astype('float32')

    # 数据集分为训练集和测试集
    test_data_size = 8
    train_data = dataset[:-test_data_size]
    test_data = dataset[-test_data_size:]

    def Normalize(list):
        list = np.array(list)
        low, high = np.percentile(list, [0, 100])
        delta = high - low
        if delta != 0:
            for i in range(0, len(list)):
                list[i] = (list[i] - low) / delta
        return list, low, high

    def FNoramlize(list, low, high):
        delta = high - low
        if delta != 0:
            for i in range(0, len(list)):
                list[i] = list[i] * delta + low
        return list

    list, low, high = Normalize(train_data)
    ##3.模型预测
    fut_pred = 8
    train_window = 5

    data = request.get_data()
    print('data=%s' % data)
    json_data = json.loads(data.decode("utf-8"))
    print(json_data)
    test_list = json_data.get('test_list')
    print(test_list)

    def Normalize2(list, low, high):
        list = np.array(test_list)
        delta = high - low
        if delta != 0:
            for i in range(0, len(list)):
                list[i] = (list[i] - low) / delta
        return list.tolist()

    test_inputs = Normalize2(test_list, low, high)
    print(test_inputs)
    ###预测
    model.eval()
    for i in range(fut_pred):
        seq = torch.FloatTensor(test_inputs[-train_window:])  ##是个张量
        print(seq)
        with torch.no_grad():
            model.hidden = (torch.zeros(1, 1, model.hidden_layer_size),
                            torch.zeros(1, 1, model.hidden_layer_size))
            pre = model(seq)  ########################## 预测的结果
            print(pre)
            test_inputs.append(
                model(seq).item())  ########################## 模型预测时模型的输入
    print(test_inputs)  #它包含13个元素。5+8
    print(len(test_inputs))  #13
    test_pre = test_inputs[-fut_pred:]  #最后8个预测结果

    def FNoramlize(list, low, high):
        delta = high - low
        if delta != 0:
            for i in range(0, len(list)):
                list[i] = list[i] * delta + low
        return list

    test_real = FNoramlize(test_pre, low, high)
    print(test_real)

    result["prediction"] = test_real
    result["success"] = True
    return jsonify(result)
def experiment(repeats):
    """
Runs the experiment itself.

    :param repeats: Number of times to repeat the experiment. When we are trying to create a good network, it is reccomended to use 1.
    :return: Error scores for each repeat.
    """
    # create data
    raw_timestamp = range(600)
    raw_timestamp = array(raw_timestamp) + numpy.random.rand(
        len(raw_timestamp))
    diff_timestamp = difference(
        numpy.array(raw_timestamp) +
        2 * numpy.random.rand(raw_timestamp.shape[0]) - 1, 1)
    raw_pos = [fake_position(i / 30) for i in range(-300, 300)]
    raw_pos = array(raw_pos)
    diff_pos = difference(raw_pos, 1)
    diff_pos = array(diff_pos)
    raw_accel = [fake_acceleration(i / 30) for i in range(-300, 300)]
    raw_accel = array(raw_accel)
    diff_accel = difference(raw_accel, 1)
    diff_accel = array(diff_accel)

    # Scaling data
    X_scaler = StandardScaler()
    raw_accel = X_scaler.fit_transform(raw_accel.reshape(-1, 1))
    y_scaler = MinMaxScaler(feature_range=(-1, 1))
    diff_pos = y_scaler.fit_transform(diff_pos.reshape(-1, 1))

    raw_timestamp = raw_timestamp[1:]
    raw_pos = raw_pos[1:]
    raw_accel = raw_accel[1:]

    raw_accel = raw_accel.reshape(-1)
    diff_pos = diff_pos.reshape(-1)

    X, y = timeseries_dataloader(data_x=raw_accel,
                                 data_y=diff_pos,
                                 enable_asymetrical=True)

    model = LSTM(input_size=1,
                 hidden_layer_size=80,
                 n_lstm_units=3,
                 bidirectional=False,
                 output_size=1,
                 training_batch_size=60,
                 epochs=20000,
                 device=device)

    # enabling CUDA
    model.to(device)
    # Let's go fit! Comment if only loading pretrained model.
    model.fit(X, y)

    X_graphic = torch.from_numpy(raw_accel.astype("float32")).to(device)
    y_graphic = diff_pos.astype("float32")

    # =====================TEST=================================================
    model = torch.load("best_model.pth")
    model.to(device)
    yhat = []
    model.hidden_cell = (torch.zeros(model.num_directions * model.n_lstm_units,
                                     1,
                                     model.hidden_layer_size).to(model.device),
                         torch.zeros(model.num_directions * model.n_lstm_units,
                                     1,
                                     model.hidden_layer_size).to(model.device))
    model.eval()
    for X in X_graphic:
        yhat.append(model(X.view(1, -1, 1)).detach().cpu().numpy())
    # from list to numpy array
    yhat = array(yhat).reshape(-1)

    # ======================PLOT================================================
    plt.close()
    plt.plot(range(yhat.shape[0]), yhat, range(y_graphic.shape[0]), y_graphic)
    plt.savefig("output_reconstruction.png", dpi=800)
    # plt.show()
    rmse = mean_squared_error(yhat, y_graphic)**1 / 2
    print("RMSE trajetoria inteira: ", rmse)

    error_scores = []

    return error_scores
def experiment(repeats):
    """
Runs the experiment itself.

    :param repeats: Number of times to repeat the experiment. When we are trying to create a good network, it is reccomended to use 1.
    :return: Error scores for each repeat.
    """
    # transform data to be stationary
    raw_pos = [fake_position(i / 30) for i in range(-300, 300)]
    diff_pos = difference(raw_pos, 1)
    raw_accel = [fake_acceleration(i / 30) for i in range(-300, 300)]
    diff_accel = difference(raw_accel, 1)
    # diff_accel = numpy.array(raw_accel)

    # raw_timestamp = range(len(raw_pos))
    # # diff_timestamp = difference(numpy.array(raw_timestamp) + numpy.random.rand(len(raw_pos)), 1)
    # diff_timestamp = array(raw_timestamp)

    # testar com lstm BIDIRECIONAL
    model = LSTM(input_size=1,
                 hidden_layer_size=40,
                 n_lstm_units=3,
                 bidirectional=False,
                 output_size=1,
                 training_batch_size=60,
                 epochs=2000,
                 device=device)

    raw_pos = raw_pos[1:]
    raw_accel = raw_accel[1:]

    raw_accel = array(raw_accel)
    raw_pos = array(raw_pos)
    diff_accel = array(diff_accel)
    diff_pos = array(diff_pos)

    X_scaler = StandardScaler()
    raw_accel = X_scaler.fit_transform(raw_accel.reshape(-1, 1))
    y_scaler = MinMaxScaler(feature_range=(-1, 1))
    diff_pos = y_scaler.fit_transform(diff_pos.reshape(-1, 1))

    raw_accel = raw_accel.reshape(-1)
    diff_pos = diff_pos.reshape(-1)

    X, y = timeseries_dataloader(data_x=raw_accel,
                                 data_y=diff_pos,
                                 enable_asymetrical=True)

    # Invertendo para a sequencia ser DEcrescente.
    # X = X[::-1]
    # y = y[::-1]

    # enabling CUDA
    model.to(device)
    # Let's go fit
    model.fit(X, y)

    X_graphic = torch.from_numpy(raw_accel.astype("float32")).to(device)
    y_graphic = torch.from_numpy(diff_pos.astype("float32")).to(device)

    model = torch.load("best_model.pth")
    model.to(device)
    yhat = []
    model.hidden_cell = (torch.zeros(model.num_directions * model.n_lstm_units,
                                     1,
                                     model.hidden_layer_size).to(model.device),
                         torch.zeros(model.num_directions * model.n_lstm_units,
                                     1,
                                     model.hidden_layer_size).to(model.device))
    model.eval()
    for X in X_graphic:
        yhat.append(model(X.view(1, -1, 1)))

    # report performance
    plt.close()
    plt.plot(range(len(yhat)), yhat, range(len(y)), y)
    plt.savefig("output_train.png", dpi=800)
    plt.show()
    # rmse = mean_squared_error(raw_pos[:len(train_scaled)], predictions)

    error_scores = []

    return error_scores