gpt4 book ai didi

node.js - 如何在 tensorflow.js 中构建和训练 lstm 网络

转载 作者:搜寻专家 更新时间:2023-11-01 00:50:27 25 4
gpt4 key购买 nike

我正在尝试使用 tensorflow.js 构建和训练 lstm 网络,我的数据集就像

输入:“我不喜欢这双鞋,它们对我来说太黄了。所以返回。”预期输出原因:“颜色”

我可以使用预训练的 word2vec 模型将文本呈现为向量。尝试阅读文档 tensorflow.js但我无法从中得到太多意义,即使从其他来源也找不到关于如何在 tensorflow.js 中实现和训练网络的好例子。

谁能提供一个使用 LSTM 层创建模型并使用 node.js 和 tensorflow.js 对其进行训练的直接示例?

最佳答案

这是一个带有多层 LSTM 的 RNN 示例,它实现了 Word2Vec。我认为您不需要对其进行太多调整来匹配您的用例——可能只需要数据源和超参数。当然,您也需要编写自己的测试函数。既然您说您可以提供自己的预训练 Word2Vec 向量,那么您也可以根据需要删除大部分代码。

我应该注意 - 我写了这个例子,我的偏好是使用一个名为 Numjs 的库创建向量(它是与 Numpy 类似的库),然后将它们转换为 Tensorflow.js 使用的张量。这并不是 Tensorflow.js 设计使用的方式,但我发现他们创建张量的方法过于繁琐且不直观。

无论如何,这是我的示例(使用 node.js)。我希望你觉得它有用。

require('@tensorflow/tfjs-node')
const tf = require('@tensorflow/tfjs');
const fs = require('fs');
var nj = require('numjs');

const maxlen = 30;
const step = 3;
fs.readFile(<file>, 'utf8', function (error, data) {
if (error) throw error;
var text = data.toString();
create_model(text)
});


function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

function indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}
var max = arr[0];
var maxIndex = 0;
for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
max = arr[i];
}
}
return maxIndex;
}

function sample(preds, temperature) {
preds = nj.array(preds, 'float64');
preds = nj.log(preds).divide(temperature)
exp_preds = nj.exp(preds)
preds = exp_preds.divide(nj.sum(exp_preds))
arr = preds.tolist()
return indexOfMax(arr)
}

async function create_model(text) {
/* data prep */
text = text.toLowerCase()
console.log('corpus length:', text.length)
var words = text.replace(/(\r\n\t|\n|\r\t)/gm," ").split(" ")
words = words.filter(onlyUnique)
words = words.sort()
words = words.filter(String)

console.log("total number of unique words" + words.length)

var word_indices = {}
var indices_word = {}
for (let e0 of words.entries()) {
var idx = e0[0]
var word = e0[1]
word_indices[word] = idx
indices_word[idx] = word
}

console.log("maxlen: " + maxlen, " step: " + step)

var sentences = []
var sentences1 = []

var next_words = []
list_words = text.toLowerCase().replace(/(\r\n\t|\n|\r\t)/gm," ").split(" ").filter(String)
console.log('list_words ' + list_words.length)

for (var i = 0; i < (list_words.length - maxlen); i += step) {
var sentences2 = list_words.slice(i, i + maxlen).join(" ")
sentences.push(sentences2)
next_words.push(list_words[i + maxlen])
}
console.log('nb sequences(length of sentences):', sentences.length)
console.log("length of next_word", next_words.length)

console.log('Vectorization...')
var X = nj.zeros([sentences.length, maxlen, words.length])
console.log('X shape' + X.shape)
var y = nj.zeros([sentences.length, words.length])
console.log('y shape' + y.shape)
for (let e of sentences.entries()) {
var i = e[0]
var sentence = e[1]
for (let e2 of sentence.split(" ").entries()) {
var t = e2[0]
var word = e2[1]
X.set(i, t, word_indices[word], 1)
}
y.set(i, word_indices[next_words[i]], 1)
}

console.log('Creating model... Please wait.');

console.log("MAXLEN " + maxlen + ", words.length " + words.length)
var model = tf.sequential();
model.add(tf.layers.lstm({
units: 128,
returnSequences: true,
inputShape: [maxlen, words.length]
}));
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.lstm({
units: 128,
returnSequences: false
}));
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.dense({units: words.length, activation: 'softmax'}));

model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});

x_tensor = tf.tensor3d(X.tolist(), null, 'bool')
//x_tensor.print(true)
y_tensor = tf.tensor2d(y.tolist(), null, 'bool')
//y_tensor.print(true)

/* training */
await model.fit(x_tensor, y_tensor, {
epochs: 100,
batchSize: 32,
callbacks: {
onEpochEnd: async (epoch, logs) => {
console.log(logs.loss + ",")
}
}
})

关于node.js - 如何在 tensorflow.js 中构建和训练 lstm 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com