Skip to content

Commit a972e67

Browse files
committed
repo init
1 parent 2a7ff32 commit a972e67

14 files changed

+25564
-0
lines changed

1DGAN/.babelrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"esmodules": false,
7+
"targets": {
8+
"browsers": [
9+
"> 3%"
10+
]
11+
}
12+
}
13+
]
14+
],
15+
"plugins": [
16+
"@babel/plugin-transform-runtime"
17+
]
18+
}

1DGAN/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TensorFlow.js Example: Multivariate Regression for browser
2+
3+
This example shows you how to perform regression with more than one input feature using [Boston Housing Dataset](https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html) which is a famous dataset derived from information collected by the U.S. Census Service concerning housing in the area of Boston Massachusetts.
4+
5+
Prepare the environment:
6+
```sh
7+
$ npm install
8+
# Or
9+
$ yarn
10+
```
11+
12+
To build and watch the example, run:
13+
```sh
14+
$ yarn watch
15+
```

1DGAN/data.js

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
const Papa = require('papaparse');
19+
20+
21+
// Boston Housing data constants:
22+
const BASE_URL =
23+
'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/';
24+
25+
const TRAIN_FEATURES_FN = 'train-data.csv';
26+
const TRAIN_TARGET_FN = 'train-target.csv';
27+
const TEST_FEATURES_FN = 'test-data.csv';
28+
const TEST_TARGET_FN = 'test-target.csv';
29+
30+
/**
31+
* Given CSV data returns an array of arrays of numbers.
32+
*
33+
* @param {Array<Object>} data Downloaded data.
34+
*
35+
* @returns {Promise.Array<number[]>} Resolves to data with values parsed as floats.
36+
*/
37+
const parseCsv = async (data) => {
38+
return new Promise(resolve => {
39+
data = data.map((row) => {
40+
return Object.keys(row).map(key => parseFloat(row[key]));
41+
});
42+
resolve(data);
43+
});
44+
};
45+
46+
/**
47+
* Downloads and returns the csv.
48+
*
49+
* @param {string} filename Name of file to be loaded.
50+
*
51+
* @returns {Promise.Array<number[]>} Resolves to parsed csv data.
52+
*/
53+
export const loadCsv = async (filename) => {
54+
return new Promise(resolve => {
55+
const url = `${BASE_URL}${filename}`;
56+
57+
console.log(` * Downloading data from: ${url}`);
58+
Papa.parse(url, {
59+
download: true,
60+
header: true,
61+
complete: (results) => {
62+
resolve(parseCsv(results['data']));
63+
}
64+
})
65+
});
66+
};
67+
68+
/** Helper class to handle loading training and test data. */
69+
export class BostonHousingDataset {
70+
constructor() {
71+
// Arrays to hold the data.
72+
this.trainFeatures = null;
73+
this.trainTarget = null;
74+
this.testFeatures = null;
75+
this.testTarget = null;
76+
}
77+
78+
get numFeatures() {
79+
// If numFetures is accessed before the data is loaded, raise an error.
80+
if (this.trainFeatures == null) {
81+
throw new Error('\'loadData()\' must be called before numFeatures')
82+
}
83+
return this.trainFeatures[0].length;
84+
}
85+
86+
/** Loads training and test data. */
87+
async loadData() {
88+
[this.trainFeatures, this.trainTarget, this.testFeatures, this.testTarget] =
89+
await Promise.all([
90+
loadCsv(TRAIN_FEATURES_FN), loadCsv(TRAIN_TARGET_FN),
91+
loadCsv(TEST_FEATURES_FN), loadCsv(TEST_TARGET_FN)
92+
]);
93+
94+
shuffle(this.trainFeatures, this.trainTarget);
95+
shuffle(this.testFeatures, this.testTarget);
96+
}
97+
}
98+
99+
export const featureDescriptions = [
100+
'Crime rate', 'Land zone size', 'Industrial proportion', 'Next to river',
101+
'Nitric oxide concentration', 'Number of rooms per house', 'Age of housing',
102+
'Distance to commute', 'Distance to highway', 'Tax rate', 'School class size',
103+
'School drop-out rate'
104+
];
105+
106+
/**
107+
* Shuffles data and target (maintaining alignment) using Fisher-Yates
108+
* algorithm.flab
109+
*/
110+
function shuffle(data, target) {
111+
let counter = data.length;
112+
let temp = 0;
113+
let index = 0;
114+
while (counter > 0) {
115+
index = (Math.random() * counter) | 0;
116+
counter--;
117+
// data:
118+
temp = data[counter];
119+
data[counter] = data[index];
120+
data[index] = temp;
121+
// target:
122+
temp = target[counter];
123+
target[counter] = target[index];
124+
target[index] = temp;
125+
}
126+
};

1DGAN/index.html

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!--
2+
Copyright 2018 Google LLC. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================
16+
-->
17+
18+
<html>
19+
20+
<head>
21+
<meta charset="UTF-8">
22+
<meta name="viewport" content="width=device-width, initial-scale=1">
23+
<link rel="stylesheet" href="../shared/tfjs-examples.css" />
24+
25+
<style>
26+
.negativeWeight {
27+
color: #cc0000;
28+
}
29+
30+
.positiveWeight {
31+
color: #00aa00;
32+
}
33+
34+
#buttons {
35+
margin-top: 20px;
36+
padding: 5px;
37+
}
38+
39+
#oneHidden {
40+
border-left: 1px solid #ededed;
41+
border-right: 1px solid #ededed;
42+
}
43+
44+
#linear,
45+
#oneHidden,
46+
#twoHidden {
47+
padding: 5px;
48+
}
49+
</style>
50+
</head>
51+
52+
<body>
53+
<div class='tfjs-example-container centered-container'>
54+
<section class='title-area'>
55+
<h1>Multivariate Regression</h1>
56+
<p class='subtitle'>Compare different models for housing price prediction.</p>
57+
</section>
58+
59+
<section>
60+
<p class='section-head'>Description</p>
61+
<p>
62+
This example shows you how to perform regression with more than one input feature using the
63+
<a href="https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html">Boston Housing Dataset</a>,
64+
which is a famous dataset derived from information collected by the U.S. Census Service concerning housing
65+
in the area of Boston Massachusetts.
66+
</p>
67+
<p>
68+
It allows you to compare the performance of 3 different models for predicting the house prices. When training
69+
the linear model, it will also display the largest 5 weights (by absolute value) of the model and
70+
the feature associated with each of those weights.
71+
</p>
72+
</section>
73+
74+
<section>
75+
<p class='section-head'>Status</p>
76+
<p id="status">Loading data...</p>
77+
<p id="baselineStatus">Baseline not computed...</p>
78+
</section>
79+
80+
<section>
81+
<p class='section-head'>Training Progress</p>
82+
<div class="with-cols">
83+
<div id="linear">
84+
<div class="chart"></div>
85+
<div class="status"></div>
86+
<div id="modelInspectionOutput">
87+
<p id="inspectionHeadline"></p>
88+
<table id="myTable"></table>
89+
</div>
90+
</div>
91+
<div id="oneHidden">
92+
<div class="chart"></div>
93+
<div class="status"></div>
94+
</div>
95+
<div id="twoHidden">
96+
<div class="chart"></div>
97+
<div class="status"></div>
98+
</div>
99+
</div>
100+
101+
<div id="buttons">
102+
<div class="with-cols">
103+
<button id="simple-mlr">Train Linear Regressor</button>
104+
<button id="nn-mlr-1hidden">Train Neural Network Regressor (1 hidden layer)</button>
105+
<button id="nn-mlr-2hidden">Train Neural Network Regressor (2 hidden layers)</button>
106+
</div>
107+
</div>
108+
109+
</section>
110+
</div>
111+
<script src="index.js"></script>
112+
</body>
113+
114+
</html>

0 commit comments

Comments
 (0)