Skip to content

Commit

Permalink
Merge pull request #460 from jsuyash1514/issue#459
Browse files Browse the repository at this point in the history
[WIP] ND4J and DL4J notebook
  • Loading branch information
samagra14 authored Jun 19, 2019
2 parents 36c0ab5 + 22f570f commit cbea2ad
Showing 1 changed file with 272 additions and 0 deletions.
272 changes: 272 additions & 0 deletions notebooks/User_guide_for_ND4J_and_DL4J.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ND4J and DL4J exploration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ND4J is a tensor & N-dimensional array scientific computing library built for the JVM and is part of the [Deeplearning4j](https://deeplearning4j.org/) suite of software. This notebook serves as the user guide to understand the main functionality of ND4J. In this notebook, we'll explore its use-cases through numerous examples and then we'll discuss its usage in the project. So, let's begin with loading some essential JARs."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "79464426-0661-46aa-9c0d-f252429dbef1",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%classpath add mvn org.nd4j nd4j-native-platform 0.9.0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e1be3069-6c5b-4b18-b424-5235b361ca78",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%classpath add mvn org.bytedeco javacpp 1.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ND4J(N-Dimensions For Java) or – as its creators present it – the “numpy” for the JVM, allows the user to create and perform various mathematical operations on N-Dimensional arrays. Throughout this notebook, we’ll use the term **NDArray** to refer to the general concept of an n-dimensional array; the term **INDArray** refers specifically to the [Java interface](https://github.com/deeplearning4j/nd4j/blob/master/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/api/ndarray/INDArray.java) that ND4J defines. In practice, these two terms can be used interchangeably. \n",
"\n",
"First, let's see how to create an NDArray:-"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating NDArrays\n",
"\n",
"Before we begin with creating an NDArray, let's see some concepts you should be familiar with:\n",
"\n",
"* The rank of an NDArray is the number of dimensions. 2d NDArrays have a rank of 2, 3d arrays have a rank of 3, and so on. You can create NDArrays with any arbitrary rank.\n",
"* The shape of an NDArray defines the size of each of the dimensions. Suppose we have a 2d array with 3 rows and 5 columns. This NDArray would have shape [3,5].\n",
"* The length of an NDArray defines the total number of elements in the array. The length is always equal to the product of the values that make up the shape.\n",
"* The stride of an NDArray is defined as the separation (in the underlying data buffer) of contiguous elements in each dimension. Stride is defined per dimension, so a rank N NDArray has N stride values, one for each dimension. Note that most of the time, you don’t need to know (or concern yourself with) the stride - just be aware that this is how ND4J operates internally.\n",
"\n",
"Now, let's see the different ways to create an NDArray:-"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nd4j.zeros(nRows, nColumns)\n",
"[[0.00, 0.00, 0.00, 0.00, 0.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00]]\n",
"\n",
"\n",
"Nd4j.ones(nRows, nColumns)\n",
"[[1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [1.00, 1.00, 1.00, 1.00, 1.00]]\n",
"\n",
"\n",
"Nd4j.valueArrayOf(nRows, nColumns, 10.0)\n",
"[[10.00, 10.00, 10.00, 10.00, 10.00], \n",
" [10.00, 10.00, 10.00, 10.00, 10.00], \n",
" [10.00, 10.00, 10.00, 10.00, 10.00]]\n",
"\n",
"\n",
"INDArray defined from double[][]:\n",
"[[1.00, 2.00, 3.00], \n",
" [4.00, 5.00, 6.00]]\n",
"\n",
"\n",
"Uniform random array:\n",
"[[0.62, 0.39, 0.59, 0.29, 0.59], \n",
" [0.35, 0.13, 0.05, 0.67, 0.71], \n",
" [0.70, 0.48, 0.59, 0.90, 0.23]]\n",
"\n",
"\n",
"Creating INDArrays from other INDArrays, using hstack and vstack:\n",
"vStack:\n",
"[[0.00, 0.00, 0.00, 0.00, 0.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00], \n",
" [1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [1.00, 1.00, 1.00, 1.00, 1.00]]\n",
"hStack:\n",
"[[0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00, 1.00, 1.00], \n",
" [0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00, 1.00, 1.00]]\n",
"\n",
"\n",
"Nd4j.eye(3):\n",
"[[1.00, 0.00, 0.00], \n",
" [0.00, 1.00, 0.00], \n",
" [0.00, 0.00, 1.00]]\n",
"Nd4j.linspace(1,10,10):\n",
"[1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, 10.00]\n"
]
},
{
"data": {
"text/plain": [
"null"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import org.nd4j.linalg.api.ndarray.INDArray;\n",
"import org.nd4j.linalg.factory.Nd4j;\n",
"\n",
"import java.util.Arrays;\n",
"\n",
"//First, we'll see how to create INDArrays with different scalar value initializations\n",
"int nRows = 3;\n",
"int nColumns = 5;\n",
"INDArray allZeros = Nd4j.zeros(nRows, nColumns);\n",
"System.out.println(\"Nd4j.zeros(nRows, nColumns)\");\n",
"System.out.println(allZeros);\n",
"\n",
"INDArray allOnes = Nd4j.ones(nRows, nColumns);\n",
"System.out.println(\"\\n\\nNd4j.ones(nRows, nColumns)\");\n",
"System.out.println(allOnes);\n",
"\n",
"INDArray allTens = Nd4j.valueArrayOf(nRows, nColumns, 10.0);\n",
"System.out.println(\"\\n\\nNd4j.valueArrayOf(nRows, nColumns, 10.0)\");\n",
"System.out.println(allTens);\n",
"\n",
"//We can also create INDArrays from double[] and double[][] (or, float/int etc Java arrays)\n",
"\n",
"double[][] matrixDouble = new double[][]{\n",
" {1.0, 2.0, 3.0},\n",
" {4.0, 5.0, 6.0}};\n",
"INDArray matrix = Nd4j.create(matrixDouble);\n",
"System.out.println(\"\\n\\nINDArray defined from double[][]:\");\n",
"System.out.println(matrix);\n",
"\n",
"//It is also possible to create random INDArrays:\n",
"\n",
"int[] shape = new int[]{nRows, nColumns};\n",
"INDArray uniformRandom = Nd4j.rand(shape);\n",
"System.out.println(\"\\n\\nUniform random array:\");\n",
"System.out.println(uniformRandom);\n",
"\n",
"//We can create INDArrays by combining other INDArrays, too:\n",
"\n",
"INDArray vStack = Nd4j.vstack(allZeros, allOnes);//Vertical stack: [1,3]+[1,3] to [2,3]\n",
"INDArray hStack = Nd4j.hstack(allZeros, allOnes);//Horizontal stack: [1,3]+[1,3] to [1,6]\n",
"System.out.println(\"\\n\\nCreating INDArrays from other INDArrays, using hstack and vstack:\");\n",
"System.out.println(\"vStack:\\n\" + vStack);\n",
"System.out.println(\"hStack:\\n\" + hStack);\n",
"\n",
"//There's some other miscellaneous methods, too:\n",
"\n",
"INDArray identityMatrix = Nd4j.eye(3);\n",
"System.out.println(\"\\n\\nNd4j.eye(3):\\n\" + identityMatrix);\n",
"INDArray linspace = Nd4j.linspace(1,10,10); //Values 1 to 10, in 10 steps\n",
"System.out.println(\"\\n\\nNd4j.linspace(1,10,10):\\n\" + linspace);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"codemirror_mode": "text/x-java",
"file_extension": ".java",
"mimetype": "",
"name": "Java",
"nbconverter_exporter": "",
"version": "1.8.0_144"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit cbea2ad

Please sign in to comment.