Skip to content

refactor/naive bayes with tensors#142

Merged
JasonShin merged 26 commits intov2from
bayes-tensors
Dec 9, 2018
Merged

refactor/naive bayes with tensors#142
JasonShin merged 26 commits intov2from
bayes-tensors

Conversation

@sudo-ben
Copy link
Contributor

@sudo-ben sudo-ben commented Nov 25, 2018

  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
    Rewrite of GaussianNB with Tensorflow support.

The PR also introduces:

  • Iterator implementation for predict
  • Tensorflow Linear Algebra refactoring
  • Updating the doc-processor to support native types such as IterableIteration

@JasonShin
Copy link
Member

JasonShin commented Nov 25, 2018

Hey, could you help me fix this issue?

I cloned your repository and ran yarn to install npm modules then yarn test, but I am getting below error

src/lib/naive_bayes/multinomial.ts:84:14 - error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '(<U>(callbackfn: (value: ReadonlyArray<number>, index: number, array: ReadonlyArray<ReadonlyArray...' has no compatible call signatures.

84       return X.map((x):T => this.singlePredict(x));
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


src/lib/naive_bayes/multinomial.ts:167:30 - error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{ (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Readonl...' has no compatible call signatures.

167       productReducedRow.push(rowFrequencyCount.reduce((s, c) => s + c, 0));
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^^^ is non issue, I realised I was looking at your fork but you are working on a branch bayes_tensors

@JasonShin JasonShin changed the title naive bayes with tensors refactor/naive bayes with tensors Nov 26, 2018
@JasonShin
Copy link
Member

Ok, I resolved the error from doc processor

Using TypeScript 2.7.2 from /home/jason/Desktop/kalimdorjs/node_modules/typedoc/node_modules/typescript/lib
To generate markdown please set option --theme markdown

JSON written to /home/jason/Desktop/kalimdorjs/docs/docs.json

/home/jason/Desktop/kalimdorjs/docs/processor/index.js:274
            if (foundRef.kindString === consts.refKindInterface) {
                         ^

TypeError: Cannot read property 'kindString' of null
    at /home/jason/Desktop/kalimdorjs/docs/processor/index.js:274:26
    at arrayReduce (/home/jason/Desktop/kalimdorjs/node_modules/lodash/lodash.js:683:21)
    at Function.reduce (/home/jason/Desktop/kalimdorjs/node_modules/lodash/lodash.js:9683:14)

You were getting this error due to introducing a TS Native Interface IterableIterator. So far in Kalimdor, we've only been using custom defined Interfaces such as StrNumDict or InterfaceFitModel. The difference between custom and native interface is that the former gives you an ID as a reference, which you can use it to find its definition, on the other hand, the latter does not expose an ID. Here are examples:

Custom Interface

{
					"id": 1130,
					"name": "InterfaceFitModel",
					"kind": 256,
					"kindString": "Interface",
					"flags": {},
					"typeParameter": [
						{
							"id": 1131,
							"name": "T",
							"kind": 131072,
							"kindString": "Type parameter",
							"flags": {}
						}
					],
					"children": [
						{
							"id": 1132

^^ has an ID that we can use to reference its definition within docs.json. However, a native interface such as IterableIteration does not have an ID because it's a native type.

Anyways, thanks @BenjaminMcDonald for helping us improving the documentation processor =) !!

@sudo-ben
Copy link
Contributor Author

sudo-ben commented Dec 3, 2018

Update the branch with added support for constraint in doc types added multinomial naive bayes. This with your above patch fixed the issue with the document generation. yarn test now completes

@JasonShin JasonShin self-assigned this Dec 3, 2018
@JasonShin JasonShin added the feature New feature that does not exist in Kalimdor.js yet label Dec 3, 2018
@JasonShin
Copy link
Member

JasonShin commented Dec 3, 2018

I am going to write some TODOs for myself here

TODO:
(/) Removing excessive comments
(/) Add exception tests for MultinomialNB
(?) Add real data tests using Iris for MultinomialNB
(/) modelState can be just class fields
(/) export MultinomialNB in index.ts
(/) Add integration tests
(/) refactor isMatrix usage to tensor_ops.ts APIs
(/) Running yarn doc prints

[ { type: 'reference',
    name: 'IterableIterator',
    typeArguments: [ [Object] ] } ]
[ { type: 'typeParameter',
    name: 'T',
    constraint: { type: 'union', types: [Array] } } ]
[ { type: 'reference',
    name: 'IterableIterator',
    typeArguments: [ [Object] ] } ]

to the screen. Remove the console logs.
(/) Running the MultinomialNB unit tests prints


  console.log node_modules/@tensorflow/tfjs-core/dist/ops/array_ops.js:500
    Tensor
        [[-0.8873032, -0.5306282],
         [-0.6931472, -0.6931472],
         [-0.4700036, -0.9808292]]

  console.log node_modules/@tensorflow/tfjs-core/dist/ops/array_ops.js:500
    Tensor
        [-1.0986123, -1.0986123, -1.0986123]

on the screen

(?) Some methods parameters in the docstrings are missing either description or a default value
(/) Release note

1. Removing duplicate interface types. InterfaceFitModel and
InterfaceFitModelAsArray can be consolidated
2. removing modelState, we can just save them as class fields
3. Using Type1DMatrix and Type2DMatrix instead of ReadOnlyArray as type
4. Refactring toJSON and fromJSON accordingly
5. Refactoring fitModel function, mostly typings
6. using tf's sum reduction to get product reduced row
1. toJSON and fromJSON now leverages reshape method
2. fixing TF warnings with typing
@JasonShin
Copy link
Member

I propose we exclude


  /**
   * @param  {IterableIterator<IterableIterator<number>>} X
   * @returns IterableIterator
   */
  public *predictIterator(
    X: IterableIterator<IterableIterator<number>>
  ): IterableIterator<T> {
    for (const x of X) {
      yield this.singlePredict([...x]);
    }
  }

from this PR. I think it's best to introduce this feature in a separate PR that implements it globally throughout the existing models.

@JasonShin JasonShin self-requested a review December 9, 2018 03:54
@JasonShin JasonShin added the enhancement New feature or request label Dec 9, 2018
Copy link
Member

@JasonShin JasonShin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alll good now

public predict(X: Type2DMatrix<number> = []): number[] {
validateMatrix2D(X);
let clonedX = X;
public model(): InterfaceFitModel<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this when we already have toJSON?

import { IMlModel, Type1DMatrix, Type2DMatrix } from '../types';
import math from '../utils/MathExtra';

const { isMatrix } = math.contrib;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should start using ops/tensor_ops.ts for the Tensor / Matrix validations. It relies on TF.js for validation =)


if (this.clone) {
clonedX = cloneDeep(X);
public *predictIterator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to do some research on Iterators. I think it's a powerful feature from TS that we haven't been using so far and we should think about supporting it globally in v2 release.

@JasonShin
Copy link
Member

I think this PR Is fine to be merged now!! GREAT WORK and thanks for your second contributions @BenjaminMcDonald !!!

I will address the documentation issue that I describe in #161 in a separate branch.

@JasonShin JasonShin merged commit 1a578de into v2 Dec 9, 2018
@JasonShin JasonShin deleted the bayes-tensors branch December 10, 2018 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature New feature that does not exist in Kalimdor.js yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants