Deeplearning:尝试向掩码数组添加值时出错

时间:2016-03-14 06:42:39

标签: java arrays machine-learning deep-learning

在深度学习中,当我们具有不同长度的特征时,需要填充和屏蔽。我正在开发一种使用RNN算法的情绪分析程序。 在我的程序中,我试图为我创建的掩码数组添加值。但它会出现以下错误。

java.lang.IllegalArgumentException: Indexes, shape, and stride must be the same length
    at org.nd4j.linalg.api.shape.Shape.getOffset(Shape.java:480)
    at org.nd4j.linalg.api.ndarray.BaseNDArray.putScalar(BaseNDArray.java:1071)
    at MovieReviewClassifierModel.predict(MovieReviewClassifierModel.java:80)
    at org.apache.spark.ml.PredictionModel$$anonfun$1.apply(Predictor.scala:182)
    at org.apache.spark.ml.PredictionModel$$anonfun$1.apply(Predictor.scala:181)
    at org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2.apply(ScalaUDF.scala:75)
    at org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2.apply(ScalaUDF.scala:74)
    at org.apache.spark.sql.catalyst.expressions.ScalaUDF.eval(ScalaUDF.scala:964)
    at org.apache.spark.sql.catalyst.expressions.GeneratedClass$SpecificMutableProjection.apply(Unknown Source)
    at org.apache.spark.sql.execution.Project$$anonfun$1$$anonfun$apply$2.apply(basicOperators.scala:55)
    at org.apache.spark.sql.execution.Project$$anonfun$1$$anonfun$apply$2.apply(basicOperators.scala:53)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
    at scala.collection.Iterator$$anon$14.hasNext(Iterator.scala:389)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:327)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:308)
    at scala.collection.Iterator$class.foreach(Iterator.scala:727)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
    at scala.collection.AbstractIterator.to(Iterator.scala:1157)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:265)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1157)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:252)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1157)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:215)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$5.apply(SparkPlan.scala:215)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
    at org.apache.spark.scheduler.Task.run(Task.scala:89)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我试图通过使用以下for循环向掩码数组添加值。你能解释一下错误的原因以及我如何解决它。

int lengthOfFeatureArray=featureArray.length;
        INDArray arr=Nd4j.create(featureArray);
        INDArray featureMaskArray=Nd4j.zeros(maxLength);
        int[] temp = new int[1];
        for(int i=0;i<lengthOfFeatureArray;i++){
            temp[0]=i;
            featureMaskArray.putScalar(temp,1.0);
        }

1 个答案:

答案 0 :(得分:0)

看看featureMaskArray.rank()。这很可能是2,因此

int[] temp = new int[] { 1, 1 }
featureMaskArray.putScalar(temp, 1.0);

可能有效

但是

int[] temp = new int[] { 1 }
featureMaskArray.putScalar(temp, 1.0);

不会。