Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"private": true,
"dependencies": {
"@tensorflow/tfjs-node": "^4.1.0",
"@tensorflow/tfjs-node-gpu": "^4.1.0",
"tslib": "^2.4.1"
},
"devDependencies": {
Expand All @@ -30,4 +31,3 @@
"typescript": "4.8.4"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import { Metric } from '../testing/metric';
import { MetricCalculator } from '../testing/metric-calculator';
import { binarize } from '../utils/binarize';
import { FeatureNormalizer } from '../feature-engineering/feature-normalizer';
import { switchHardwareUsage } from '../utils/switch-hardware-usage';

export type BinaryClassificationTrainerOptions = {
shouldUseGPU?: boolean;
batchSize?: number;
epochs?: number;
patience?: number;
Expand Down Expand Up @@ -53,6 +55,8 @@ export class BinaryClassificationTrainer {
protected static DEFAULT_PATIENCE: number = 20;

constructor(options: BinaryClassificationTrainerOptions) {
switchHardwareUsage(options.shouldUseGPU);

this.batchSize = options.batchSize ?? BinaryClassificationTrainer.DEFAULT_BATCH_SIZE;
this.epochs = options.epochs ?? BinaryClassificationTrainer.DEFAULT_EPOCHS;
this.patience = options.patience ?? BinaryClassificationTrainer.DEFAULT_PATIENCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { LayersModel, loadLayersModel, tensor, Tensor } from '@tensorflow/tfjs-node';
import { switchHardwareUsage } from '../utils/switch-hardware-usage';

export type BinaryClassifierOptions = {
model: LayersModel;
shouldUseGPU?: boolean;
};

export class BinaryClassifier {
protected model?: LayersModel;

constructor(options?: BinaryClassifierOptions) {
switchHardwareUsage(options?.shouldUseGPU);

this.model = options?.model;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/tfjs-node-helpers/src/utils/switch-hardware-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const switchHardwareUsage = (shouldUseGPU?: boolean): void => {
if (shouldUseGPU) {
console.log('Attempting to use GPU.');
require('@tensorflow/tfjs-node-gpu');
} else {
console.log('Using CPU.');
require('@tensorflow/tfjs-node');
}
};