Skip to content

Commit

Permalink
quick fix for checking if normalizationOptions exists
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Oct 2, 2019
1 parent 9d39ff9 commit 88af7ea
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/NeuralNetwork/NeuralNetworkData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class NeuralNetworkData {
constructor(options) {
this.config = options;
this.meta = {
// number of units - varies depending on input data type
// number of units - varies depending on input data type
inputUnits: null,
outputUnits: null,
// objects describing input/output data by property name
Expand All @@ -20,20 +20,23 @@ class NeuralNetworkData {
tensor: {
inputs: null, // tensor
outputs: null, // tensor
inputMax:null, // tensor
inputMin:null, // tensor
outputMax:null, // tensor
outputMin:null, // tensor
inputMax: null, // tensor
inputMin: null, // tensor
outputMax: null, // tensor
outputMin: null, // tensor
},
inputMax: options.dataOptions.normalizationOptions.inputMax || null, // array or number
inputMin: options.dataOptions.normalizationOptions.inputMin || null, // array or number
outputMax: options.dataOptions.normalizationOptions.outputMax || null, // array or number
outputMin: options.dataOptions.normalizationOptions.outputMin || null, // array or number
}

if (options.dataOptions.normalizationOptions) {
this.data.inputMax = options.dataOptions.normalizationOptions.inputMax || null; // array or number
this.data.inputMin = options.dataOptions.normalizationOptions.inputMin || null; // array or number
this.data.outputMax = options.dataOptions.normalizationOptions.outputMax || null; // array or number
this.data.outputMin = options.dataOptions.normalizationOptions.outputMin || null; // array or number
}
}

/**
* load data
* load data
*/
async loadData() {
const {
Expand All @@ -51,7 +54,7 @@ class NeuralNetworkData {
}

/**
* load csv
* load csv
* TODO: pass to loadJSONInternal()
*/
async loadCSVInternal() {
Expand All @@ -66,7 +69,7 @@ class NeuralNetworkData {

/**
* load json data
* @param {*} parsedJson
* @param {*} parsedJson
*/
async loadJSONInternal(parsedJson) {
const {
Expand Down Expand Up @@ -105,7 +108,7 @@ class NeuralNetworkData {
xs: {},
ys: {}
}
// TODO: keep an eye on the order of the
// TODO: keep an eye on the order of the
// property name order if you use the order
// later on in the code!
const props = Object.keys(item);
Expand Down Expand Up @@ -149,7 +152,7 @@ class NeuralNetworkData {

/**
* sets the data types of the data we're using
* important for handling oneHot
* important for handling oneHot
*/
setDTypes() {
// this.meta.inputs
Expand All @@ -173,7 +176,7 @@ class NeuralNetworkData {

/**
* Get the input and output units
*
*
*/
getIOUnits() {
let inputUnits = 0;
Expand All @@ -190,9 +193,9 @@ class NeuralNetworkData {
inputUnits += 1;
} else if (dtype === 'string') {
const uniqueVals = [...new Set(this.data.raw.map(obj => obj.xs[prop]))]
// Store the unqiue values
// Store the unqiue values
this.meta.inputs[prop].uniqueValues = uniqueVals;
const onehotValues = [...new Array(uniqueVals.length).fill(null).map( (item,idx) => idx)];
const onehotValues = [...new Array(uniqueVals.length).fill(null).map((item, idx) => idx)];

const oneHotEncodedValues = tf.oneHot(tf.tensor1d(onehotValues, 'int32'), uniqueVals.length);
const oneHotEncodedValuesArray = oneHotEncodedValues.arraySync();
Expand All @@ -218,9 +221,9 @@ class NeuralNetworkData {
outputUnits += 1;
} else if (dtype === 'string') {
const uniqueVals = [...new Set(this.data.raw.map(obj => obj.ys[prop]))]
// Store the unqiue values
// Store the unqiue values
this.meta.outputs[prop].uniqueValues = uniqueVals;
const onehotValues = [...new Array(uniqueVals.length).fill(null).map( (item,idx) => idx)];
const onehotValues = [...new Array(uniqueVals.length).fill(null).map((item, idx) => idx)];

const oneHotEncodedValues = tf.oneHot(tf.tensor1d(onehotValues, 'int32'), uniqueVals.length);
const oneHotEncodedValuesArray = oneHotEncodedValues.arraySync();
Expand Down Expand Up @@ -254,7 +257,7 @@ class NeuralNetworkData {

/**
* checks whether or not a string is a json
* @param {*} str
* @param {*} str
*/
// eslint-disable-next-line class-methods-use-this
isJsonString(str) {
Expand All @@ -269,7 +272,7 @@ class NeuralNetworkData {

/**
* Creates a csv from a strin
* @param {*} csv
* @param {*} csv
*/
// via: http://techslides.com/convert-csv-to-json-in-javascript
// eslint-disable-next-line class-methods-use-this
Expand Down Expand Up @@ -299,8 +302,8 @@ class NeuralNetworkData {

/**
* Takes data as an array
* @param {*} xArray
* @param {*} yArray
* @param {*} xArray
* @param {*} yArray
*/
addData(xArray, yArray) {
const inputs = {};
Expand Down Expand Up @@ -380,7 +383,7 @@ class NeuralNetworkData {
}

/**
*
*
*/
normalizeInternal(inputTensor, outputTensor) {
// inputTensor.print()
Expand All @@ -394,7 +397,7 @@ class NeuralNetworkData {
let outputMin;

if (this.config.architecture.task === 'regression') {
// if the task is a regression, return all the
// if the task is a regression, return all the
// output stats as an array
inputMax = inputTensor.max(0);
inputMin = inputTensor.min(0);
Expand All @@ -410,7 +413,7 @@ class NeuralNetworkData {

// TODO: refine this custom normalization function option
// Experimental!!!!
if(this.config.dataOptions.normalizationOptions instanceof Object){
if (this.config.dataOptions.normalizationOptions instanceof Object) {
inputMax = tf.tensor1d(this.data.inputMax);
inputMin = tf.tensor1d(this.data.inputMin);
// outputMax = tf.tensor1d(this.data.outputMax);
Expand All @@ -432,12 +435,12 @@ class NeuralNetworkData {
}

/**
* onehot encode values
* onehot encode values
*/
convertRawToTensor() {
console.log(this.meta)

// Given the inputs and output types,
// Given the inputs and output types,
// now create the input and output tensors
// 1. start by creating a matrix
const inputs = []
Expand Down Expand Up @@ -494,7 +497,7 @@ class NeuralNetworkData {
});

// console.log(inputs, outputs)
// 3. convert to tensors
// 3. convert to tensors
const inputTensor = tf.tensor(inputs, [this.data.raw.length, this.meta.inputUnits]);
const outputTensor = tf.tensor(outputs, [this.data.raw.length, this.meta.outputUnits]);

Expand Down

0 comments on commit 88af7ea

Please sign in to comment.