dl4j - Unable to get number of of rows for a non 2d matrix - nd4j

I am current using nd4j and dl4j to implement some deep learning algorithm. However, I can not get datavec + dl4j working in the first place.
Here is my image converter:
public class ImageConverter {
private static Logger log = LoggerFactory.getLogger(ImageConverter.class);
public DataSetIterator Convert() throws IOException, InterruptedException {
log.info("Start to convert images...");
File parentDir = new File(System.getProperty("user.dir"), "src/main/resources/images/");
ParentPathLabelGenerator parentPathLabelGenerator = new ParentPathLabelGenerator();
ImageRecordReader recordReader = new ImageRecordReader(28,28,1,parentPathLabelGenerator);
FileSplit fs = new FileSplit(parentDir);
InputSplit[] filesInDirSplit = fs.sample(null, 100);
recordReader.initialize(filesInDirSplit[0]);
DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, 2, 1, 2);
log.info("Image convert finished.");
return dataIter;
}
}
Here is the main class:
ImageConverter icv = new ImageConverter();
DataSetIterator dataSetIterator = icv.Convert();
log.info("Build model....");
int numEpochs = 10;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(123)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.iterations(1)
.learningRate(0.006)
.updater(Updater.NESTEROVS).momentum(0.9)
.regularization(true).l2(1e-4)
.list()
.layer(0, new ConvolutionLayer.Builder(5, 5)
.nIn(28 * 28)
.stride(1, 1)
.nOut(20)
.activation("identity")
.build())
.layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
.nIn(24 * 24)
.nOut(2)
.activation("softmax")
.build())
.pretrain(false)
.backprop(true)
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(1));
log.info("Train model....");
for( int i=0; i<numEpochs; i++ ){
model.fit(dataSetIterator);
}
In the image folders, I have some grey scale 28x28 images located in sub-folder a,b respectively.
However, Exception in thread "main" java.lang.IllegalStateException: Unable to get number of of rows for a non 2d matrix is thrown.
Looking into the data by dataSetIterator.next().toString(), it is something like:
[[[...],
...
]]
=================OUTPUT==================
[[1.00, 0.00],
[1.00, 0.00]]
Moreover, the output of dataSetIterator.next().get(0).toString() is
[[[[...],
...
]]]
=================OUTPUT==================
[1.00, 0.00]
And for the mnisterIterator in the examples, mnisterIterator.next().toString() should be something like:
[[...]...]
=================OUTPUT==================
[[0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
...]
From which I infer that the dataSetIterator I returned contained data in wrong format.
Anybody knows how to fix it?

We've implemented this for you already in our examples.
4d and 2d don't matter to us, just specify the convolution layer setup:
https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/LenetMnistExample.java#L84
If you face anymore trouble, where you think a discussion would be easier, come on our gitter:
https://gitter.im/deeplearning4j/deeplearning4j
Thanks!

Your dataset is 4d matrix, but MNist require 2d matrix https://github.com/deeplearning4j/deeplearning4j/issues/2010

Related

How can I access the mesh data of a MeshInstance?

I'm trying, without success, to access the data of a mesh from a MeshInstance node.
I've imported a 3d object, opened it as "New Inherited", turned it as "Unique" and saved it as foo.mesh. Then, on a new scene, I did create a MeshInstance and loaded the foo.mesh as its Mesh.
The Script is attached to the very MeshInstance, kinda like follows:
extends MeshInstance
func _ready():
var themesh = Mesh
var mdt = MeshDataTool.new()
if mdt.create_from_surface(themesh, 0):
print("Ok!!")
print(mdt.get_vertex_count()) # get_vertex_count() returns 0
else:
print("Failed...")
It doesn't work for built in meshes. Needs to be an imported mesh, that I also save as a Godot .mesh.
Reference links: Facebook, Mesh Data Tool, Mesh Class
I was mistaking pointing to Mesh class instead of mesh attribute to get the mesh reference. And the if test needs to check pass, because "create_from_surface()" returns a non zero when an error occours.
extends MeshInstance
func _ready():
var themesh = mesh # Same as bellow, points to same object in memory
var themesh2 = self.get_mesh() # Same as above, points to same object in memory
print("Mesh surface count: " + str(themesh.get_surface_count()))
var mdt = MeshDataTool.new()
if mdt.create_from_surface(themesh, 0) == OK: # Check pass
print("Ok!!")
print(mdt.get_vertex_count())
else:
print("Fail...")
var aMeshVerts = []
for i in range(mdt.get_vertex_count()):
aMeshVerts.append(mdt.get_vertex(i)) # Storing the vertices positions
mdt.set_vertex(0, Vector3(1, 2, 1)) # Changing a vertice position
themesh.surface_remove(0)
mdt.commit_to_surface(themesh)
mdt.clear()

PaddingFIFOQueue.enqueue_many throwing Value Error: Shapes must be equal rank

I have an RNN model, to which I'm trying to feed in varying length input sequences through input pipelines randomly sampling from multiple TF record files containing serialized SequenceExamples, batch padding and shuffling across multiple batches
each sequence example has 3 elements, as length: constant, input:1-D array, labels:1-D array
Procedure is as follows
def read_file_queue(self,filename_queue):
reader = tf.TFRecordReader()
key, ex = reader.read(filename_queue)
context_features = {
"seq-len": tf.FixedLenFeature([],dtype=tf.int64)
}
sequence_features = {
"tokens": tf.FixedLenSequenceFeature([],dtype=tf.int64),
"labels": tf.FixedLenSequenceFeature([],dtype=tf.int64)
}
context_parsed, sequence_parsed = tf.parse_single_sequence_example(serialized=ex,
context_features=context_features,
sequence_features=sequence_features)
return context_parsed["seq-len"], sequence_parsed["tokens"],sequence_parsed["labels"]
def get_batch_data(self):
fqueue = tf.train.string_input_producer(self.data_filelist,
shuffle=True,
num_epochs=self.num_epochs)
# read from multiple tf records as defined by read_threads
ex = [self.read_file_fmt(fqueue) for _ in range(self.read_threads)]
print(ex)
# ex = self.read_file_fmt(fqueue)
pad_output = self.padding_pipeline(ex)
shuffle_output = self.shuffle_pipeline(pad_output)
return shuffle_output
def padding_pipeline(self, input):
padding_queue = tf.PaddingFIFOQueue(
capacity=self.pad_capacity,
dtypes=[tf.int64, tf.int64, tf.int64],
shapes=[[], [None], [None]])
# use enqueue_many instead enqueue because
# the input is list of tuples from each tf record reader thread
padding_enqueue_op = padding_queue.enqueue_many(input) # <<< !!!!! error here !!!!!
padding_queue_runner = tf.train.QueueRunner(padding_queue, [padding_enqueue_op] * self.pad_threads)
tf.train.add_queue_runner(padding_queue_runner)
padding_dequeue_op = padding_queue.dequeue_up_to(self.batch_size)
return padding_dequeue_op
def shuffle_pipeline(self,input):
shuffle_queue = tf.RandomShuffleQueue(
capacity=self.shuffle_capacity,
min_after_dequeue=self.shuffle_min_after_dequeue,
dtypes=[tf.int64, tf.int64, tf.int64],
shapes=None)
shuffle_enqueue_op = shuffle_queue.enqueue(input)
shuffle_queue_runner = tf.train.QueueRunner(
shuffle_queue, [shuffle_enqueue_op] * self.shuffle_threads)
tf.train.add_queue_runner(shuffle_queue_runner)
shuffle_dequeue_op = shuffle_queue.dequeue()
return shuffle_dequeue_op
For which I'm getting the following error:
ValueError: Shapes must be equal rank, but are 0 and 1 From merging
shape 0 with other shapes. for
'padding_fifo_queue_EnqueueMany/component_0' (op: 'Pack') with input
shapes: [], [?], [?].
I'm sure I'm doing something silly here, however, I could not find what is that im doing wrong..
Taking a hint from here, maybe you should have the following?
padding_queue = tf.PaddingFIFOQueue(
capacity=self.pad_capacity,
dtypes=[tf.int64, tf.int64, tf.int64],
shapes=[None, [None], [None]])
By the way, if you could add some basic script for generating random data in the format you are using, it would be easier to replicate. Thanks.

Pachinko Modeling in Mallet

I am experimenting with the Pachinko topic model in Mallet, and am having trouble getting it working. When it prints out the topics at each update, they are all the same. This occurs when I use both the default alpha and beta values, and when I use my own. I haven't been able to find that much written about Mallet's Pachinko model, and their documentation is pretty sparse. Here is my code with my parameters:
PAM4L model = new PAM4L(superTopics: 25, subTopics: 5);
String output = new String("output");
Randoms rand = new Randoms(4);
model.estimate(InstanceList: instances, iterations: 500,
optimizeInterval: 5,showTopicsInterval: 50,
outputModelInterval: 50, outputFile: output, r: rand);
Thank you

SharpDX: Creating TextureCube from set of images

So I have 6 seperate images and I would like to build a textureCube out of them. What's the best way to go about it? Right now this is what I have, but I'm getting a memory access violation when the TextureCube tries to create itself.
SharpDX.DataBox[] textureData = new SharpDX.DataBox[6];
for(int i = 0; i < 6; i++)
{
Texture2D tex = Texture2D.Load(device, resources[i].Filepath);
SharpDX.Direct3D11.Texture2D staged = (SharpDX.Direct3D11.Texture2D)tex.ToStaging();
textureData[i] = new SharpDX.DataBox(staged.NativePointer, staged.Description.Width, 0);
}
SharpDX.Toolkit.Graphics.TextureCube cube = SharpDX.Toolkit.Graphics.TextureCube.New(
device,
2048,
PixelFormat.R8G8B8A8.UNorm,
textureData);
The texture load is working fine. I can load the texture and then create a textureCube from a single image no problem. That's not much use though. When I try to create a cube using the raw data from my 6 separate images I'm getting the memory exception.
Result Message:
Test method ResourceLoadingTests.LoadCubemapAndSave threw exception:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Result StackTrace:
at SharpDX.Direct3D11.Device.CreateTexture2D(Texture2DDescription& descRef, DataBox[] initialDataRef, Texture2D texture2DOut)
at SharpDX.Direct3D11.Texture2D..ctor(Device device, Texture2DDescription description, DataBox[] data)
at SharpDX.Toolkit.Graphics.Texture2DBase..ctor(GraphicsDevice device, Texture2DDescription description2D, DataBox[] dataBoxes)
at SharpDX.Toolkit.Graphics.TextureCube..ctor(GraphicsDevice device, Texture2DDescription description2D, DataBox[] dataBoxes)
at SharpDX.Toolkit.Graphics.TextureCube.New(GraphicsDevice device, Int32 size, PixelFormat format, DataBox[] textureData, TextureFlags flags, ResourceUsage usage)

HyperNEAT network for Time Series forecasting with Encog help needed

I am using Encog AI Framework for Time Series forecasting using HyperNEAT network.
Here is the simple code I use to create the network.
Substrate substrate = SubstrateFactory.factorSandwichSubstrate(columns*windowSize,days);
CalculateScore score = new TrainingSetScore(trainingSet);
NEATPopulation pop = new NEATPopulation(substrate, 500);
pop.setActivationCycles(4);
pop.reset();
EvolutionaryAlgorithm train = NEATUtil.constructNEATTrainer(pop, score);
OriginalNEATSpeciation speciation = new OriginalNEATSpeciation();
speciation.setCompatibilityThreshold(1);
train.setSpeciation(speciation = new OriginalNEATSpeciation());
System.out.println("Is HyperNEAT "+pop.isHyperNEAT());
// train the neural network
int epoch = 1;
do {
train.iteration();
if(writeOnStdOut)
System.out.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
if(Math.abs(train.getError()-previousError)<0.000000001) iterationWithoutImprovement++; else iterationWithoutImprovement = 0;
previousError = train.getError();
Date dtemp = new Date();
} while(train.getError() > maximumAcceptedErrorTreshold && epoch < maxIterations && iterationWithoutImprovement < maxiter);
NEATGenome genome = (NEATGenome) pop.getBestGenome();
HyperNEATCODEC codec = new HyperNEATCODEC();
network2 = (NEATNetwork) codec.decode(pop, substrate, genome);
It was taken from Box exampel https://github.com/encog/encog-java-examples/tree/master/src/main/java/org/encog/examples/neural/neat/boxes
Where columns is the number of the features and windowSize is the number of previous days needed to forecast the future value (in my example windowSize is 1).
I get this exception:
Exception in thread "pool-2-thread-416" java.lang.ArrayIndexOutOfBoundsException
at org.encog.util.EngineArray.arrayCopy(EngineArray.java:107)
at org.encog.neural.neat.NEATNetwork.compute(NEATNetwork.java:194)
at org.encog.util.error.CalculateRegressionError.calculateError(CalculateRegressionError.java:46)
at org.encog.neural.networks.training.TrainingSetScore.calculateScore(TrainingSetScore.java:61)
at org.encog.ml.ea.score.parallel.ParallelScoreTask.run(ParallelScoreTask.java:83)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
It seems that there are problems handling threads.
Can someone help me solving this problem?
My second question is: how can I train a NEAT network with Backpropagation in Encog?
As to the array out of bounds exception. I looked at that line and the only thing that can cause it is that that you are sending in an input vector that has more elements than you have input neurons for. I would make sure that you are defining the neural network to be of the same input dimensions as your data ultimately ends up being.
As to backpropagation and NEAT/HyperNEAT, that is not how these networks are designed to be trained. At least not the Kenneth Stanley implementations work. It is all genetic training. There might be a way to fine tune a NEAT network with backprop, but I have not attempted it.
I have declared this
Substrate substrate = SubstrateFactory.factorSandwichSubstrate((int)Math.sqrt(NDataSetFeatures),1);
where the last parameter is the Class, and this works for me.

Resources