задача должна использывать весь ресурс cpu и ядер, вот мой код const fs =…
- User
задача должна использывать весь ресурс cpu и ядер, вот мой код const fs = require('fs').promises;
// Функции активации и их производные function sigmoid(x) { return 1 / (1 + Math.exp(-x)); }
function sigmoidDerivative(x) { return x * (1 - x); }
function relu(x) { return x > 0 ? x : 0; }
function reluDerivative(x) { return x > 0 ? 1 : 0; }
function softmax(arr) { const max = Math.max(...arr); const exps = arr.map(x => Math.exp(x - max)); const sum = exps.reduce((a, b) => a + b, 0); return exps.map(x => x / sum); }
// Функция для токенизации предложения с улучшенной предобработкой function tokenize(sentence) { return sentence .toLowerCase() .replace(/[.,!?]/g, '') // Удаление знаков препинания .split(' ') .filter(word => word); }
// Функция для создания TF-IDF векторов class TfidfVectorizer { constructor(vocabulary) { this.vocabulary = vocabulary; this.idf = new Array(vocabulary.length).fill(0); }
fit(docs) { const docCount = docs.length; const termDocCounts = new Array(this.vocabulary.length).fill(0); docs.forEach(doc => { const tokens = new Set(tokenize(doc)); tokens.forEach(word => { let index = this.vocabulary.indexOf(word); if (index !== -1) termDocCounts[index]++; }); }); this.idf = this.vocabulary.map((word, idx) => { return Math.log((1 + docCount) / (1 + termDocCounts[idx])) + 1; }); } transform(doc) { let tf = new Array(this.vocabulary.length).fill(0); tokenize(doc).forEach(word => { let index = this.vocabulary.indexOf(word); if (index !== -1) { tf[index]++; } }); let tfidf = tf.map((termFreq, idx) => termFreq * this.idf[idx]); return tfidf; }}
// Инициализация нейронной сети с дополнительными улучшениями class NeuralNetwork { constructor(input_nodes, hidden_nodes, output_nodes) { this.input_nodes = input_nodes; this.hidden_nodes = hidden_nodes; this.output_nodes = output_nodes;
this.weights_ih = this.initWeights(this.hidden_nodes, this.input_nodes); this.weights_ho = this.initWeights(this.output_nodes, this.hidden_nodes); this.bias_h = new Array(this.hidden_nodes).fill(0).map(() => Math.random() * 2 - 1); this.bias_o = new Array(this.output_nodes).fill(0).map(() => Math.random() * 2 - 1); this.velocity_ih = this.initZeroes(this.hidden_nodes, this.input_nodes); this.velocity_ho = this.initZeroes(this.output_nodes, this.hidden_nodes); } initWeights(rows, cols) { return new Array(rows).fill(0).map(() => new Array(cols).fill(0).map(() => Math.random() * 0.01 - 0.005) ); } initZeroes(rows, cols) { return new Array(rows).fill(0).map(() => new Array(cols).fill(0)); } feedforward(input_array) { let hidden = this.weights_ih.map((weights, i) => weights.reduce((sum, w, j) => sum + w * input_array[j], this.bias_h[i]) ).map(x => relu(x)); let output = this.weights_ho.map((weights, i) => weights.reduce((sum, w, j) => sum + w * hidden[j], this.bias_o[i]) ); output = softmax(output); return output; } train(input_array, target_array, learning_rate = 0.1, momentum = 0.9, lambda = 0.01) { let hidden_inputs = this.weights_ih.map((weights, i) => weights.reduce((sum, w, j) => sum + w * input_array[j], this.bias_h[i]) ); let hidden_outputs = hidden_inputs.map(x => relu(x)); let final_inputs = this.weights_ho.map((weights, i) => weights.reduce((sum, w, j) => sum + w * hidden_outputs[j], this.bias_o[i]) ); let final_outputs = softmax(final_inputs); let output_errors = final_outputs.map((output, i) => final_outputs[i] - target_array[i]); let hidden_errors = new Array(this.hidden_nodes).fill(0); for (let i = 0; i < this.output_nodes; i++) { for (let j = 0; j < this.hidden_nodes; j++) { hidden_errors[j] += this.weights_ho[i][j] * output_errors[i]; } } for (let i = 0; i < this.output_nodes; i++) { for (let j = 0; j < this.hidden_nodes; j++) { let delta = output_errors[i] * hidden_outputs[j]; this.velocity_ho[i][j] = momentum * this.velocity_ho[i][j] - learning_rate * delta; this.weights_ho[i][j] += this.velocity_ho[i][j]; } this.bias_o[i] -= learning_rate * output_errors[i]; } for (let i = 0; i < this.hidden_nodes; i++) { let hidden_gradient = hidden_errors[i] * reluDerivative(hidden_inputs[i]); for (let j = 0; j < this.input_nodes; j++) { let delta = hidden_gradient * input_array[j]; this.velocity_ih[i][j] = momentum * this.velocity_ih[i][j] - learning_rate * delta; this.weights_ih[i][j] += this.velocity_ih[i][j]; } this.bias_h[i] -= learning_rate * hidden_gradient; } } - Assistant
- User
напиши весь код целиком
- Assistant