-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess_data.py
68 lines (56 loc) · 2.38 KB
/
process_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import pandas as pd
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
def preprocess_data(data_name):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if data_name == 'Simulation':
a = 1
b = 1
TotalP = 2000
print('p = ', TotalP)
NTrain = 10000
x_train = np.matrix(np.zeros([NTrain, TotalP]))
y_train = np.matrix(np.zeros([NTrain, 1]))
sigma = 1.0
for i in range(NTrain):
if i % 1000 == 0:
print("x_train generate = ", i)
ee = np.sqrt(sigma) * np.random.normal(0, 1)
while ee > 10 or ee < -10:
ee = np.sqrt(sigma) * np.random.normal(0, 1)
for j in range(TotalP):
zj = np.sqrt(sigma) * np.random.normal(0, 1)
while zj > 10 or zj < -10:
zj = np.sqrt(sigma) * np.random.normal(0, 1)
x_train[i, j] = (a * ee + b * zj) / np.sqrt(a * a + b * b)
x0 = x_train[i, 0]
x1 = x_train[i, 1]
x2 = x_train[i, 2]
x3 = x_train[i, 3]
x4 = x_train[i, 4]
y_train[i, 0] = 5 * x1 / (1 + x0 * x0) + 5 * np.sin(x2 * x3) + 2 * x4 + np.random.normal(0, 1)
NTest = 1000
x_test = np.mat(np.zeros([NTest, TotalP]))
y_test = np.mat(np.zeros([NTest, 1]))
for i in range(NTest):
ee = np.sqrt(sigma) * np.random.normal(0, 1)
while ee > 10 or ee < -10:
ee = np.sqrt(sigma) * np.random.normal(0, 1)
for j in range(TotalP):
zj = np.sqrt(sigma) * np.random.normal(0, 1)
while zj > 10 or zj < -10:
zj = np.sqrt(sigma) * np.random.normal(0, 1)
x_test[i, j] = (a * ee + b * zj) / np.sqrt(a * a + b * b)
x0 = x_test[i, 0]
x1 = x_test[i, 1]
x2 = x_test[i, 2]
x3 = x_test[i, 3]
x4 = x_test[i, 4]
y_test[i, 0] = 5 * x1 / (1 + x0 * x0) + 5 * np.sin(x2 * x3) + 2 * x4 + np.random.normal(0, 1)
x_train = torch.FloatTensor(x_train).to(device)
y_train = torch.FloatTensor(y_train).to(device)
x_test = torch.FloatTensor(x_test).to(device)
y_test = torch.FloatTensor(y_test).to(device)
return x_train, y_train, x_test, y_test