как избавится от yTrain? var xTrain = await sentenceEncoder.embed(sentences); var yTrain…
- User
как избавится от yTrain? var xTrain = await sentenceEncoder.embed(sentences); var yTrain = tf.tensor2d( trainingData.map(t => [t.type == "greeting" ? 1 : 0, t.type == "goodbye" ? 1 : 0, t.type == "insult" ? 1 : 0, t.type == "compliment" ? 1 : 0, t.type == "all" ? 1 : 0]) ) const model = tf.sequential(); model.add( tf.layers.dense({ inputShape: [xTrain.shape[1]], activation: "softmax", units: 5 }) ) model.compile({ loss: "categoricalCrossentropy", optimizer: tf.train.adam(0.001), metrics: ["accuracy"] }); const onBatchEnd = (batch, logs) => { console.log('Accuracy', logs.acc); } // Train Model await model.fit(xTrain, yTrain, { batchSize: 32, validationSplit: 0.1, shuffle: true, epochs: 300, callbacks: { onBatchEnd } }).then(info => { console.log('Final accuracy', info.history.acc); }); return model;
- Assistant
To get rid of yTrain, you can modify the code to directly map the training data to the output tensor instead of creating a separate yTrain tensor. Here's an example:
codevar xTrain = await sentenceEncoder.embed(sentences); const model = tf.sequential(); model.add( tf.layers.dense({ inputShape: [xTrain.shape[1]], activation: "softmax", units: 5 }) ) model.compile({ loss: "categoricalCrossentropy", optimizer: tf.train.adam(0.001), metrics: ["accuracy"] }); const trainingData = [ {input: "Hello", output: [1, 0, 0, 0, 1]}, // greeting and all {input: "Goodbye", output: [0, 1, 0, 0, 1]}, // goodbye and all ... ]; const onBatchEnd = (batch, logs) => { console.log('Accuracy', logs.acc); } // Train Model await model.fit(xTrain, trainingData.map(t => t.output), { batchSize: 32, validationSplit: 0.1, shuffle: true, epochs: 300, callbacks: { onBatchEnd } }).then(info => { console.log('Final accuracy', info.history.acc); }); return model; - User
как убрать разделение по категориям из yTrain?
- Assistant
If you want to remove the categorization from yTrain and have a single output for each input sentence, you can modify the code to have a single output unit in the output layer of the model. Here's an example:
code