首页 > 解决方案 > 如何在另一个神经网络中使用一个神经网络的层?

问题描述

我做了一个神经网络,对衣服的 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()

标签: tensorflowmachine-learningkerasneural-networkcomputer-vision

解决方案


推荐阅读