Search code examples
machine-learningconv-neural-networkconvolutiontensorflow.jsdanfojs

Could not feed my convolution1d with csv data


I need a help for my following problem. I'm trying to feed my csv data to my first layer which is convolution1d but it shows

Input 0 is incompatible with layer conv1d_Conv1D1: expected ndim=3, found ndim=2

Here is my code

//move the tfjs_binding.node file in build-tmp-napi-v7/Release folder to build-tmp-napi-v7 folder will solve the problem.

const dfd = require("danfojs-node");
const tf = require("@tensorflow/tfjs-node");

var petData;
const TIME_STEPS = (24 * 60) / 60;

console.log("start");

var model = tf.sequential();
model.add(
  tf.layers.conv1d({
    filters: 3,
    kernelSize: 3,
    inputShape:[1]
  })
);
// model.add(tf.layers.dropout({ rate: 0.2 }));
// model.add(
//   tf.layers.conv1d({
//     filters: 16,
//     kernelSize: 7,
//     padding: "same",
//     strides: 2,
//     activation: "relu",
//   })
// );
// model.add(
//   tf.layers.conv1d({
//     filters: 16,
//     kernelSize: 7,
//     padding: "same",
//     strides: 2,
//     activation: "relu",
//   })
// );
// model.add(tf.layers.dropout({ rate: 0.2 }));
// model.add(
//   tf.layers.conv1d({
//     filters: 32,
//     kernelSize: 7,
//     padding: "same",
//     strides: 2,
//     activation: "relu",
//   })
// );
// model.add(
//   tf.layers.conv1d({
//     filters: 1,
//     kernelSize: 7,
//     padding: "same",
//   })
// );
model.compile({
  optimizer: tf.train.adam((learningRate = 0.001)),
  loss: tf.losses.meanSquaredError,
});
model.summary();
console.log("model created.");

dfd
  .read_csv("./petTempData.csv", (chunk = 10000))
  .then((df) => {
    let encoder = new dfd.LabelEncoder();
    let cols = ["Date", "Time"];
    cols.forEach((col) => {
      encoder.fit(df[col]);
      enc_val = encoder.transform(df[col]);
      df.addColumn({ column: col, value: enc_val });
    });

    petData = df.iloc({ columns: [`1`] });
    yData = df["Temperature"];

    // let scaler = new dfd.MinMaxScaler();
    // scaler.fit(petData);
    // petData = scaler.transform(petData);
    // petData = petData.tensor.expandDims(-1);
    // const data = petData.tensor.reshape([24, 2, 1]);
    console.log(petData.shape);

    model.fit(petData.tensor, yData.tensor, {
      epochs: 10,
      batchSize: 4,
      // validationSplit: 0.01,
      callbacks: tf.callbacks.earlyStopping({
        monitor: "loss",
        patience: "5",
        mode: "min",
      }),
    });
  })
  .catch((err) => {
    console.log(err);
  });

And here is my csv raw file

Date,Time,Temperature
31-12-2020,01:30,36.6
31-12-2020,02:30,36.7
31-12-2020,03:30,36.6
31-12-2020,04:30,36.5
31-12-2020,05:30,36.8
31-12-2020,06:30,36.6
31-12-2020,07:30,36.6
31-12-2020,08:30,36.5
31-12-2020,09:30,36.6
31-12-2020,10:30,36.7
31-12-2020,11:30,36.6
31-12-2020,12:30,36.7
31-12-2020,13:30,36.7
31-12-2020,14:30,36.8
31-12-2020,15:30,36.9
31-12-2020,16:30,36.6
31-12-2020,17:30,36.7
31-12-2020,18:30,36.8
31-12-2020,19:30,36.7
31-12-2020,20:30,36.6
31-12-2020,21:30,36.6
31-12-2020,22:30,36.5
31-12-2020,23:30,36.5
,,

I've tried to reshape my input, and expandDims but none of them work. Any solution is much appreciated!


Solution

  • The conv1d layer expects an inputShape of dim 2, therefore, the inputShape needs to be [a, b](with a, b positive integers).

    model = tf.sequential();
    model.add(
      tf.layers.conv1d({
        filters: 3,
        kernelSize: 1,
        inputShape:[1, 3]
      })
    );
    
    model.predict(tf.ones([1, 1, 3])).print()