当前位置:   article > 正文

好像还挺好玩的生成式对抗网络生成一维数据(DCGAN)_dcgan处理一维数据

dcgan处理一维数据
from __future__ import print_function, division
import os

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# from keras.datasets import mnist
from keras.layers import Input, Dense, Reshape, Flatten, Dropout
from keras.layers import BatchNormalization, Activation, ZeroPadding2D, GlobalAveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
import pandas as pd
import matplotlib.pyplot as plt
import sys
import numpy as np
import csv


class GAN():
    def __init__(self):
        self.data_rows = 20
        self.data_cols = 20
        self.channels = 1
        self.data_shape = (self.data_rows, self.data_cols, self.channels)
        self.latent_dim = 100
        self.sample_size = 200
        optimizer = Adam(0.0002, 0.5)
        # 构建和编译判别器
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=optimizer,
                                   metrics=['accuracy'])

        # 构建生成器
        self.generator = self.build_generator()

        # 生成器输入噪音,生成假的图片
        z = Input(shape=(self.latent_dim,))
        data = self.generator(z)  # 生成器生成的图片

        # 为了组合模型,只训练生成器,不训练判别器
        self.discriminator.trainable = False

        # 判别器将生成的图像作为输入并确定有效性
        validity = self.discriminator(data)  # 这个是判别器判断生成器生成图片的结果

        # The combined model  (stacked generator and discriminator)
        # 训练生成器骗过判别器
        self.combined = Model(z, validity)
        self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)

    def build_generator(self):
        model = Sequential()
        # 先全连接到32*7*7的维度上
        model.add(Dense(128 * 5 * 5, activation="relu", input_dim=self.latent_dim)
  • 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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号