Closed juliangamble closed 1 year ago
Did this specifically happened at this computation?
NDArray y = X.dot(w).add(b);
Have you tried the matMul as suggested in the error message?
I think i spot one possble error that causes dimension mismatch when doing X.dot(w)
// Generate y = X w + b + noise
public static DataPoints syntheticData(NDManager manager, NDArray w, float b, int numExamples) {
NDArray X = manager.randomNormal(new Shape(numExamples, w.size()));
NDArray y = X.dot(w).add(b);
//java.lang.UnsupportedOperationException: Dimension mismatch or high dimensional dot operation is not supported. Please use .matMul instead.
// Add noise
y = y.add(manager.randomNormal(0, 0.01f, y.getShape(), DataType.FLOAT32));
return new DataPoints(X, y);
}
Here, in NDArray X = manager.randomNormal(new Shape(numExamples, w.size())); Shape(numExamples, w.size()) -> Shape(numExamples, w.getShape().get(0)); Then the dimension would match in that X.dot(w) computation.
hi @KexinFeng,
Thanks for your response.
Did this specifically happened at this computation?
Yes.
Have you tried the matMul as suggested in the error message?
There is not enough information for me to go ahead with this. But what I'm using is based on the textbook here: https://d2l.djl.ai/chapter_linear-networks/linear-regression-scratch.html#generating-the-dataset So I'm interested in the reason to do it differently.
I think i spot one possble error that causes dimension mismatch when doing
X.dot(w)
Here, inNDArray X = manager.randomNormal(new Shape(numExamples, w.size()));
Shape(numExamples, w.size())
->Shape(numExamples, w.getShape().get(0));
Then the dimension would match in thatX.dot(w)
computation.
Here is what I changed it to:
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.index.NDIndex;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import tech.tablesaw.api.FloatColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.plotly.Plot;
import tech.tablesaw.plotly.api.ScatterPlot;
import tech.tablesaw.plotly.components.Figure;
public class Exercise3_2_LinearRegressionFromScratch {
public static void main(String[] args) {
NDManager manager = NDManager.newBaseManager();
NDArray trueW = manager.create(new float[]{2, -3.4f});
float trueB = 4.2f;
DataPoints dp = syntheticData(manager, trueW, trueB, 1000);
NDArray features = dp.getX();
NDArray labels = dp.getY();
System.out.printf("features: [%f, %f]\n", features.get(0).getFloat(0), features.get(0).getFloat(1));
System.out.println("label: " + labels.getFloat(0));
float[] X = features.get(new NDIndex(":, 1")).toFloatArray();
float[] y = labels.toFloatArray();
Table data = Table.create("Data")
.addColumns(
FloatColumn.create("X", X),
FloatColumn.create("y", y)
);
Figure figure = ScatterPlot.create("Synthetic Data", data, "X", "y");
Plot.show(figure);
}
// Generate y = X w + b + noise
public static DataPoints syntheticData(NDManager manager, NDArray w, float b, int numExamples) {
//NDArray X = manager.randomNormal(new Shape(numExamples, w.size()));
NDArray X = manager.randomNormal(new Shape(numExamples, w.getShape().get(0)));
NDArray y = X.dot(w).add(b);
//java.lang.UnsupportedOperationException: Dimension mismatch or high dimensional dot operation is not supported. Please use .matMul instead.
// Add noise
y = y.add(manager.randomNormal(0, 0.01f, y.getShape(), DataType.FLOAT32));
return new DataPoints(X, y);
}
}
It still fails with the error:
[main] INFO ai.djl.pytorch.engine.PtEngine - PyTorch graph executor optimizer is enabled, this may impact your inference latency and throughput. See: https://docs.djl.ai/docs/development/inference_performance_optimization.html#graph-executor-optimization
[main] INFO ai.djl.pytorch.engine.PtEngine - Number of inter-op threads is 8
[main] INFO ai.djl.pytorch.engine.PtEngine - Number of intra-op threads is 8
Exception in thread "main" java.lang.UnsupportedOperationException: Dimension mismatch or high dimensional dot operation is not supported. Please use .matMul instead.
at ai.djl.pytorch.engine.PtNDArray.dot(PtNDArray.java:1353)
at ai.djl.pytorch.engine.PtNDArray.dot(PtNDArray.java:39)
at org.example.Exercise3_2_LinearRegressionFromScratch.syntheticData(Exercise3_2_LinearRegressionFromScratch.java:46)
at org.example.Exercise3_2_LinearRegressionFromScratch.main(Exercise3_2_LinearRegressionFromScratch.java:22)
Process finished with exit code 1
Have you got a suggestion?
The reason of changing it is to make sure that in y = X w + b
, where X of shape [s1, s2], w of shape [s3, s4], s2 be the same as s3. This is due to the rule of matrix multiplication.
I found the error. The dot operation only applies on 1D vectors. Please do further change of changing .dot to .matMul
Thankyou - that fixed it.
Description
Running Deep Java Learning Exercise 3.2 on an M1 Mac in Java 11 with Pytorch leads to the Exception:
java.lang.UnsupportedOperationException: Dimension mismatch or high dimensional dot operation is not supported. Please use .matMul instead.
Expected Behavior
The code completes and shows a scatterplot of Synthetic Data.
Error Message
How to Reproduce?
(If you developed your own code, please provide a short script that reproduces the error. For existing examples, please provide link.)
Maven
pom.xml
Java class
Exercise3_2_LinearRegressionFromScratch
Steps to reproduce
(Paste the commands you ran that produced the error.)
export DJL_DEFAULT_ENGINE=PyTorch
(via IntelliJ run configuration)Exercise3_2_LinearRegressionFromScratch
What have you tried to solve it?
Implement Linear Regression From Scratch
https://d2l.djl.ai/chapter_linear-networks/linear-regression-scratch.htmlEnvironment Info
Please run the command
./gradlew debugEnv
from the root directory of DJL (if necessary, clone DJL first). It will output information about your system, environment, and installation that can help us debug your issue. Paste the output of the command below:Via Intellij configuration.