我的模型不学习。它应该在最后进行softmax计算。我希望得到一个分类结果(退出或不退出)。模型应该预测客户是否会退出。我提供了退出列作为标签,并有196个输入特征。
我的可视化工具显示完全没有学习。但我并不确定,可视化工具如何获取模型是否学习的信息。我对JavaScript很新手,任何帮助都将不胜感激。
ngOnInit() { this.train();}async train(): Promise<any> { const csvUrl = '/assets/little.csv'; const csvDataset = tf.data.csv( csvUrl, { columnConfigs: { quit: { isLabel: true } }, delimiter:',' }); const numOfFeatures = (await csvDataset.columnNames()).length -1; console.log(numOfFeatures); const flattenedDataset = csvDataset .map(({xs, ys}: any) => { // Convert xs(features) and ys(labels) from object form (keyed by // column name) to array form. return {xs:Object.values(xs), ys:Object.values(ys)}; }).batch(10); console.log(flattenedDataset.toArray()); const model = tf.sequential({ layers: [ tf.layers.dense({inputShape: [196], units: 100, activation: 'relu'}), tf.layers.dense({units: 100, activation: 'relu'}), tf.layers.dense({units: 100, activation: 'relu'}), tf.layers.dense({units: 1, activation: 'softmax'}), ] }); await trainModel(model, flattenedDataset); const surface = { name: 'Model Summary', tab: 'Model Inspection'}; tfvis.show.modelSummary(surface, model); console.log('Done Training');}async function trainModel(model, flattenedDataset) { // Prepare the model for training. model.compile({ optimizer: tf.train.adam(), loss: tf.losses.sigmoidCrossEntropy, metrics: ['accuracy'] }); const batchSize = 32; const epochs = 50; return await model.fitDataset(flattenedDataset, { batchSize, epochs, shuffle: true, callbacks: tfvis.show.fitCallbacks( { name: 'Training Performance' }, ['loss'], { height: 200, callbacks: ['onEpochEnd'] } ) });}
回答:
最后一层的单元数量应等于类别的数量。这里有两个类别quit
和no-quit
。另外,你的标签应该进行独热编码。关于模型为何不学习的更普遍的答案可以在这里找到这里。