本文已使用 Google Cloud Translation API 自动翻译。
某些文档最好以原文阅读。
深度学习是学习复杂数据表示的强大工具。自动编码器是一种神经网络,可用于以无监督方式学习这些表示。
在本文中,我们将使用 TensorFlow.js 和 Node.js 构建一个简单的自动编码器。我们将使用 MNIST 数据集,它由手写数字的图像组成。
首先,我们需要安装依赖项。我们将使用以下库:
你可以使用 npm 安装它们:
npm install tensorflowjs mnist
接下来,我们需要加载 MNIST 数据集。我们将使用 mnist 库来执行此操作:
const mnist = require('mnist');
// Load the dataset
const dataset = mnist.set(0.7, 0.3);
// Get the training and test sets
const [train, test] = dataset.getTrainingAndTestSets();
MNIST 数据集由手写数字图像组成,每个图像为 28x28 像素。我们需要将这些图像展平为大小为 784 的向量,然后才能将它们用作自动编码器的输入。
// Flatten the images into vectors
const trainX = train.map(example => example.input);
const testX = test.map(example => example.input);
现在我们有了数据集,我们可以开始构建自动编码器了。我们将使用具有一个隐藏层的全连接神经网络。隐藏层的单元数将少于输入层,这将迫使网络学习数据的压缩表示。
// Create the model
const model = tf.sequential();
// Add the input layer
model.add(tf.layers.dense({
inputShape: [784],
units: 64
}));
// Add the hidden layer
model.add(tf.layers.dense({
units: 32
}));
// Add the output layer
model.add(tf.layers.dense({
units: 784
}));
我们需要为模型指定优化器和损失函数。我们将使用 Adam 优化器和均方误差 (MSE) 损失函数。
// Compile the model
model.compile({
optimizer: 'adam',
loss: 'meanSquaredError'
});
现在我们有了模型,我们可以在训练集上训练它。我们将训练模型 20 个时期。
// Train the model
model.fit(tf.tensor2d(trainX), tf.tensor2d(trainX), {
epochs: 20
}).then(() => {
// Evaluate the model on the test set
model.evaluate(tf.tensor2d(testX), tf.tensor2d(testX));
});
为了可视化训练结果,我们可以使用“tensorflow-vis”库来绘制重建图像。
// Install the library
npm install @tensorflow/tfjs-vis
// Load the library
const tfvis = require('@tensorflow/tfjs-vis');
// Plot the results
tfvis.show.image3d(
model.predict(tf.tensor2d(testX)),
{
width: 28,
height: 28
}
);
在本文中,我们了解了如何使用 TensorFlow.js 和 Node.js 构建一个简单的自动编码器。我们还看到了如何训练自动编码器和可视化结果。
自编码器可用于各种任务,例如降维、去噪和生成建模。如果您有兴趣了解有关自动编码器的更多信息,我推荐以下资源: