// Read the data from the given path
var data = new CSVLoader<>(new RegressionFactory()).loadDataSource(Paths.get("C:\Users\20187\Desktop\regression_data.csv"), "Output");
// Divide it into training and testing parts
var splitter = new TrainTestSplitter<>(data, 0.6, Trainer.DEFAULT_SEED);
var trainPart = new MutableDataset<>(splitter.getTrain());
var testPart = new MutableDataset<>(splitter.getTest());
// Construct the NN graph using Tribuo and Tensorflow
var nnGraph = new Graph();
var operationObject = Ops.create(nnGraph); // this object is used
var inputName = "g_input";
var numberOfFeatures = trainPart.getFeatureMap().size();
var nnInitializer = new Glorot<TFloat32>(/*Initializer distribution*/ VarianceScaling.DISTRIBUTION_DEFAULT,
/*Initializer seed*/ Trainer.DEFAULT_SEED);
// The input layer that we will feed our input features into (no calculation is conducted in this layer)
var inputLayer = operationObject.withName(inputName).placeholder(TFloat32.class, Placeholder.shape(Shape.of(-1, numberOfFeatures)));
// The output layer (20 -> 1) that contains 20 units
var outputWeights = operationObject.variable(nnInitializer.call(operationObject,operationObject.array(20L, 1L), TFloat32.class));
var outputBiases = operationObject.variable(operationObject.fill(operationObject.array(1), operationObject.constant(0.1f)));
var sigmoid = operationObject.math.sigmoid(operationObject.math.add(operationObject.linalg.matMul(inputLayer, outputWeights), outputBiases));
var outputLayer = operationObject.math.add(operationObject.linalg.matMul(sigmoid, outputWeights), outputBiases);
// This name will be passed to our trainer
var outputName = outputLayer.op().name();
// Use Adam optimizer as our optimization algorithm
var optimizer = GradientOptimiser.GRADIENT_DESCENT;
var optimizerParameters = Map.of("learningRate",0.1f,"initialAccumulatorValue",0.01f);
// Converting Features into Tensors with FeatureConverter
var denseFeaturesConverter = new DenseFeatureConverter(inputName);
// Converting Outputs into Tensors (and back again) with OutputConverter
var outputConverter = new RegressorConverter();
// Now we want to construct our tensorflow trainer
var TF_Trainer = new TensorFlowTrainer<Regressor>(nnGraph,
outputName,
optimizer,
optimizerParameters,
denseFeaturesConverter,
outputConverter,
16, /*training batch size*/
20, /*number of epochs*/
16, /*test batch size*/
-1); /*disable logging of the loss vale*/
// Now we close the original graph to free the associated native resources.
// The TensorFlowTrainer keeps a copy of the GraphDef protobuf to rebuild it when necessary.
// This process is suitable for memory management
nnGraph.close();
// Train the model
var trainedModel = TF_Trainer.train(trainPart);
// Evaluate the model
var evaluator = new RegressionEvaluator().evaluate(trainedModel, testPart);
System.out.println(evaluator.rmse());
And I get this exception when I run the file:
2023-10-03 00:06:27.953705: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Exception in thread "main" org.tensorflow.exceptions.TFInvalidArgumentException: Dimensions must be equal, but are 1 and 20 for '{{node MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](g_input, Variable)' with input shapes: [?,1], [20,1].
at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:87)
at org.tensorflow.GraphOperationBuilder.finish(GraphOperationBuilder.java:459)
at org.tensorflow.GraphOperationBuilder.build(GraphOperationBuilder.java:100)
at org.tensorflow.GraphOperationBuilder.build(GraphOperationBuilder.java:71)
at org.tensorflow.op.linalg.MatMul.create(MatMul.java:96)
at org.tensorflow.op.LinalgOps.matMul(LinalgOps.java:692)
at Main.org.MainNN.main(MainNN.java:50)
I aimed to buld a regression model as follows:
// Read the data from the given path var data = new CSVLoader<>(new RegressionFactory()).loadDataSource(Paths.get("C:\Users\20187\Desktop\regression_data.csv"), "Output");
And I get this exception when I run the file:
2023-10-03 00:06:27.953705: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. Exception in thread "main" org.tensorflow.exceptions.TFInvalidArgumentException: Dimensions must be equal, but are 1 and 20 for '{{node MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](g_input, Variable)' with input shapes: [?,1], [20,1]. at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:87) at org.tensorflow.GraphOperationBuilder.finish(GraphOperationBuilder.java:459) at org.tensorflow.GraphOperationBuilder.build(GraphOperationBuilder.java:100) at org.tensorflow.GraphOperationBuilder.build(GraphOperationBuilder.java:71) at org.tensorflow.op.linalg.MatMul.create(MatMul.java:96) at org.tensorflow.op.LinalgOps.matMul(LinalgOps.java:692) at Main.org.MainNN.main(MainNN.java:50)
Can you help me to fix theis complication?!