Concentration of CO2 of Crickets at Specific Temperatures AI
In this AI project I used the data I had from the Thesis statement I wrote about the respiration rate of crickets at different temperatures. In 9th grade I researched how the respiration rate of crickets is influenced by the temperature as they are cold blooded animals (this research was also mentioned in the Temperature and CO, CO2, and Combustable Gasses Measuring Device - Arduino Project). I had a lot of data from this experiment (3020 datapoint to be exact) and I used it to learn about saving the models that are already trained.Some background information about the experiment is that I always used 10 crickets of roughly the same size which weighed ~3.2g (so ~0.32g/cricket). The data I gathered in the experiment can be found in this file:
// Add tensorflowjs, prompt, csv, and fs functions to the library
const tf = require('@tensorflow/tfjs');
const prompt = require('prompt');
const csv = require('csv-parser');
const fs = require('fs');
prompt.start();
require('@tensorflow/tfjs-node');
var inputs = [];
var outputs = [];
// Saves the model that was trained
async function asyncCallSave(model) {
const saveResult = await model.save('file://./CricketModel');
}
// Sets up the train function
train = function(inputs, outputs) {
let model;
// Set the created data as variables to be input into the model
const xs = tf.tensor(inputs);
const ys = tf.tensor(outputs);
// Setting up the model that will be used for the data processing
(async () => {
try {
// If there is a trained model that is present in the file directory it loads that file that that model was on
model = await tf.loadLayersModel('file://./CricketModel/model.json');
} catch (e) {
// Setup of the single model with 2 input nodes, 10 processing nodes, and 1 output node
model = tf.sequential();
model.add(tf.layers.dense({units: 10, activation: 'relu', inputShape: [2]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
}
model.compile({optimizer: 'adam', loss: 'meanSquaredError', lr: 0.0001});
})().then(() => {
// Train the model over 500 epochs (this number is interchangable and stacks with each time you run the model)
model.fit(xs, ys, {
epochs: 500
}).then(info => {
// Saves the model that was trained
asyncCallSave(model);
// Ask for user input for the temperature and the time, and display the results of running the model on the data
prompt.get(['temperature', 'time'], function (err, result) {
if (err) {
return onErr(err);
}
console.log('Results : ' + model.predict(tf.tensor([[parseInt(result.temperature), parseInt(result.time)]])));
});
});
});
}
// Read the data from the .csv file and record it into arrays for the input and output
fs.createReadStream('CricketsData.csv')
.pipe(csv())
.on('data', (row) => {
inputs[inputs.length] = [];
outputs[outputs.length] = [];
inputs[inputs.length-1][0] = parseInt(row['timecolumn']);
inputs[inputs.length-1][1] = parseFloat(row['temperature']);
outputs[outputs.length-1][0] = parseInt(row['CO2']);
})
.on('end', () => {
// when the input and output arrays populate with the data the function that trains the Network begins
train(inputs, outputs);
});
The output for the core looks something like this:
// Last epoch of the training cycle
376ms 124us/step - loss=1289567.13
Epoch 500 / 500
378ms 125us/step - loss=1291194.88
// user prompts
prompt: temperature: 10
prompt: time: 400
Results : Tensor
//Result of predicted CO2 in PPM (Parts per Million)
[[994.387085],]
Komentáre
Zverejnenie komentára