Skip to content

Convert blob generator to numpower #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Draft
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
24 changes: 11 additions & 13 deletions src/Datasets/Generators/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Exceptions\InvalidArgumentException;
use NDArray as nd;

use function count;
use function sqrt;
Expand All @@ -32,14 +33,12 @@ class Blob implements Generator
*
* @var Vector
*/
protected \Tensor\Vector $center;
protected nd $center;

/**
* The standard deviation of the blob.
*
* @var \Tensor\Vector|int|float
*/
protected $stdDev;
protected int|float|nd $stdDev;

/**
* Fit a Blob generator to the samples in a dataset.
Expand Down Expand Up @@ -94,15 +93,15 @@ public function __construct(array $center = [0, 0], $stdDev = 1.0)
}
}

$stdDev = Vector::quick($stdDev);
$stdDev = nd::array($stdDev);
} else {
if ($stdDev < 0) {
throw new InvalidArgumentException('Standard deviation'
. " must be greater than 0, $stdDev given.");
}
}

$this->center = Vector::quick($center);
$this->center = nd::array($center);
$this->stdDev = $stdDev;
}

Expand All @@ -113,7 +112,7 @@ public function __construct(array $center = [0, 0], $stdDev = 1.0)
*/
public function center() : array
{
return $this->center->asArray();
return $this->center->toArray();
}

/**
Expand All @@ -125,7 +124,7 @@ public function center() : array
*/
public function dimensions() : int
{
return $this->center->n();
return $this->center->size();
}

/**
Expand All @@ -138,11 +137,10 @@ public function generate(int $n) : Unlabeled
{
$d = $this->dimensions();

$samples = Matrix::gaussian($n, $d)
->multiply($this->stdDev)
->add($this->center)
->asArray();
$samples = nd::normal([$n, $d]);
$samplesMul = nd::multiply($samples, $this->stdDev);
$samplesMulAddCenter = nd::add($samplesMul, $this->center);

return Unlabeled::quick($samples);
return Unlabeled::quick($samplesMulAddCenter->toArray());
}
}