small fix

This commit is contained in:
rsgrinko committed 2023-03-30 00:26:41 +03:00
1 parent 413617d745
commit 8085cd0700
5 files changed
+84 -46

No files matched your search

+27 -43
View File
@@ -40,27 +40,8 @@
[new Neuron(7)],
];
/** Строим связи нейронов */
$this->generateSynapses();
/*$this->synaps = [
new Synaps($this->layers[0][0], $this->layers[1][0]),
new Synaps($this->layers[0][0], $this->layers[1][1]),
new Synaps($this->layers[0][0], $this->layers[1][2]),
new Synaps($this->layers[0][0], $this->layers[1][3]),
new Synaps($this->layers[0][1], $this->layers[1][0]),
new Synaps($this->layers[0][1], $this->layers[1][1]),
new Synaps($this->layers[0][1], $this->layers[1][2]),
new Synaps($this->layers[0][1], $this->layers[1][3]),
new Synaps($this->layers[1][0], $this->layers[2][0]),
new Synaps($this->layers[1][0], $this->layers[2][1]),
new Synaps($this->layers[1][1], $this->layers[2][0]),
new Synaps($this->layers[1][1], $this->layers[2][1]),
new Synaps($this->layers[1][2], $this->layers[2][0]),
new Synaps($this->layers[1][2], $this->layers[2][1]),
new Synaps($this->layers[1][3], $this->layers[2][0]),
new Synaps($this->layers[1][3], $this->layers[2][1]),
];*/
// Получение сохраненных данных по весам
$this->getSynapsValue();
@@ -71,7 +52,7 @@
*/
private function generateSynapses(): void
{
$synapsId = 1;
$synapseId = 1;
$layersCount = count($this->layers);
// Бежим по слоям нейронов (последний нам не нужен) и строим связи
@@ -80,10 +61,10 @@
for ($j = 0; $j < $neuronCount; $j++) {
$neuronInCount = count($this->layers[$i]);
for ($k = 0; $k < $neuronInCount; $k++) {
$this->synapses[] = new Synaps(
$synapsId, $this->layers[$i][$k], $this->layers[$i + 1][$j], $this->arValues[$synapsId] ?? null
$this->synapses[] = new Synapse(
$synapseId, $this->layers[$i][$k], $this->layers[$i + 1][$j], $this->arValues[$synapseId] ?? null
);
$synapsId++;
$synapseId++;
}
}
}
@@ -98,7 +79,7 @@
{
$data = [];
foreach ($this->synapses as $synapse) {
/** @var Synaps $synapse */
/** @var Synapse $synapse */
$data[$synapse->getId()] = $synapse->getValue();
}
@@ -123,7 +104,7 @@
// Устанавливаем веса для синапсов
foreach ($this->synapses as $synapse) {
/** @var Synaps $synapse */
/** @var Synapse $synapse */
$synapse->setValue($synapsValues[$synapse->getId()]);
}
@@ -136,16 +117,30 @@
/**
* Мутация веса
*
* @param bool $useFullRandom Использовать полностью случайный новый вес
* @param float|null $shift Величина сдвига для мутации
*
* @throws \Exception
* @throws Exception
*/
public function mutate(?float $shift = 0.1): self
public function mutate(bool $useFullRandom = false, ?float $shift = 0.1): self
{
foreach ($this->synapses as $synapse) {
/** @var Synaps $synapse */
$tempValue = random_int((int)(($synapse->getValue() - $shift) * 100), (int)(($synapse->getValue() + $shift) * 100)) / 100;
/** @var Synapse $synapse */
if ($useFullRandom) {
// Использовать полностью случайный новый вес
$tempValue = mt_rand() / mt_getrandmax();
} else {
// Использовать случайный вес в диапазоне "$shift < текущий < $shift"
// Вариант генерации 1
//$tempValue = random_int((int)(($synapse->getValue() - $shift) * 100), (int)(($synapse->getValue() + $shift) * 100)) / 100;
// Вариант генерации 2
$tempValue = random_int(
(int)(($synapse->getValue() - $shift) * 100000000),
(int)(($synapse->getValue() + $shift) * 100000000)
) / 100000000;
}
// Ограничиваемся диапазоном 0..1
if ($tempValue < 0) {
$tempValue = 0;
@@ -174,32 +169,21 @@
}
$result = [];
// Отдаем значения нейронов последнего слоя как результат
foreach ($this->layers[2] as $neuron) {
$result[] = $neuron->getValue();
}
$this->saveSynapsValue();
return $result;
//return [$this->layers[2][0]->getValue(), $this->layers[2][1]->getValue()];
}
/**
* Шаг преобразования значений между нейронами
*/
public function step(Synaps $synaps): void
public function step(Synapse $synapse): void
{
/*
// Умножение значения на коэфициент
$synaps->getOutNeuron()->setValue(
$synaps->getInNeuron()->getValue() * $synaps->getValue()
);*/
// Гиперболический тангенс
// https://otus.ru/journal/kak-sozdat-nejroset/
// Функция активации
$synaps->getOutNeuron()->setValue(
tanh($synaps->getInNeuron()->getValue() * $synaps->getValue())
);
$synapse->getOutNeuron()->activate($synapse->getInNeuron()->getValue(), $synapse->getValue());
}
}