【问题标题】:python time series synthetic data using ydata-synthetic package - Time series GANpython时间序列合成数据使用ydata-synthetic包 - 时间序列GAN
【发布时间】:2021-11-17 19:19:33
【问题描述】:

你好,我尝试为时间序列 GAN 使用合成包 第一次我想输入整数然后输出也是数字但不是,输出数据是十进制数我使用 ydata-synthetic (https://github.com/ydataai/ydata-synthetic)

这是我制作数据的代码,请帮助我

#导入练习所需的库

from os import path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from ydata_synthetic.synthesizers import ModelParameters
from ydata_synthetic.preprocessing.timeseries import processed_stock
from ydata_synthetic.synthesizers.timeseries import TimeGAN
import torch


arr_data = np.random.randint(0,600000,(100,1))

#Specific to TimeGANs

#stock_data
seq_len=20
n_seq = 1  #number of columns


hidden_dim=24
gamma=1

noise_dim = 32
dim = 128
batch_size = len(arr_data) - seq_len

log_step = 100
learning_rate = 5e-4

gan_args = ModelParameters(batch_size=batch_size,
                           lr=learning_rate,
                           noise_dim=noise_dim,
                           layers_dim=dim)


lst_temp = []
for i in range(0,len(arr_data) - seq_len):
    _x = arr_data[i:i+20]
    lst_temp.append(_x)

tens_rand_data = torch.tensor(lst_temp)
lst_rand_data = tens_rand_data.numpy()

synth = TimeGAN(model_parameters=gan_args, hidden_dim=24, seq_len=seq_len, n_seq=n_seq, gamma=1)

synth.train(lst_rand_data, train_steps=10)
synth_data = synth.sample(len(lst_rand_data))
print(synth_data.shape)

cols = ['Car price']

for j, col in enumerate(cols):
    df = pd.DataFrame({'Real': lst_rand_data[-1][:, j],'Synthetic': synth_data[-1][:, j]})
    
df.plot(title = "Car price",secondary_y='Synthetic data', style=['-', '--'])

print(df)

enter image description here

【问题讨论】:

    标签: python time-series generative-adversarial-network


    【解决方案1】:

    在适合 TimeGAN 之前,您的输入应使用 MinMaxScaler 进行处理,并且由于在其生成器的最后一层激活 sigmoid,您将始终收到介于 0 和 1 之间的十进制输出。

    您可以通过 2 种方式更改代码:

    1. 将输入从整数更改为十进制范围 [0,1]。

      arr_data = np.random.randint(0,600000,(100,1))
      

      进入

      arr_data = np.random.uniform(0,1,(100,1))
      

      这样你的虚拟输入不需要缩放,因为它已经在 [0,1] 中。

    2. 使用 MinMaxScaler 缩放数据

      from sklearn.preprocessing import MinMaxScaler
      
      arr_data = np.random.randint(0,600000,(100,1))
      
      scaler = MinMaxScaler(feature_range = (0,1))
      scaled_data = scaler.fit_transform(arr_data)
      
      ...
      

      请注意,使用 TimeGAN 时,您始终会收到来自 [0,1] 的十进制输出。现在,如果您想将合成数据反转为整数,请考虑使用inverse transform

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2016-09-22
      • 2015-08-27
      相关资源
      最近更新 更多