improve: remove deps on tensorflow.net

This commit is contained in:
h z
2025-01-28 18:36:25 +00:00
parent ed9bfe9869
commit 8d7fe158c8
2 changed files with 51 additions and 30 deletions

View File

@@ -10,10 +10,4 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.16.0" />
<PackageReference Include="TensorFlow.Keras" Version="0.15.0" />
<PackageReference Include="TensorFlow.NET" Version="0.150.0" />
</ItemGroup>
</Project>

View File

@@ -8,19 +8,12 @@ namespace InverseOfLife.NeuralSolver;
public class NeuralSolver : IDisposable
{
private int Width { get; set; }
private int Height { get; set; }
private bool QuotientX { get; set; }
private bool QuotientY { get; set; }
private string NeuralBackend { get; set; }
private HttpClient HttpClient { get; set; }
private int Width { get; }
private int Height { get; }
private bool QuotientX { get; }
private bool QuotientY { get; }
private string NeuralBackend { get; }
private HttpClient HttpClient { get; }
public NeuralSolver(int width, int height, bool quotientX = false, bool quotientY = false,
string neuralBackend = "http://localhost:8000")
@@ -50,6 +43,20 @@ public class NeuralSolver : IDisposable
response.EnsureSuccessStatusCode();
}
public void TryLoad(TrainRequest request)
{
string jsonContent = JsonSerializer.Serialize(request);
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/try_load", content).Result;
response.EnsureSuccessStatusCode();
while (true)
{
string status = BackendStatus();
if (status == "ready" || status == "error")
return;
}
}
public void Train(TrainRequest request)
{
string jsonContent = JsonSerializer.Serialize(request);
@@ -69,7 +76,6 @@ public class NeuralSolver : IDisposable
public Board Predict(PredictRequest request)
{
string jsonContent = JsonSerializer.Serialize(request);
Console.WriteLine(jsonContent);
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/predict", content).Result;
response.EnsureSuccessStatusCode();
@@ -152,37 +158,58 @@ public class NeuralSolver : IDisposable
yield return point;
}
}
public void Dispose()
{
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/finish", null).Result;
}
public static void Test()
{
using (NeuralSolver s = new NeuralSolver(20, 20, false, false))
{
//s.Initialize();
s.Train(new TrainRequest
s.TryLoad(new TrainRequest
{
ForwardDatasetSize = 2000,
ForwardBatchSize = 16,
ForwardEpochs = 15,
BackwardDatasetSize = 4000,
BackwardDatasetSize = 16000,
BackwardBatchSize = 16,
BackwardEpochs = 20,
BackwardEpochs = 40,
});
//s.SaveModel();
Board b = new Board(20, 20);
foreach ((int, int) p in Circle())
b.Lives.Add(p);
b.Evaluate();
Console.WriteLine(b.ToString());
Console.WriteLine("-------------");
Board h = s.Predict(new PredictRequest
{
Lives = b.Lives.Select(((int x, int y) cell) => new List<int>{cell.x, cell.y}).ToList(),
Direction = "backward"
});
Console.WriteLine(h.ToString());
Board h2 = s.Predict(new PredictRequest
{
Lives = h.Lives.Select(((int x, int y) cell) => new List<int>{cell.x, cell.y}).ToList(),
Direction = "backward",
});
Board h3 = s.Predict(new PredictRequest
{
Lives = h2.Lives.Select(((int x, int y) cell) => new List<int>{cell.x, cell.y}).ToList(),
Direction = "backward",
});
Console.WriteLine(h3.ToString());
Console.WriteLine("-------------");
h3.Evaluate();
Console.WriteLine(h3.ToString());
Console.WriteLine("-------------");
h3.Evaluate();
Console.WriteLine(h3.ToString());
}
}
public void Dispose()
{
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/finish", null).Result;
}
}