How to use LSTM with a 2d array with DeepLearning4j - deep-learning

i am trying to learn how to use LSTM with deeplearning4j lib.
I created a dummy scenario where i want to get an output (3 classes) based on data that i collected.
I got the data from here (http://www.osservatoriodioropa.it/meteoropa/NOAAMO.TXT) if someone is curious :)
Back to the scenario.
I created 2 matrix, one with features, other with classes that i want to output, just as a test.
When i try the classifier i got
Exception in thread "main" java.lang.IllegalStateException: 3D input expected to RNN layer expected, got 2
i think because the RnnOutputLayer expect a 3d matrix, but i am not able to understand how to populate it. How can i convert a 2d matrix into a 3d matrix correlating the previous event with the new one? The data are a time serie, and i want to relate the classification of the new day based on previous days as well. (I know that probably the data won't fit this scenario and that there are better way to do that, but that's just to learning how to use LSTM, not how to classify this specific dataset)
this is the code so far
public class Test {
public static void main(String args[]) {
int events = 5;
int features = 6;
int classes = 3;
double[][] featureMatrix = new double[events][features];
double[][] labelMatrix = new double[events][classes];
for (int i = 0; i < events; i++) {
for (int f = 0; f < features; f++) {
featureMatrix[i][f] = getFeature(i, f);
}
for (int c = 0; c < classes; c++) {
labelMatrix[i][c] = getResult(i, c);
}
}
INDArray trainingIn = Nd4j.create(featureMatrix);
INDArray trainingOut = Nd4j.create(labelMatrix);
DataSet myData = new DataSet(trainingIn, trainingOut);
MultiLayerNetwork multiLayerNetwork = createModel(features,classes);
multiLayerNetwork.init();
multiLayerNetwork.fit(myData);
}
private static double getFeature(int i, int f) {
//dummy
return 1.;
}
private static double getResult(int i, int c) {
//dummy
return 1.;
}
public static MultiLayerNetwork createModel(int inputNum, int outputNum) {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.trainingWorkspaceMode(ENABLED).inferenceWorkspaceMode(ENABLED)
.seed(123456)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new RmsProp.Builder().learningRate(0.05).rmsDecay(0.002).build())
.l2(0.0005)
.weightInit(WeightInit.XAVIER)
.activation(Activation.TANH)
.list()
.layer(new LSTM.Builder().name("1").nIn(inputNum).nOut(inputNum).build())
.layer(new LSTM.Builder().name("2").nIn(inputNum).nOut(inputNum).build())
.layer(new RnnOutputLayer.Builder().name("output").nIn(inputNum).nOut(outputNum)
.activation(Activation.IDENTITY).lossFunction(LossFunctions.LossFunction.MSE).build())
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
return net;
}
}

Related

element.length not working when using generics

My problem is this. I am passing arguments to a function that uses generics. For example I am passing element. I need to use element.length in the function but it gives me this error:
The property 'length' can't be unconditionally accessed because the receiver can be 'null'.
Try making the access conditional (using '?.') or adding a null check to the target ('!'). dartunchecked_use_of_nullable_value
void main(List<String> args) {
const List keyList = ['asd', 3, 'tyu', 67];
const List valueList = ['dfg', 'ert', 4, 'lkj'];
print(myMap(keyList, valueList));
}
Map myMap<K, V>(K key, V val) {
final Map<K, V> myMap = {};
for (int i = 0; i < key.length; i++) {
myMap[key[i]] = val[i];
}
return myMap;
}
I don't know if null aware operators can be used here, or if they solve the problem and how to use it.
You defined your generic types, but your parameters are lists of that type. You did not make this clear to your compiler. For your compiler, K is now of type List<dynamic> while what you wanted it to be is just dynamic.
Changing it to make it fit (my guess of) your requirements:
Map<K, V> myMap<K, V>(List<K> keys, List<V> values) {
final result = <K, V>{};
for (int i = 0; i < keys.length; i++) {
result[keys[i]] = values[i];
}
return result;
}
Please note that you are reinventing the wheel here. There already is a constructor that does this:
void main(List<String> args) {
const List keyList = ['asd', 3, 'tyu', 67];
const List valueList = ['dfg', 'ert', 4, 'lkj'];
print(Map.fromIterables(keyList, valueList));
}

Outputting data to JSON file csharp (unity)

I can't figure out how to output a complex data type to JSON.
I constructed a data type which basically holds smaller data types, I have also assigned the data types to new data types so they all seem to have a reference. I have looked into outputting complex data but don't seem to be able to find a problem similar to mine. I will consider appending data but this method will be much simpler if I can output the data type successfully.
Save Data Code
[System.Serializable]
public class SaveData
{
public MapData mapData;
}
[System.Serializable]
public class TileData
{
public List<BlockData> blockData;
}
[System.Serializable]
public class BlockData
{
public Vector3 blockPosition;
public string blockName;
public float blockOrientation;
public int blockLayer;
}
[System.Serializable]
public class MapData
{
public List<TileData> tileData;
}
Get Map Data Method
SaveData GetMapData()
{
mapHeight += mapStartY;
maplength += mapStartX;
int tileCounter = 0;
MapData mapData = new MapData();
SaveData saveData = new SaveData();
List<TileData> tileList = new List<TileData>();
for (float r = mapStartY; r < mapHeight; r++)
{
for(float c = mapStartX; c < maplength; c++)
{
Vector2 currentPosition = new Vector2(c * (blocksize)-(blocksize/2), blocksize * r -(blocksize/2));
GameObject[] currentTile = getObjectID.RayDetectAll(currentPosition);
if (currentTile!= null)
{
//adds a tiledata list here if the tile is occupied.
TileData tileData = new TileData();
//adds a list of blocks here.
List<BlockData> blocks = new List<BlockData>();
for (int i = 0; i < currentTile.Length; i++)
{
BlockData blockData = new BlockData();
GameObject currentBlock = currentTile[i];
blockData.blockPosition = currentBlock.transform.position;
blockData.blockName = currentBlock.name;
blockData.blockOrientation = currentBlock.transform.eulerAngles.z;
blockData.blockLayer = currentBlock.GetComponent<SpriteRenderer>().sortingOrder;
//adds a blockdata to the blocks list
blocks.Add(blockData);
Debug.LogWarning(blockData.blockName);
}
//need to assign tile data and add a new one to the list
tileList.Add(tileData);
//assins the blocks to tile data block data list
tileData.blockData = blocks;
}
else
{
//Debug.LogWarning("warning! no objects found on tile: " + currentPosition);
}
tileCounter++;
}
}
'''
I want the file to output all the data so that i can read the data and reassign it. Right now it outputs the data wrong.
Generally I think this is something that would be commented, but I can't comment yet.
If all you want is to convert an object to Json, could you use JsonUtility.ToJson() as described here?
just to let people know I devised a new method which counted an array of all tiles and assigned it to a data type with an array in it. It managed to load from this format.

Brick Breaker in Unity w json data input

I'm making a brick breaker game in Unity2D, but was told to make the bricks dynamic instead of dragging in cubes from the UI. The idea is to make the brick patterns dynamic, and receive brick data (i.e. positions) from a .json file. I've taken the approach of populating using Instantiate and a prefab, although I'm not sure how to incorporate that with data from .json or if I'm on the right track. Newbie here please help! Even just directing me to concepts would be fantastic.
using System.Collections.Generic;
using UnityEngine;
public class BrickGrid : MonoBehaviour
{
public Transform brickPrefab;
private Vector3[] BrickCoords;
private int totalBricks;
void Start()
{
totalBricks = 4;
BrickCoords = new Vector3[totalBricks];
BrickCoords[0] = new Vector3(-2.2f, 2f, 0f);
BrickCoords[1] = new Vector3(-0.75f, 2f, 0f);
BrickCoords[2] = new Vector3(0.75f, 2f, 0f);
BrickCoords[3] = new Vector3(2.2f, 2f, 0f);
for (int x = 0; x < totalBricks; x++)
{
Instantiate(brickPrefab, BrickCoords[x], Quaternion.identity);
}
}
public static void initGrid(){
}
}
[System.Serializable]
public class BrickCoordinate
{
public Vector3 brickPos;
}
It depends on how your JsonString looks. But something along these lines might be what you need:
Make a class containing a list of blocks.
[System.Serializable]
public class Bricks
{
public List<BrickCoordinate> BrickCoordinates;
}
Then load in your json from wherever you have it, and pass it into Unity's JsonUtility class.
var listOfBlocks = JsonUtility.FromJson<Bricks>(yourJsonStringHere);
And then you can iterate over your list and instantiate each item.
You can view more info here
Edit:
An easy way to get a correct Json string, is to do the exact opposite.
Use the Json function in your code to test and serialize a list of blocks.
(Also i changed the name of the list, because it was misleading)
var test = new Bricks
{
BrickCoordinates = new List<BrickCoordinate>
{
new BrickCoordinate
{
brickpos = new Vector3{ x = 1, y = 2, z = 3};
},
new BrickCoordinate
{
brickpos = new Vector3{ x = 2, y = 2, z = 3};
},
new BrickCoordinate
{
brickpos = new Vector3{ x = 1, y = 4, z = 3};
},
}
};
var jsonString = JsonUtility.ToJson(test);
Debug.Log(jsonString);

How to adress elements of a recursive function?

Take a recursive function, say:
public static long rekurs(long n) {
if (n == 0) {
return 1;
} else if (n == 1) {
return 1;
} else {
return rekurs(n - 1)*(rekurs(n - 2)+4;
}
}
When n=20, the function has to find all the values S(n) for n=2,...,19 first.
When I let n go from 20 to 21, it does the same thing again (plus finding S(20)).
Now I want to create an array, in which the found values S(n) for n=2,...,19 are filled into, so that the function for n=21 does not have to do the same thing again, but how do I get those elements?
This is the solution I figured out, it's a little bit different from the lecture example.
The keyword that helped me is "dynamic programming".
import java.util.Arrays;
public class Bsp13 {
public static final int N = 0;
public static final int Ende = 20;
public static long[] schroe = new long[N + Ende + 1];
public static void main(String[] args) {
schroe[0] = 1;
schroe[1] = 1;
for (int n = 2; n <= Ende + N; n++) {
schroe[n] = ((6 * n - 3) * (schroe[n-1]) - (n - 2) * (schroe[n-2])) / (n + 1);
}
System.out.println(schroe[N]);
System.out.println(Arrays.toString(schroe));
System.out.println(schroe[N+Ende]);
}
}
What you are trying to do is called dynamic programming. Basically it is bookkeeping in order to not compute subsolutions more than once.
So basically, you need a mapping of n values to solution values. I would suggest you use a dictionary-like-datastructure for this task.
When a value for n needs to be computed, you first check whether the solution is in the dictionary, if yes you return the result. If not, you compute the result and put it into the dictionary.
Think about how you would initialize this dictionary and how you would pass it down to the recursive function calls.
Here's a lecture video on dynamic programming where the computation of Fibonnaci-numbers using dynamic programming is explained, which is very similar to what you are trying to do:
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-19-dynamic-programming-i-fibonacci-shortest-paths/

rootbeer CUDA example code quantified throughput gain

The following is the rootbeer example code for Nvidia CUDA that I ran on a laptop with Ubuntu 12.04 (Precise) with bumblebee and optirun. The laptop features Nvidia Optimus, hence the optirun. The GPU happens to be a Nvidia GeForce GT 540M which the Nvidia website says has 96 cores. I get almost no throughput gain. What is the problem?
package com.random.test;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import edu.syr.pcpratts.rootbeer.runtime.Kernel;
import edu.syr.pcpratts.rootbeer.runtime.Rootbeer;
public class ArraySumApp {
final static int numberOfJobs = 1024; // 1024 in the original example
final static int sizeOfArray = 512; // 512 in the original example
final static int theAnswer = 130816;
public int[] sumArrays(List<int[]> arrays) {
List<Kernel> jobs = new ArrayList<Kernel>();
int[] ret = new int[arrays.size()];
for (int i = 0; i < arrays.size(); ++i) {
jobs.add(new ArraySum(arrays.get(i), ret, i));
}
Rootbeer rootbeer = new Rootbeer();
rootbeer.runAll(jobs);
return ret;
}
private static long measureOneJob() {
int[] source = new int[ArraySumApp.sizeOfArray];
int[] destination = new int[1];
for (int i = 0; i < ArraySumApp.sizeOfArray; i++)
source[i] = i;
Kernel job = new ArraySum(source, destination, 0);
ElapsedTimer et = new ElapsedTimer();
job.gpuMethod();
long timeInMs = et.stopInMilliseconds();
System.out.println("measureOneJob " + et.stringInMilliseconds());
assert destination[0] == ArraySumApp.theAnswer : "cosmic rays";
return timeInMs;
}
public static void main(String[] args) {
Helper.assertAssertionEnabled();
// measure the time to do one job
ArraySumApp.measureOneJob();
long oneJob = ArraySumApp.measureOneJob();
ArraySumApp app = new ArraySumApp();
List<int[]> arrays = new ArrayList<int[]>();
// you want 1000s of threads to run on the GPU all at once for speedups
for (int i = 0; i < ArraySumApp.numberOfJobs; ++i) {
int[] array = new int[ArraySumApp.sizeOfArray];
for (int j = 0; j < array.length; ++j) {
array[j] = j;
}
arrays.add(array);
}
ElapsedTimer et = new ElapsedTimer();
int[] sums = app.sumArrays(arrays);
long allJobs = et.stopInMilliseconds();
System.out.println("measureAllJobs " + et.stringInMilliseconds());
double gainFactor = ((double) ArraySumApp.numberOfJobs) * oneJob
/ allJobs;
System.out.println(String.format(
"throughput gain factor %.1f\nthroughput gain %.1f\n",
gainFactor, gainFactor - 1.0d));
// check the number of answers is correct
assert sums.length == ArraySumApp.numberOfJobs : "cosmic rays";
// check they all have the answer
for (int i = 0; i < ArraySumApp.numberOfJobs; i++)
assert sums[i] == ArraySumApp.theAnswer : "cosmic rays";
}
}
class ArraySum implements Kernel {
final static int repetitionFactor = 100000;
private int[] source;
private int[] ret;
private int index;
public ArraySum(int[] src, int[] dst, int i) {
source = src;
ret = dst;
index = i;
}
public void gpuMethod() {
for (int repetition = 0; repetition < ArraySum.repetitionFactor; repetition++) {
int sum = 0;
for (int i = 0; i < source.length; ++i) {
sum += source[i];
}
ret[index] = sum;
}
}
}
class Helper {
private Helper() {
}
static void assertAssertionEnabled() {
try {
assert false;
} catch (AssertionError e) {
return;
}
Helper.noteCosmicRays();
}
static void noteCosmicRays() // programmer design or logic error
{
throw new RuntimeException("cosmic rays");
}
}
class ElapsedTimer {
private org.joda.time.DateTime t0;
private long savedStopInMilliseconds;
public ElapsedTimer() {
this.t0 = new org.joda.time.DateTime();
}
public long stopInMilliseconds() {
return stop();
}
public String stringInMilliseconds() // relies on a saved stop
{
Formatter f = new Formatter();
f.format("%d ms", this.savedStopInMilliseconds);
String s = f.toString();
f.close();
return s;
}
public String stopStringInMilliseconds() {
stop();
return stringInMilliseconds();
}
public String stringInSecondsAndMilliseconds() // relies on a saved stop
{
Formatter f = new Formatter();
f.format("%5.3f s", this.savedStopInMilliseconds / 1000.0d);
String s = f.toString();
f.close();
return s;
}
public String stopStringInSecondsAndMilliseconds() {
stop();
return stringInSecondsAndMilliseconds();
}
public long stopInSeconds() {
return (stop() + 500L) / 1000L; // rounding
}
public String stringInSeconds() // relies on a saved stop
{
Formatter f = new Formatter();
long elapsed = (this.savedStopInMilliseconds + 500L) / 1000L; // rounding
f.format("%d s", elapsed);
String s = f.toString();
f.close();
return s;
}
public String stopStringInSeconds() {
stop();
return stringInSeconds();
}
/**
* This is private. Use the stopInMilliseconds method if this is what you
* need.
*/
private long stop() {
org.joda.time.DateTime t1 = new org.joda.time.DateTime();
savedStopInMilliseconds = t1.getMillis() - this.t0.getMillis();
return savedStopInMilliseconds;
}
}
This is the output:
measureOneJob 110 ms
measureOneJob 26 ms
CudaRuntime2 ctor: elapsedTimeMillis: 609
measureAllJobs 24341 ms
throughput gain factor 1.1
throughput gain 0.1
The rootbeer developer said the example code that takes the sum of array elements is not the best example and an alternative example would show throughput gains.
You can see: https://github.com/pcpratts/rootbeer1/tree/develop/gtc2013/Matrix
This is an example for the 2013 NVIDIA GTC conference. I obtained a 20x speedup over a 4-core Java Matrix Multiply that uses transpose.
The example is a tiled Matrix Multiply using shared memory on the GPU. From the NVIDIA literature, using shared memory is one of the most important apsects of getting good speedups. To use shared memory you have each thread in a block load values into a shared array. Then you have to reuse these shared values several times. This saves the time to fetch from global memory.
A fetch from global memory takes about 200-300 clock cycles and a fetch from shared memory takes about 2-3 clock cycles on the Tesla 2.0 archicture.