* Train a model with dataset, then save the model to a local folder.
(epochs, batchSize, savePath)
| 31 | * Train a model with dataset, then save the model to a local folder. |
| 32 | */ |
| 33 | async function run(epochs, batchSize, savePath) { |
| 34 | const datasetObj = await createDataset('file://' + csvPath); |
| 35 | const model = createModel([datasetObj.numOfColumns]); |
| 36 | // The dataset has 4177 rows. Split them into 2 groups, one for training and |
| 37 | // one for validation. Take about 3500 rows as train dataset, and the rest as |
| 38 | // validation dataset. |
| 39 | const trainBatches = Math.floor(3500 / batchSize); |
| 40 | const dataset = datasetObj.dataset.shuffle(1000).batch(batchSize); |
| 41 | const trainDataset = dataset.take(trainBatches); |
| 42 | const validationDataset = dataset.skip(trainBatches); |
| 43 | |
| 44 | await model.fitDataset( |
| 45 | trainDataset, {epochs: epochs, validationData: validationDataset}); |
| 46 | |
| 47 | await model.save(savePath); |
| 48 | |
| 49 | const loadedModel = await tf.loadLayersModel(savePath + '/model.json'); |
| 50 | const result = loadedModel.predict( |
| 51 | tf.tensor2d([[0, 0.625, 0.495, 0.165, 1.262, 0.507, 0.318, 0.39]])); |
| 52 | console.log( |
| 53 | 'The actual test abalone age is 10, the inference result from the model is ' + |
| 54 | result.dataSync()); |
| 55 | } |
| 56 | |
| 57 | const parser = new argparse.ArgumentParser( |
| 58 | {description: 'TensorFlow.js-Node Abalone Example.', addHelp: true}); |
no test coverage detected