tensorflow - 如何在另一个神经网络中使用一个神经网络的层?
问题描述
我做了一个神经网络,对衣服的 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()
解决方案
推荐阅读
- java - 使用 Jackson 从 Java 类生成的 JAXB 创建自定义 jsonSchema
- python - 将 Python 脚本作为 Windows 服务运行
- python - numpy,“清理”索引和值数组
- android - 如何在一个模型类(房间)android studio中添加两个表
- amazon-web-services - 如何以经济高效的方式管理 AWS Spot 实例?
- swift - 向分段上传添加自定义自定义参数
- python - 有没有办法可以将我的 REST API (Python-MongoDB) 连接到 PowerBI?
- fortran - 在 Fortran 中使用 MPI_AllReduce 和 MPI_IN_PLACE
- c - 无法在 VSCode 中运行 C
- python - 如何扩展预测值?