Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* @desc creates array of input data for every sample
* @param json data - complete json that contains wine quality data
* @return array of input data
*/
function extractInputs(data)
{
let inputs = []
inputs = data.map(d => [d.fixed_acidity, d.volatile_acidity, d.citric_acid, d.residual_sugar, d.chlorides, d.free_sulfur_dioxide, d.total_sulfur_dioxide, d.density, d.pH, d.sulphates, d.alcohol])
return inputs;
}
/**
* @desc converts data from json format to tensors
* @param json data - complete json that contains wine quality data
* @return tuple of converted data that can be used for training model
*/
function prepareDataFunction(data) {
return tf.tidy(() => {
tf.util.shuffle(data);
const inputs = extractInputs(data);
const outputs = data.map(d => d.quality);
const inputTensor = tf.tensor2d(inputs, [inputs.length, inputs[0].length]);
const outputTensor = tf.oneHot(tf.tensor1d(outputs, 'int32'), 10);
const inputMax = inputTensor.max();
const inputMin = inputTensor.min();
const outputMax = outputTensor.max();
const outputMin = outputTensor.min();
const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
const normalizedoutputs = outputTensor.sub(outputMin).div(outputMax.sub(outputMin));
return {
inputs: normalizedInputs,
outputs: normalizedoutputs,
inputMax,
inputMin,
outputMax,
outputMin,
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment