正在改進的GAN模型,可至Aidea下載B.csv
In [1]:
!python -m pip install --upgrade pip
!pip install tensorflow
Requirement already satisfied: pip in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (21.3.1) Requirement already satisfied: tensorflow in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (2.6.2) Requirement already satisfied: numpy~=1.19.2 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.19.2) Requirement already satisfied: six~=1.15.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.15.0) Requirement already satisfied: wheel~=0.35 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (0.37.1) Requirement already satisfied: clang~=5.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (5.0) Requirement already satisfied: opt-einsum~=3.3.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (3.3.0) Requirement already satisfied: tensorboard<2.7,>=2.6.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (2.6.0) Requirement already satisfied: typing-extensions~=3.7.4 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (3.7.4.3) Requirement already satisfied: tensorflow-estimator<2.7,>=2.6.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (2.6.0) Requirement already satisfied: astunparse~=1.6.3 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.6.3) Requirement already satisfied: absl-py~=0.10 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (0.15.0) Requirement already satisfied: grpcio<2.0,>=1.37.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.48.2) Requirement already satisfied: h5py~=3.1.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (3.1.0) Requirement already satisfied: flatbuffers~=1.12.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.12) Requirement already satisfied: termcolor~=1.1.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.1.0) Requirement already satisfied: wrapt~=1.12.1 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.12.1) Requirement already satisfied: google-pasta~=0.2 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (0.2.0) Requirement already satisfied: keras-preprocessing~=1.1.2 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (1.1.2) Requirement already satisfied: keras<2.7,>=2.6.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (2.6.0) Requirement already satisfied: protobuf>=3.9.2 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (3.19.6) Requirement already satisfied: gast==0.4.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorflow) (0.4.0) Requirement already satisfied: cached-property in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from h5py~=3.1.0->tensorflow) (1.5.2) Requirement already satisfied: werkzeug>=0.11.15 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (2.0.3) Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (0.6.1) Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (0.4.6) Requirement already satisfied: requests<3,>=2.21.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (2.27.1) Requirement already satisfied: setuptools>=41.0.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (58.0.4) Requirement already satisfied: google-auth<2,>=1.6.3 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (1.35.0) Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (1.8.1) Requirement already satisfied: markdown>=2.6.8 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from tensorboard<2.7,>=2.6.0->tensorflow) (3.3.7) Requirement already satisfied: cachetools<5.0,>=2.0.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (4.2.4) Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (0.3.0) Requirement already satisfied: rsa<5,>=3.1.4 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (4.9) Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.7,>=2.6.0->tensorflow) (1.3.1) Requirement already satisfied: importlib-metadata>=4.4 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from markdown>=2.6.8->tensorboard<2.7,>=2.6.0->tensorflow) (4.8.3) Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (1.26.18) Requirement already satisfied: charset-normalizer~=2.0.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (2.0.12) Requirement already satisfied: idna<4,>=2.5 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (3.4) Requirement already satisfied: certifi>=2017.4.17 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.7,>=2.6.0->tensorflow) (2021.5.30) Requirement already satisfied: dataclasses in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from werkzeug>=0.11.15->tensorboard<2.7,>=2.6.0->tensorflow) (0.8) Requirement already satisfied: zipp>=0.5 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.7,>=2.6.0->tensorflow) (3.6.0) Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<2.7,>=2.6.0->tensorflow) (0.5.0) Requirement already satisfied: oauthlib>=3.0.0 in c:\users\admin\anaconda3\envs\pytorch\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.7,>=2.6.0->tensorflow) (3.2.2)
GAN model¶
In [6]:
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
import tensorflow as tf
import numpy as np
import pandas as pd
# Load the original data
data = pd.read_csv('B.csv') # original data
input_dim = 19
output_dim = 19
## Data preprocessing
# Calculate means
column_means = data.mean(axis=0, skipna=True)
#fixed (1)
# Turn means to integers
closest_integers = np.round(column_means).astype(int)
# Fill in blanks with means and assign to a new variable
data = data.fillna(closest_integers, axis=0)
# Define the Attack model (your model)
attack_model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(19,)), # input layer
tf.keras.layers.Dense(64, activation='relu'), # hidden layer 1
tf.keras.layers.Dense(64, activation='relu'), # hidden layer 2
tf.keras.layers.Dense(5302, activation='softmax') # output layer
])
def build_gan(generator, discriminator):
discriminator.trainable = False
model = tf.keras.Sequential([generator, discriminator])
return model
# Define the GAN model
def build_discriminator(output_dim):
discriminator = tf.keras.Sequential([
tf.keras.layers.Input(shape=(output_dim,)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid') # Output layer with sigmoid activation for binary classification
])
return discriminator
def build_generator(input_dim, output_dim):
generator = tf.keras.Sequential([
tf.keras.layers.Input(shape=(input_dim,)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(output_dim, activation='tanh') # Output layer with tanh activation
])
return generator
# Build and compile models
generator = build_generator(input_dim, output_dim)
discriminator = build_discriminator(output_dim)
gan = build_gan(generator, discriminator)
# Apply gradient clipping to optimizers
clip_value = 1.5 # Adjust this value as needed
gen_optimizer = tf.keras.optimizers.Adam(learning_rate=0.002, clipvalue=clip_value)
disc_optimizer = tf.keras.optimizers.Adam(learning_rate=0.002, clipvalue=clip_value)
generator.compile(loss='binary_crossentropy', optimizer=gen_optimizer)
discriminator.compile(loss='binary_crossentropy', optimizer=disc_optimizer)
gan.compile(loss='binary_crossentropy', optimizer=gen_optimizer)
# Training GAN
num_epochs = 1000
batch_size = 64
import matplotlib.pyplot as plt
# Training GAN
num_epochs = 300
batch_size = 64
# Lists to store loss values
d_losses = []
g_losses = []
with tf.device('/GPU:0'):
for epoch in range(num_epochs):
noise = np.random.randn(batch_size, input_dim)
fake_data = generator.predict(noise)
combined_data = np.vstack((data, fake_data))
labels = np.concatenate((np.ones(data.shape[0]), np.zeros(batch_size)))
indices = np.arange(combined_data.shape[0])
np.random.shuffle(indices)
combined_data = combined_data[indices]
labels = labels[indices]
discriminator_loss = discriminator.train_on_batch(combined_data, labels)
noise = np.random.randn(batch_size, input_dim)
generator_loss = gan.train_on_batch(noise, np.ones(batch_size))
# Store loss values
d_losses.append(discriminator_loss)
g_losses.append(generator_loss)
if epoch % 100 == 0:
print(f'Epoch {epoch}, D Loss: {discriminator_loss}, G Loss: {generator_loss}')
# Plot loss curves
plt.plot(range(num_epochs), d_losses, label='Discriminator Loss')
plt.plot(range(num_epochs), g_losses, label='Generator Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('GAN Training Loss')
plt.legend()
plt.show()
# Save the generator model
generator.save('generator_model')
# Save the discriminator model
discriminator.save('discriminator_model')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-2fb806b0f8dc> in <module> 82 noise = np.random.randn(batch_size, input_dim) 83 fake_data = generator.predict(noise) ---> 84 combined_data = np.vstack((data, fake_data)) 85 labels = np.concatenate((np.ones(data.shape[0]), np.zeros(batch_size))) 86 <__array_function__ internals> in vstack(*args, **kwargs) ~\anaconda3\envs\pytorch\lib\site-packages\numpy\core\shape_base.py in vstack(tup) 281 if not isinstance(arrs, list): 282 arrs = [arrs] --> 283 return _nx.concatenate(arrs, 0) 284 285 <__array_function__ internals> in concatenate(*args, **kwargs) ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 19 and the array at index 1 has size 100738
In [7]:
# 新的資料集
new_data = pd.read_csv('B.csv') # original data
## Data preprocessing
# Calculate means
column_means = new_data.mean(axis=0, skipna=True)
#fixed (1)
# Turn means to integers
closest_integers = np.round(column_means).astype(int)
# Fill in blanks with means and assign to a new variable
new_data = new_data.fillna(closest_integers, axis=0) # 假設你有一個新的資料集,形狀為 (n_samples, 19)
# 使用攻擊模型進行預測
predictions = attack_model.predict(new_data)
# 對於每一列資料,找到預測概率最高的類別
predicted_classes = np.argmax(predictions, axis=1)
import matplotlib.pyplot as plt
# 獲取每個類別的出現次數
unique_classes, class_counts = np.unique(predicted_classes, return_counts=True)
# 繪製散點圖
plt.figure(figsize=(10, 6))
plt.scatter(unique_classes, class_counts, s=50, c='blue', alpha=0.5)
plt.title('Distribution of Predicted Classes')
plt.xlabel('Class Index')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
In [ ]: