Created
March 31, 2020 14:38
-
-
Save Rishit-dagli/d3d316a3b4e1fdd62eb391da6128980e to your computer and use it in GitHub Desktop.
Data preperation for car dataset TF.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert the input data to tensors that we can use for machine | |
* learning. We will also do the important best practices of _shuffling_ | |
* the data and _normalizing_ the data | |
* MPG on the y-axis. | |
*/ | |
function convertToTensor(data) { | |
// Wrapping these calculations in a tidy will dispose any | |
// intermediate tensors. | |
return tf.tidy(() => { | |
// Step 1. Shuffle the data | |
tf.util.shuffle(data); | |
// Step 2. Convert data to Tensor | |
const inputs = data.map(d => d.horsepower) | |
const labels = data.map(d => d.mpg); | |
const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]); | |
const labelTensor = tf.tensor2d(labels, [labels.length, 1]); | |
//Step 3. Normalize the data to the range 0 - 1 using min-max scaling | |
const inputMax = inputTensor.max(); | |
const inputMin = inputTensor.min(); | |
const labelMax = labelTensor.max(); | |
const labelMin = labelTensor.min(); | |
const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin)); | |
const normalizedLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin)); | |
return { | |
inputs: normalizedInputs, | |
labels: normalizedLabels, | |
// Return the min/max bounds so we can use them later. | |
inputMax, | |
inputMin, | |
labelMax, | |
labelMin, | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment