{"name":"hidden-markov-model-tf","version":"1.2.0","license":"Apache-2.0","_id":"hidden-markov-model-tf@1.2.0","maintainers":[{"name":"addaleax","email":"anna@addaleax.net"},{"name":"alan.slater","email":"alan.slater@nearform.com"},{"name":"andreasmadsen","email":"amwebdk@gmail.com"},{"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":"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"}],"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":"dcc52ade294cbe40fc0f731574276abc127d0172","tarball":"http://registry-npm.ywwl.com/hidden-markov-model-tf/download/hidden-markov-model-tf-1.2.0.tgz","fileCount":40,"integrity":"sha512-ZjpYMbp6VAngltgvP6g7MUP7mvxSJpVRLiKdDe0m7kLbvKpJ8q9ZumJBHcM6XhfIT2vbbgEl6lKbc41txy2jHw==","signatures":[{"sig":"MEUCIQDiP3LStdr1GOOrKDbREKEapL5ZTJhfBWlbMrACxessHwIgOLSdwiJYrHVMG2PJGuM5gYRwEuGso8QQcKlOMtcwTtc=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":146815,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb3tX+CRA9TVsSAnZWagAAygkP/RtEd4YlWWbYIk7TB7h5\nByV5iHK3S/Lzvi2eNU2svnvP30Oj22AY0pLj9Ky2WRat2J8OwRU8q1mDG6gt\nVudRliv8Za+EF1yLNHOUXG1uxg6MyZtcxcIKFzt6G7RzAtIXxZkxLcABWako\njaoEEq/R9VbZiVa2zj6Dnw5ehAlFGG2e0p083OHMlryffI6RNKDnLZZBb5fU\nn658+ZAJkdsNU1j9sAX8SA0bcNkVlteUraq3VHCtw5a9I0irjwgBstnk1NTl\nmkvAq3jm5pFB4tdYC57dbqP2vFYfB4QMJz7QK8WYGJIvUfIU2er3DyDkkEXG\nnLg15uozCsS4MrvanSURWa1jv2PM70dfEpltCazCsFmxUz14CvCKZzxkXcuZ\njIcWgMrUtE0S2clxWtSnkQsnxZO/Yf9eXPtasiipJUjyuLqeRVYa2O2ZXTOM\nT6kJuYagJ7H36aQMjXLSIdF7+QNYYp6/lTrVoZ5+59L3Ce70qNdxrhJgBB6c\n+rXaGMqr3607O1pZxc+nnMh0EPOef/wxCgmnxAxn7A5+Zr/vGguC1CTPhgfH\ndyVxR9q46GjOUPONYE654vh1fqQGM7huxDLMbxPgg4RPEDY6kU1U5L2j0DAx\nIVYNgR4A5EddvySxbZv6WwQyNYBTk97gUQiOyF/CAo2F5rUYRLJg2KtcgJOm\nF7LW\r\n=hmr0\r\n-----END PGP SIGNATURE-----\r\n","size":46543,"noattachment":false,"key":"/hidden-markov-model-tf/-/hidden-markov-model-tf-1.2.0.tgz"},"main":"index.js","gitHead":"033e5e47dcbe6df05d8bfd0acf6bcfb68936fd57","scripts":{"lint":"standardx --fix","test":"tap test/*.test.js","test-ci":"tap --100 test/*.test.js"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/nearform/node-hidden-markov-model-tf.git","type":"git"},"_npmVersion":"6.4.1","description":"A trainable Hidden Markov Model with Gaussian emissions using TensorFlow.js","directories":{},"_nodeVersion":"8.12.0","dependencies":{"semver":"^5.6.0","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.9","@tensorflow/tfjs-node":"^0.1.19"},"peerDependencies":{"@tensorflow/tfjs-core":"^0.13.9"},"_npmOperationalInternal":{"tmp":"tmp/hidden-markov-model-tf_1.2.0_1541330429153_0.5687206679880787","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":1541330429342,"_cnpm_publish_time":1541330429342,"dist-tags":{"latest":"4.0.0"}}