ValueError:“生成器”层的输入 1 与该层不兼容:预期形状=(无,128),发现形状=(32, 20)

问题描述 投票:0回答:1

我正在尝试使用我自己的数据运行一个程序,其中包含 4 个特征和 11599 行。 我收到此错误: ValueError:层“生成器”的输入 1 与该层不兼容:预期形状=(无,128),发现形状=(32, 20) 下面是完整的错误。

ValueError                                Traceback (most recent call last)
<ipython-input-31-966c8c98f44b> in <module>()
    176 if __name__ == '__main__':
    177     malgan = MalGAN()
--> 178     malgan.train(epochs=1000, batch_size=128)

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1801, in predict_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1790, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1783, in run_step  **
        outputs = model.predict_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1751, in predict_step
        return self(x, training=False)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 1 of layer "generator" is incompatible with the layer: expected shape=(None, 128), found shape=(32, 20)

这是我的代码:

from __future__ import print_function, division
from keras.layers import Input, Dense, Activation
from keras.layers import Maximum, Concatenate
from keras.models import Model
from tensorflow.keras.optimizers import Adam
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

class MalGAN():
    def __init__(self):
        self.apifeature_dims = 128
        self.z_dims = 20   #could try 20,malware

        self.hide_layers = 256
        self.generator_layers = [self.apifeature_dims+self.z_dims, self.hide_layers, self.apifeature_dims]
        self.substitute_detector_layers = [self.apifeature_dims, self.hide_layers, 1]
        self.blackbox = 'MLP'
        optimizer = Adam(lr=0.001)

        # Build and Train blackbox_detector
        self.blackbox_detector = self.build_blackbox_detector()

        # Build and compile the substitute_detector
        self.substitute_detector = self.build_substitute_detector()
        self.substitute_detector.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes malware and noise as input and generates adversarial malware examples
        example = Input(shape=(128,),name='exampe',dtype='float32')
        noise = Input(shape=(128,),name='noise',dtype='float32')
        input = [example, noise]
        malware_examples = self.generator(input)

        # For the combined model we will only train the generator
        self.substitute_detector.trainable = True

        # The discriminator takes generated images as input and determines validity
        validity = self.substitute_detector(malware_examples)

        # The combined model  (stacked generator and substitute_detector)
        # Trains the generator to fool the discriminator
        self.combined = Model(input, validity)
        self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)

    def build_blackbox_detector(self):

        if self.blackbox is 'MLP':
            blackbox_detector = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4,
                                              solver='sgd', verbose=0, tol=1e-4, random_state=1,
                                              learning_rate_init=.1)
        return blackbox_detector

    def build_generator(self):
         
        example = Input(shape=(128,),name='exampe',dtype='float32')
        noise = Input(shape=(128,),name='noise',dtype='float32')
        x = Concatenate(axis=1)([example, noise])
        for dim in self.generator_layers[1:]:
            x = Dense(dim)(x)
            x = Activation(activation='sigmoid')(x)
        x = Maximum()([example, x])
        generator = Model([example, noise], x, name='generator')
        generator.summary()
        return generator

    def build_substitute_detector(self):
        
        input = Input(shape=(128,),dtype='float32')
        x = input
        for dim in self.substitute_detector_layers[1:]:
            x = Dense(dim)(x)
            x = Activation(activation='sigmoid')(x)
        substitute_detector = Model(input, x, name='substitute_detector')
        substitute_detector.summary()
        return substitute_detector

    def load_data(self, filename):
        # data = pd.read_csv("feature_vectors_system calls.csv")
        data = np.load(filename)
        xmal, ymal, xben, yben = data['arr_0'].reshape(-1,1), data['arr_1'].reshape(-1,1), data['arr_2'].reshape(-1,1), data['arr_3'].reshape(-1,1)
        return (xmal, ymal), (xben, yben)

    def train(self, epochs, batch_size=128):

        # Load the dataset
        (xmal, ymal), (xben, yben) = self.load_data('my_data.npz')
        xtrain_mal, xtest_mal, ytrain_mal, ytest_mal = train_test_split(xmal, ymal, test_size=0.20)
        xtrain_ben, xtest_ben, ytrain_ben, ytest_ben = train_test_split(xben, yben, test_size=0.20)
        print("here 1")
        # Train blackbox_detctor
        self.blackbox_detector.fit(np.concatenate([xmal, xben]),
                                   np.concatenate([ymal, yben]))

        ytrain_ben_blackbox = self.blackbox_detector.predict(xtrain_ben)
        Original_Train_TRR = self.blackbox_detector.score(xtrain_mal, ytrain_mal)
        Original_Test_TRR = self.blackbox_detector.score(xtest_mal, ytest_mal)
        print("here 2")
        Train_TRR, Test_TRR = [], []

        for epoch in range(epochs):

            for step in range(1):#range(xtrain_mal.shape[0] // batch_size):
                # ---------------------
                #  Train substitute_detector
                # ---------------------

                # Select a random batch of malware examples
                idx = np.random.randint(0, xtrain_mal.shape[0],batch_size)
                xmal_batch = xtrain_mal[idx]
                noise = np.random.uniform(0, 1, (batch_size, self.z_dims))
                idx = np.random.randint(0, xmal_batch.shape[0], batch_size)
                xben_batch = xtrain_ben[idx]
                yben_batch = ytrain_ben_blackbox[idx]

                # Generate a batch of new malware examples
                gen_examples = self.generator.predict([xmal_batch, noise])
                print("xmal_batch is:",xmal_batch)
                ymal_batch = self.blackbox_detector.predict(np.ones(gen_examples.shape)*(gen_examples > 0.5))

                # Train the substitute_detector
                d_loss_real = self.substitute_detector.train_on_batch(gen_examples, ymal_batch)
                d_loss_fake = self.substitute_detector.train_on_batch(xben_batch, yben_batch)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

                # ---------------------
                #  Train Generator
                # ---------------------

                idx = np.random.randint(0, xtrain_mal.shape[0], batch_size)
                xmal_batch = xtrain_mal[idx]
                noise = np.random.uniform(0, 1, (batch_size, self.z_dims))

                # Train the generator
                g_loss = self.combined.train_on_batch([xmal_batch, noise], np.zeros((batch_size, 1)))

            # Compute Train TRR
            noise = np.random.uniform(0, 1, (xtrain_mal.shape[0], self.z_dims))
            gen_examples = self.generator.predict([xtrain_mal, noise])
            TRR = self.blackbox_detector.score(np.ones(gen_examples.shape) * (gen_examples > 0.5), ytrain_mal)
            Train_TRR.append(TRR)

            # Compute Test TRR
            noise = np.random.uniform(0, 1, (xtest_mal.shape[0], self.z_dims))
            gen_examples = self.generator.predict([xtest_mal, noise])
            TRR = self.blackbox_detector.score(np.ones(gen_examples.shape) * (gen_examples > 0.5), ytest_mal)
            Test_TRR.append(TRR)

            # Plot the progress
            print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

        print('Original_Train_TRR: {0}, Adver_Train_TRR: {1}'.format(Original_Train_TRR, Train_TRR[-1]))
        print('Original_Test_TRR: {0}, Adver_Test_TRR: {1}'.format(Original_Test_TRR, Test_TRR[-1]))

        # Plot TRR
        plt.figure()
        plt.plot(range(epochs), Train_TRR, c='r', label='Training Set', linewidth=2)
        plt.plot(range(epochs), Test_TRR, c='g', linestyle='--', label='Validation Set', linewidth=2)
        plt.xlabel("Epoch")
        plt.ylabel("TRR")
        plt.legend()
        plt.show()
    
       

if __name__ == '__main__':
    malgan = MalGAN()
    malgan.train(epochs=1000, batch_size=128)

我尝试更改输入形状,但仍然出现同样的错误。

python-3.x tensorflow keras deep-learning google-colaboratory
1个回答
0
投票

如何查看输入数据的形状?已经提到过 11599,4。

在此代码中,

class MalGAN():
    def __init__(self):
        self.apifeature_dims = 128
        self.z_dims = 20   #could try 20,malware

初始化时,z dims 设置为 20。并且 apifeature_dims 设置为 128

虽然错误指向相同的形状问题,

ValueError:“生成器”层的输入 1 与 层:预期形状=(无,128),发现形状=(32,20)

请将第3行提到的形状更改为self.z_dims = None

您的结果代码将是:

class MalGAN():
    def __init__(self):
        self.apifeature_dims = 128
        self.z_dims = None   #could try 20,malware
© www.soinside.com 2019 - 2024. All rights reserved.