如何在另一个神经网络中使用一个神经网络的层?

时间:2021-05-21 22:57:29

标签: tensorflow machine-learning keras neural-network computer-vision

我制作了一个神经网络来对衣服的 mnist 数据集进行分类,效果很好。我使用了 tensorflow 和 keras

现在我想创建一个新的网络来创建类似于 mnist 数据集的图像,有点像那些制作不存在的人的脸的网络这将制作不存在的衣服的图像

我的想法是这样的:

数据集由 28x28 像素的图像组成,有 10 个类别的图像,因此第一层将只有 10 个神经元,每个类别一个,其中 9 个接收噪声,其中一个接收 1,它们是连接到另一层 392 个神经元 (28x28/2),该层连接到 784 个神经元中的一个 (28x28),但这不是最后一层,因为我把我已经制作的网络层能够对图像进行分类。

如果网络末端的分类与开始时的输入匹配,则成功,如果不匹配,则我们训练网络,但我们只训练新层,而不是模型中已经有效的层

>

在训练这个新网络后,我可以只取前三层并将它们分成自己的网络,这应该能够生成我想要的图像

问题是要做到这一点,我需要能够使用现有层的层创建新的神经网络,但我不知道该怎么做

如果你想在这里看到我的代码

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

data = keras.datasets.fashion_mnist

(trainImages, trainLabels), (testImages, testLabels) = 
data.load_data()

classNames = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

trainImages = trainImages/255
testImages = testImages/255

model = keras.Sequential\
        (
            [
                keras.layers.Flatten(input_shape=(28,28)),
                keras.layers.Dense(128,activation='relu'),
                keras.layers.Dense(10,activation='softmax')
            ]
        )
        
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(trainImages,trainLabels,epochs=1)

prediction = model.predict(testImages)
for n in range(30,40):
    plt.grid('False')
    plt.imshow(testImages[n],cmap=plt.cm.binary)
    plt.xlabel('Actual: '+classNames[testLabels[n]])
    plt.title('Prediction: '+classNames[np.argmax(prediction[n])])
    plt.show()

0 个答案:

没有答案