为什么Tensorflow JS模型不学习?
我已经用基于的tfjs构建了一个模型。在我移除第三个标签后,人工智能不再工作,它的损失和精度锁定在4.6和0.5。此外,在去除第三个标签后,训练后的预测也是错误的。我认为,对于这种类型的人工智能,可能损失函数、优化器或度量是错误的。为什么Tensorflow JS模型不学习?,tensorflow,machine-learning,tensorflow.js,Tensorflow,Machine Learning,Tensorflow.js,我已经用基于的tfjs构建了一个模型。在我移除第三个标签后,人工智能不再工作,它的损失和精度锁定在4.6和0.5。此外,在去除第三个标签后,训练后的预测也是错误的。我认为,对于这种类型的人工智能,可能损失函数、优化器或度量是错误的。 有什么建议吗?这个模型有什么问题吗 代码信息: 数据示例:[24,10,20,10,1](前四个数字代表服装,最后一个数字代表等级) 移除前的标签:[0=差评,1=好评,2=无意见] 移除后的标签:[0=不良评级,1=良好评级] 优化器:RMSProp
有什么建议吗?这个模型有什么问题吗
代码信息:
数据示例:[24,10,20,10,1]
(前四个数字代表服装,最后一个数字代表等级) 移除前的标签:[0=差评,1=好评,2=无意见]
移除后的标签:[0=不良评级,1=良好评级]
优化器:RMSProp
损失函数:分类交叉熵
指标:准确性
学习率:0.1
纪元:100
这套衣服改成了oneHot编码
InputShape:[null,4,27]
OutputShape:[null,2]
模型概要:
_________________________________________________________________
Layer (type) Output shape Param #
=================================================================
dense_Dense1 (Dense) [null,4,10] 280
_________________________________________________________________
flatten_Flatten1 (Flatten) [null,40] 0
_________________________________________________________________
dense_Dense2 (Dense) [null,2] 82
=================================================================
Total params: 362
Trainable params: 362
Non-trainable params: 0
_________________________________________________________________
删除后我的代码
const stringdata = document.getElementById('stringdata').innerHTML;
console.log(string(stringdata));
const IRIS_DATA = string(stringdata);
const kleidungo = document.getElementById('kleidungo').innerHTML;
const kleidungu = document.getElementById('kleidungu').innerHTML;
const Kleidung = [string(kleidungo), string(kleidungu)];
console.log(Kleidung);
import { string } from './js.js';
import * as tfVis from 'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis';
async function ki() {
const IRIS_CLASSES = ['Bad', 'Good'];
const IRIS_NUM_CLASSES = IRIS_CLASSES.length;
var epochsnum = 0;
console.log(tfVis); // [object Module] { ... }
function doPrediction(model, xTest, yTest) {
const labels = yTest.argMax([-1]);
const preds = model.predict(xTest).argMax([-1]);
xTest.dispose();
return [preds, labels];
}
async function calculateAndDrawConfusionMatrix(model, xTest, yTest) {
const [preds, labels] = doPrediction(model, xTest, yTest);
const confusionMatrix = await tfvis.metrics.confusionMatrix(labels, preds);
const container = document.getElementById('confusion-matrix');
tfvis.render.confusionMatrix(container, {
values: confusionMatrix,
tickLabels: IRIS_CLASSES
});
labels.dispose();
}
function convertToTensors(data, targets, testSplit) {
const numExamples = data.length;
if (numExamples !== targets.length) {
throw new Error('data and split have different numbers of examples');
}
// Randomly shuffle `data` and `targets`.
const indices = [];
for (let i = 0; i < numExamples; ++i) {
indices.push(i);
}
tf.util.shuffle(indices);
const shuffledData = [];
const shuffledTargets = [];
for (let i = 0; i < numExamples; ++i) {
shuffledData.push(data[indices[i]]);
shuffledTargets.push(targets[indices[i]]);
}
// Split the data into a training set and a tet set, based on `testSplit`.
const numTestExamples = Math.round(numExamples * testSplit);
const numTrainExamples = numExamples - numTestExamples;
const xDims = shuffledData[0].length;
// Create a `tf.Tensor` to hold the feature data.
//var dataByClass = tf.oneHot(dataByClass2, 27);
const xs = tf.oneHot(shuffledData, 27);
// Create a 1D `tf.Tensor` to hold the labels, and convert the number label
// from the set {0, 1, 2} into one-hot encoding (.e.g., 0 --> [1, 0, 0]).
const ys = tf.oneHot(tf.tensor1d(shuffledTargets).toInt(), IRIS_NUM_CLASSES);
// Split the data into training and test sets, using `slice`.
const xTrain = xs.slice([0, 0], [numTrainExamples, xDims]);
const xTest = xs.slice([numTrainExamples, 0], [numTestExamples, xDims]);
const yTrain = ys.slice([0, 0], [numTrainExamples, IRIS_NUM_CLASSES]);
const yTest = ys.slice([0, 0], [numTestExamples, IRIS_NUM_CLASSES]);
return [xTrain, yTrain, xTest, yTest];
}
function getIrisData(testSplit) {
return tf.tidy(() => {
const dataByClass = [];
const targetsByClass = [];
for (let i = 0; i < IRIS_CLASSES.length; ++i) {
dataByClass.push([]);
targetsByClass.push([]);
}
for (const example of IRIS_DATA) {
const target = example[example.length - 1];
const data = example.slice(0, example.length - 1);
dataByClass[target].push(data);
targetsByClass[target].push(target);
}
const xTrains = [];
const yTrains = [];
const xTests = [];
const yTests = [];
for (let i = 0; i < IRIS_CLASSES.length; ++i) {
const [xTrain, yTrain, xTest, yTest] =
convertToTensors(dataByClass[i], targetsByClass[i], testSplit);
xTrains.push(xTrain);
yTrains.push(yTrain);
xTests.push(xTest);
yTests.push(yTest);
}
const concatAxis = 0;
return [
tf.concat(xTrains, concatAxis), tf.concat(yTrains, concatAxis),
tf.concat(xTests, concatAxis), tf.concat(yTests, concatAxis)
];
});
}
function loadTrainParametersFromUI() {
return {
epochs: 100,
learningRate: 0.1
};
}
async function trainModel(xTrain, yTrain, xTest, yTest) {
document.getElementById('Epochs').innerHTML = 'Model is create...';
const params = loadTrainParametersFromUI();
console.log('Shape: ' + xTrain.shape + '\n Data:')
xTrain.print();
console.log('Shape: ' + yTrain.shape + '\n Data:')
yTrain.print();
// Define the topology of the model: two dense layers.
const model = tf.sequential();
model.add(tf.layers.dense(
{
units: 10,
activation: 'sigmoid',
inputShape: [
xTrain.shape[1],
xTrain.shape[2]
]
}
));
model.add(
tf.layers.flatten()
);
model.add(tf.layers.dense(
{
units: 2,
activation: 'softmax'
}
));
model.summary();
const opti = "rmsprop";
const optimizer = tf.train.rmsprop(params.learningRate);
model.compile(
{
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
}
);
const trainLogs = [];
const lossContainer = document.getElementById('lossCanvas');
const accContainer = document.getElementById('accuracyCanvas');
const beginMs = performance.now();
console.log('epochs: ' + params.epochs + ', lr: ' + params.learningRate + ', opti: ' + opti);
// Call `model.fit` to train the model.
const surface = {
name: 'show.fitCallbacks',
tab: 'Training'
};
//Train with callback after each epoh -> Diagramm
const history = await model.fit(xTrain, yTrain, {
epochs: params.epochs,
validationData: [xTest, yTest],
callbacks: {
onEpochEnd: async (epoch, logs) => {
// Plot the loss and accuracy values at the end of every training epoch.
const secPerEpoch = (performance.now() - beginMs) / (1000 * (epoch + 1));
trainLogs.push(logs);
epochsnum++;
document.getElementById('Epochs').innerHTML = (
'Epoche ' + epochsnum + "/" + params.epochs + '<br>Dauer der letzten Epoche: '+ secPerEpoch.toFixed(5) + 's <br> Verstrichene Zeit: ' + ((performance.now() - beginMs) / 1000).toFixed(5) + 's'
);
document.getElementById('Progressbar').innerHTML = (
'<progress id="progress" value="' + epochsnum + '" max="' + params.epochs + '"> ' + epochsnum + '% </progress>'
);
console.log(epochsnum + '. Epoche');
console.log('Epochendauer der Epoche ' + epochsnum + ': ' + secPerEpoch.toFixed(5));
console.log('Loss ', trainLogs[epochsnum-1].loss.toFixed(5));
console.log('Accuracy ', trainLogs[epochsnum-1].acc.toFixed(5));
tfvis.show.history(
lossContainer,
trainLogs,
['loss']
);
tfvis.show.history(
accContainer,
trainLogs,
['acc']
);
},
},
});
//Data Output
async function calculateAndDrawConfusionMatrix(model, xTest, yTest) {
const [preds, labels] = tf.tidy(() => {
const preds = model.predict(xTest).argMax(-1);
const labels = yTest.argMax(-1);
return [preds, labels];
});
const confMatrixData = await tfvis.metrics.confusionMatrix(labels, preds);
const container = document.getElementById('confusion-matrix');
tfvis.render.confusionMatrix(
container,
{
values: confMatrixData,
labels: IRIS_CLASSES
},
{
shadeDiagonal: true
},
);
tf.dispose([
preds,
labels
]);
}
calculateAndDrawConfusionMatrix(model, xTest, yTest);
const secPerEpoch = (performance.now() - beginMs) / (1000 * params.epochs);
console.log((performance.now() - beginMs) / 1000);
console.log(secPerEpoch);
document.getElementById('Epochs').innerHTML = '';
document.getElementById('Progressbar').innerHTML = '';
var time = ((performance.now() - beginMs) / 1000).toFixed(5);
document.getElementById('secperEpochs').innerHTML = (
'Durchschnittliche Dauer einer Epoche: ' + secPerEpoch.toFixed(5) + ' Sekunden <br> Gesamte Dauer: ' + time + 's'
);
//Predict Test
console.log('predict [2, 2, 14, 1]' + model.predict(
tf.oneHot(
[[2, 2, 14, 1]],
27
)
));
var predict = model.predict(
tf.oneHot(
[[2, 2, 14, 1]],
27
)
);
document.getElementById('Predict').innerHTML = predict;
console.log("===================================");
console.log('xTest ' + xTest);
console.log('Predict ' + model.predict(xTest));
console.log("===================================");
function TensortoArray(id) {
var predictneu = document.getElementById(id).innerHTML;
var predict2 = predictneu.replace("Tensor", "");
predict2 = predict2.split(",");
var predict2neu = predict2.pop();
var predict21 = predict2[0];
predict21 = predict21.replace("[", "");
predict21 = predict21.replace("[", "");
predict21 = predict21.replace("\n", "");
var predict2neu = predict2.shift();
var predict2neu = predict2.unshift(predict21);
var arraytensor = [];
predict2.forEach(element => {
var inserrrt = parseFloat(element);
arraytensor.push(inserrrt);
});
console.log('ArrayTensor ' + arraytensor);
var arrayten = [];
arraytensor.forEach(element => {
var a1 = arrayten.push(element.toFixed(5)*100);
});
return arrayten;
}
var arraytensor = TensortoArray('Predict');
if (arraytensor[0]>arraytensor[1]) {
console.log(IRIS_CLASSES[0])
document.getElementById('Predict').innerHTML = (
'The Testoutfit is rated ' + IRIS_CLASSES[0] + '. (It is to ' + arraytensor[0] + 'sure)'
);
} else {
console.log(IRIS_CLASSES[1])
document.getElementById('Predict').innerHTML = (
'The Testoutfit is rated ' + IRIS_CLASSES[1] + ' . (It is to ' + arraytensor[1] + 'sure)'
);
}
//Find new good Outfit
var wert = 0
var min = 1;
function random (max) {
var x = Math.round((Math.random() * (max - min)) + min);
return x;
}
var rundenwert = 1;
while (wert!=1) {
//Create new random Outfit
var x1 = random(Kleidung[0].length-1);
var y1 = random(Kleidung[1].length-1);
var x2 = random(Kleidung[0].length-1);
var y2 = random(Kleidung[1].length-1);
var array = [];
var array2 = [];
var array_x1 = array.push(Kleidung[0][x1][0]);
var array_y1 = array.push(Kleidung[1][y1][0]);
var array_x2 = array.push(Kleidung[0][x2][0]);
var array_y2 = array.push(Kleidung[1][y2][0]);
var druck = array[0] + ', ' + array[1] + ', ' + array[2] + ', ' + array[3];
array.forEach(element => {
array2.push(parseFloat(element));
});
console.log('Runde ' + rundenwert);
document.getElementById("Array Outfit").innerHTML = 'Finales Outfit Array: ' + druck;
document.getElementById("1o").innerHTML = '1. Top -' + Kleidung[0][x1][1];
document.getElementById("1u").innerHTML = '1. Lower part -' + Kleidung[1][y1][1];
document.getElementById("2o").innerHTML = '2. Top -' + Kleidung[0][x2][1];
document.getElementById("2u").innerHTML = '2. Lower part -' + Kleidung[1][y2][1];
console.log(druck);
console.log(array2);
//Rate the generated Outfit
console.log(model.predict(tf.oneHot([array2], 27)));
document.getElementById('Predict Outfit').innerHTML = model.predict(tf.oneHot([array2], 27));
var predicttensor = TensortoArray('Predict Outfit');
if (predicttensor[0]>predicttensor[1]) {
console.log(IRIS_CLASSES[0])
document.getElementById('Predict Outfit').innerHTML = 'The final outfit is rated ' + IRIS_CLASSES[0] + '. (It is to ' + predicttensor[0] + 'sure)';
} else if (predicttensor[1]>60) {
console.log(IRIS_CLASSES[1])
document.getElementById('Predict Outfit').innerHTML = 'The final outfit is rated ' + IRIS_CLASSES[1] + ' bewertet. (It is to ' + predicttensor[1] + 'sure)';
wert = 1;
}
document.getElementById('Sicherheit Predict').innerHTML = '<progress id="progress" value="' + predicttensor[0] + '" max="100"></progress><label> Schlecht: ' + predicttensor[0] + '%</label><br><progress id="progress" value="' + predicttensor[1] + '" max="100"></progress><label> Gut: ' + predicttensor[1] + '%</label>';
rundenwert++;
document.getElementById('Rundenzahl').innerHTML = 'It was ' + rundenwert + ' Rounds used to find a good outfit.';
//If no Outfit is find -> stop
if (rundenwert>500) {
document.getElementById("Rundenzahl").innerHTML = 'That took to long. <br> No good Outfit was found.';
wert = 1;
}
}
//Predict Test2
console.log('test2 ' + model.predict(tf.oneHot([[1,4,3,10]], 27)));
console.log('test ' + model.predict(tf.oneHot([[3,4,8,13]], 27)));
//Model Save
await model.save('http:...')
return model;
}
const stringdata=document.getElementById('stringdata').innerHTML;
log(字符串(stringdata));
常量IRIS_数据=字符串(stringdata);
const kleidungo=document.getElementById('kleidungo').innerHTML;
const kleidungu=document.getElementById('kleidungu').innerHTML;
常量Kleidung=[字符串(kleidungo),字符串(kleidungu)];
控制台日志(Kleidung);
从'/js.js'导入{string};
将*作为tfVis从导入'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis';
异步函数ki(){
常量IRIS_CLASSES=[“坏”、“好”];
常量IRIS_NUM_CLASSES=IRIS_CLASSES.length;
var-epochnum=0;
console.log(tfVis);//[对象模块]{…}
功能预测(型号、xTest、yTest){
常量标签=yTest.argMax([-1]);
const preds=model.predict(xTest).argMax([-1]);
xTest.dispose();
返回[preds,labels];
}
异步函数CalculateAndRawConfusionMatrix(模型、xTest、yTest){
const[preds,labels]=dopredition(model,xTest,yTest);
const confusionMatrix=等待tfvis.metrics.confusionMatrix(标签、pred);
const container=document.getElementById('consummation-matrix');
tfvis.render.confusionMatrix(容器{
值:confusionMatrix,
标签:IRIS_类
});
labels.dispose();
}
函数转换器传感器(数据、目标、测试拆分){
const numExamples=data.length;
if(numExamples!==targets.length){
抛出新错误(“数据和拆分有不同数量的示例”);
}
//随机洗牌'data'和'targets'。
常数指数=[];
for(设i=0;i[1,0,0])。
const ys=tf.oneHot(tf.tensor1d(shuffledTargets.toInt(),IRIS\u NUM\u类);
//使用“切片”将数据拆分为训练集和测试集。
const xTrain=xs.slice([0,0],[numTrainExamples,xDims]);
const xTest=xs.slice([numTrainExamples,0],[numTestExamples,xDims]);
const yTrain=ys.slice([0,0],[numtrain示例,IRIS_NUM_类]);
常量yTest=ys.slice([0,0],[numTestExamples,IRIS_NUM_CLASSES]);
返回[xTrain,yTrain,xTest,yTest];
}
函数getIrisData(testSplit){
返回tf.tidy(()=>{
常量数据周期=[];
常量targetsByClass=[];
for(设i=0;i