Category Archives: apache-spark-ml

Spark & Scala MultilayerPerceptronClassifier – java.lang.NegativeArraySizeException

I am running to a "NegativeArraySizeException" error when I try to train a MultilayerPerceptronClassifier model in Spark Scala. I am following the example found here. Below is my code - I am aware that the layers typically go from large to small and that they are in reverse here - this is experimental but I have found nothing in the docs that explicitly prevent it.

// Data in
var df= spark.read.option("delimiter", "|").option("inferSchema", "true").format("csv").load("hdfs://master-node:9000//example.csv")

// Split the data into train and test
val splits = df.randomSplit(Array(0.9, 0.1))
val train = splits(0)
val test = splits(1)

// Set input dimensions
val input_dim = 3072
val output_dim = 198000

// Specify layers
val layers = Array[Int](input_dim, 24777, 49555, output_dim)

// create the trainer and set its parameters
val trainer = new MultilayerPerceptronClassifier().setLayers(layers).setBlockSize(128).setSeed(1234L).setMaxIter(100).setFeaturesCol("features").setLabelCol("label")

// train the model
val model = trainer.fit(train)

The error is thrown upon "trainer.fit(train)." The data is is the same format as the example where labels = Double and data = Vector.

Here is the error, any familiarly with it?

scala> val model = trainer.fit(train)
java.lang.NegativeArraySizeException
  at scala.reflect.ManifestFactory$$anon$12.newArray(Manifest.scala:141)
  at scala.reflect.ManifestFactory$$anon$12.newArray(Manifest.scala:139)
  at breeze.linalg.DenseVector$.zeros$mDc$sp(DenseVector.scala:254)
  at org.apache.spark.ml.ann.FeedForwardModel$.apply(Layer.scala:564)
  at org.apache.spark.ml.ann.FeedForwardTopology.model(Layer.scala:397)
  at org.apache.spark.ml.ann.FeedForwardTrainer.train(Layer.scala:807)
  at org.apache.spark.ml.classification.MultilayerPerceptronClassifier.train(MultilayerPerceptronClassifier.scala:260)
  at org.apache.spark.ml.classification.MultilayerPerceptronClassifier.train(MultilayerPerceptronClassifier.scala:145)
  at org.apache.spark.ml.Predictor.fit(Predictor.scala:96)
  ... 52 elided