{"name":"hidden-markov-model-tf","version":"1.1.0","license":"Apache-2.0","_id":"hidden-markov-model-tf@1.1.0","maintainers":[{"name":"addaleax","email":"anna@addaleax.net"},{"name":"alan.slater","email":"alan.slater@nearform.com"},{"name":"andreasmadsen","email":"amwebdk@gmail.com"},{"name":"bridgear","email":"ruben@bridgewater.de"},{"name":"cianfoley_nearform","email":"cian.foley@nearform.com"},{"name":"conoro","email":"conor@conoroneill.com"},{"name":"damianberesford","email":"dberesford@gmail.com"},{"name":"dancork-nf","email":"dan.cork@nearform.com"},{"name":"davidefiorello","email":"davide@codeflyer.com"},{"name":"davidmarkclements","email":"huperekchuno@googlemail.com"},{"name":"filippo.desantis","email":"filippo.desantis@gmail.com"},{"name":"hueniverse","email":"eran@hammer.io"},{"name":"ivanjov","email":"ivanjov96@gmail.com"},{"name":"jasnell","email":"jasnell@gmail.com"},{"name":"jasonmelo","email":"jason.melo@gmail.com"},{"name":"jh3y","email":"jh3y@shiftbear.com"},{"name":"jimmymintzer","email":"jimmymintzer@gmail.com"},{"name":"kamil-mech","email":"kamil.mech@nearform.com"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"mattrobinson","email":"matt.robinson@nearform.com"},{"name":"mcdonnelldean","email":"mcdonnelldean@outlook.com"},{"name":"mihaidma","email":"mihaizn@gmail.com"},{"name":"mrister","email":"mihovil.rister@gmail.com"},{"name":"nearformer","email":"david.clements@nearform.com"},{"name":"robbdempsey","email":"robb.dempsey@gmail.com"},{"name":"shaunjbaker","email":"shaun@schb.co.uk"},{"name":"shogun_panda","email":"paolo@cowtech.it"},{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"temsa","email":"florian.traverse@gmail.com"},{"name":"thekemkid","email":"glenkeane.94@gmail.com"},{"name":"wprl","email":"william@Kun.io"},{"name":"yoshuawuyts","email":"yoshuawuyts@gmail.com"}],"homepage":"https://github.com/nearform/node-hidden-markov-model-tf#readme","bugs":{"url":"https://github.com/nearform/node-hidden-markov-model-tf/issues"},"dist":{"shasum":"6ce8c75278d4b3fc45390d378d7d823733030ea5","tarball":"http://registry-npm.ywwl.com/hidden-markov-model-tf/download/hidden-markov-model-tf-1.1.0.tgz","fileCount":40,"integrity":"sha512-vl7WejmMaieWElvqlEOKLMuLuXlCqFf5n4c+rBFY/W/8Qk5MfLvSaTgcL5KDBKXnAN41NEQffccWAIrKQF4vaw==","signatures":[{"sig":"MEQCIG/xuoWpwyQQWJd3MxtxuySliujMq/+aDiqKSy1zNAAPAiBmoh+6WwfgxgktEHMn2YBmv5mFoJqDXEqcl4sVeLIhSg==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":146552,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbvcGGCRA9TVsSAnZWagAAaw4P/1JJUp6zBG90Qvnsb0Mv\nPEooQJYch6+hZSZtKM5W3D78YyzQ7qsVz5vUYB8lG3zsrOMO3jnc/5d7byQF\n22/5GGQa0LVD732DHIZeN3Rhmbo/k9Ar9c6di+akvhPr3uZcsgQ7sTptiYnj\nwHS/qrLVHQED64ACTWnxT9pePy35He1KYxueJdqYI64OoeHng8zlk4wf5066\nYH6Vs6NpCrU2vJm9nj/JjWWsLRSfAepQE26QB+mZXOLGbhsohJ3VCa3W3Xlk\n9ac6LUPSkIr0nN8zOmxkzSqhv5D7eIiX2h9v1rYFcFgFMOajcakj/fPI64yW\nrOXsVywWw/8IZDqd4ND/VcURTTJFgmawFfceNCP2Q3LAzuPGoRn5rvl/gOql\n6d126gixcYryvH8SvKUJ59979I2/fmHqUjeMCvtkveN3mgw1K9LGMdpC+TVJ\nGjdZYGx/YQuz+t49x+xupqvaIyKeKm1gLduTz3J/wdeFp7UIaizXnpbgKIMY\nDol2b2T50dLL7N0dCpNI3ic0DD3kdTR0ifB1U0uduEmaymv6i566VvqxvEuP\nsV9fIf9pBTSKHzZ/3OvHadXXRyS12C7AWVdKlOD1AFcTPpACVdWcdtKQ5Q+k\nco3zF9bfJjNIrBKHbdZo5AyLK+XIu5svMm7fq3IFKtEheI17SoH1S/szfkua\ndCwM\r\n=L7TV\r\n-----END PGP SIGNATURE-----\r\n","size":46456,"noattachment":false,"key":"/hidden-markov-model-tf/-/hidden-markov-model-tf-1.1.0.tgz"},"main":"index.js","gitHead":"ebd6b2b591a13157d25ea80b73645b6f2304011e","scripts":{"lint":"standardx --fix","test":"tap test/*.test.js","test-ci":"tap --100 test/*.test.js"},"_npmUser":{"name":"andreasmadsen","email":"amwebdk@gmail.com"},"repository":{"url":"git+https://github.com/nearform/node-hidden-markov-model-tf.git","type":"git"},"_npmVersion":"6.1.0","description":"A trainable Hidden Markov Model with Gaussian emissions using TensorFlow.js","directories":{},"_nodeVersion":"10.6.1-pre","dependencies":{"semver":"^5.5.1","ndarray":"^1.0.18","ml-kmeans":"^4.0.1","seedrandom":"^2.4.4","ndarray-inv":"^0.2.0","ndarray-determinant":"^1.0.0","ndarray-cholesky-factorization":"^1.0.2"},"eslintConfig":{"rules":{"semi":["error","always"],"camelcase":0}},"_hasShrinkwrap":false,"devDependencies":{"tap":"^12.0.1","isndarray":"^1.0.0","standardx":"^3.0.0","ndarray-pack":"^1.2.1","test-allclose":"^1.1.0","ndarray-unpack":"^1.0.0","@tensorflow/tfjs-core":"^0.13.3","@tensorflow/tfjs-node":"^0.1.18"},"_npmOperationalInternal":{"tmp":"tmp/hidden-markov-model-tf_1.1.0_1539162501255_0.6169570467931453","host":"s3://npm-registry-packages"},"readme":"# hidden-markov-model-tf\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/nearform/node-hidden-markov-model-tf.svg)](https://greenkeeper.io/)\n\n**A trainable Hidden Markov Model with Gaussian emissions using TensorFlow.js**\n\n## Install\n\n```\n$ npm install hidden-markov-model-tf\n```\n\nRequire: Node v12+\n\n## Usage\n\n```js\nconst assert = require('assert'):\nrequire('@tensorflow/tfjs-node'); // Optional, enable native TensorFlow backend\nconst tf = require('@tensorflow/tfjs');\nconst HMM = require('hidden-markov-model-tf');\n\nconst [observations, time, states, dimensions] = [5, 7, 3, 2];\n\n// Configure model\nconst hmm = new HMM({\n  states: states,\n  dimensions: dimensions\n});\n\n// Set parameters\nawait hmm.setParameters({\n  pi: tf.tensor([0.15, 0.20, 0.65]),\n  A: tf.tensor([\n    [0.55, 0.15, 0.30],\n    [0.45, 0.45, 0.10],\n    [0.15, 0.20, 0.65]\n  ]),\n  mu: tf.tensor([\n    [-7.0, -8.0],\n    [-1.5,  3.7],\n    [-1.7,  1.2]\n  ]),\n  Sigma: tf.tensor([\n    [[ 0.12, -0.01],\n     [-0.01,  0.50]],\n    [[ 0.21,  0.05],\n     [ 0.05,  0.03]],\n    [[ 0.37,  0.35],\n     [ 0.35,  0.44]]\n  ])\n});\n\n// Sample data\nconst sample = hmm.sample({observations, time});\nassert.deepEqual(sample.states.shape, [observations, time]);\nassert.deepEqual(sample.emissions.shape, [observations, time, dimensions]);\n\n// Your data must be a tf.tensor with shape [observations, time, dimensions]\nconst data = sample.emissions;\n\n// Fit model with data\nconst results = await hmm.fit(data);\nassert(results.converged);\n\n// Predict hidden state indices\nconst inference = hmm.inference(data);\nassert.deepEqual(inference.shape, [observations, time]);\nstates.print();\n\n// Compute log-likelihood\nconst logLikelihood = hmm.logLikelihood(data);\nassert.deepEqual(logLikelihood.shape, [observations]);\nlogLikelihood.print();\n\n// Get parameters\nconst {pi, A, mu, Sigma} = hmm.getParameters();\npi.print();\nA.print();\nmu.print();\nSigma.print();\n```\n\n## Documentation\n\n`hidden-markov-model-tf` is TensorFlow.js based, therefore your input must\nbe povided as a `tf.tensor`. Likewise most outputs are also provided as a\n`tf.tensor`. You can always get a `TypedArray` with `await tensor.data()`.\n\n### hmm = new HMM({states, dimensions})\n\nThe constructor takes two integer arguments. The number of hidden `states` and\nthe number of `dimensions` in the Gaussian emissions.\n\n### result = await hmm.fit(tensor, {maxIterations = 100, tolerance = 0.001, seed})\n\nThe `fit` method, takes an required `tf.tensor` object. That must have the\nshape `[observations, time, dimensions]`. If you only have one observation\nit should have the shape `[1, time, dimensions]`.\n\nThe `fit` method, returns a `Promise` for the `results`. The `results` is\nan object with the following properties:\n\n```js\nconst {\n   // the number of iterations used, will at most be `maxIterations`\n  iterations,\n\n   // if the training coverged, given the `tolerance`,\n   // before `maxIterations` was reached\n  converged,\n\n  // The achived tolerance, after the number of iterations. This can be\n  // useful if the optimizer did not converge, but you want to know how\n  // good the fit is.\n  tolerance\n} = await hmm.fit(tensor);\n```\n\nThe `fit` method uses a KMeans initialization. This initialization algorithm is\nrandom but can be seeded with the optional `seed` parameter.\n\nAfter initialization, the model is optimized using an EM-algorithm called\nthe [Baum–Welch algorithm](https://en.wikipedia.org/wiki/Baum%E2%80%93Welch_algorithm).\n\n### states = hmm.inference(tensor)\n\nThe `inference` method, takes an required `tf.tensor` object. That must have\nthe shape `[observations, time, dimensions]`.\n\nIt uses the [Viterbi algorithm](https://en.wikipedia.org/wiki/Viterbi_algorithm)\nfor infering the hidden state. Which is returned as `tf.tensor` with the\nshape `[observations, time]`.\n\n```js\nconst states = hmm.inference(tensor);\nstates.print();\nconsole.log(await states.data());\n```\n\n### logLikelihood = hmm.logLikelihood(tensor)\n\nThe `inference` method, takes an required `tf.tensor` object. That must have\nthe shape `[observations, time, dimensions]`.\n\nIt uses the forward procedure of the\n[Baum–Welch algorithm](https://en.wikipedia.org/wiki/Baum%E2%80%93Welch_algorithm)\nto compute the logLikelihood for each observation. This is returned as a\n`tf.tensor` with the shape `[observations]`.\n\n### {states, emissions} = hmm.sample({ observations, time, seed })\n\nThe `sample` method, samples data from the Hidden Markov Model distribution\nand returns both the sampled states and Gaussian emissions, as two `tf.tensor`\nobjects.\n\nthe `states` tensor has the shape `[observations, time]`. While the `emissions`\ntensor has the `shape` [observations, time, dimensions].\n\nThe sampling can be seed with the optional `seed` parameter.\n\n### {pi, A, mu, Sigma} = hmm.getParameters()\n\nReturn the underlying parameters:\n\n* `pi`: the hidden state prior distribution. `shape = [states]`\n* `A`: the hidden state transfer distribution. `shape = [states, states]`\n* `mu`: the mean of the Gaussian emission distribution. `shape = [states, dimensions]`\n* `Sigma`: the covariance matrix of the Gaussian emission distribution. `shape = [states, dimensions,  dimensions]`\n\n### await hmm.setParameters({pi, A, mu, Sigma})\n\nSet the underlying parameters of the Hidden Markov Model. Note that some\ninternal properties related to the Gaussian distribution will be precomputed.\nTherefore this returns a `Promise`. Be sure to wait for the promise to\nresolve before calling any other method.\n","publish_time":1539162501480,"_cnpm_publish_time":1539162501480,"dist-tags":{"latest":"4.0.0"}}