improve: remove deps on tensorflow.net
This commit is contained in:
@@ -10,10 +10,4 @@
|
|||||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||||
</PropertyGroup>
|
</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>
|
</Project>
|
||||||
|
|||||||
@@ -8,19 +8,12 @@ namespace InverseOfLife.NeuralSolver;
|
|||||||
public class NeuralSolver : IDisposable
|
public class NeuralSolver : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private int Width { get; }
|
||||||
|
private int Height { get; }
|
||||||
|
private bool QuotientX { get; }
|
||||||
|
private bool QuotientY { get; }
|
||||||
|
private string NeuralBackend { get; }
|
||||||
|
private HttpClient HttpClient { get; }
|
||||||
|
|
||||||
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; }
|
|
||||||
|
|
||||||
public NeuralSolver(int width, int height, bool quotientX = false, bool quotientY = false,
|
public NeuralSolver(int width, int height, bool quotientX = false, bool quotientY = false,
|
||||||
string neuralBackend = "http://localhost:8000")
|
string neuralBackend = "http://localhost:8000")
|
||||||
@@ -50,6 +43,20 @@ public class NeuralSolver : IDisposable
|
|||||||
response.EnsureSuccessStatusCode();
|
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)
|
public void Train(TrainRequest request)
|
||||||
{
|
{
|
||||||
string jsonContent = JsonSerializer.Serialize(request);
|
string jsonContent = JsonSerializer.Serialize(request);
|
||||||
@@ -69,7 +76,6 @@ public class NeuralSolver : IDisposable
|
|||||||
public Board Predict(PredictRequest request)
|
public Board Predict(PredictRequest request)
|
||||||
{
|
{
|
||||||
string jsonContent = JsonSerializer.Serialize(request);
|
string jsonContent = JsonSerializer.Serialize(request);
|
||||||
Console.WriteLine(jsonContent);
|
|
||||||
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
||||||
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/predict", content).Result;
|
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/predict", content).Result;
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
@@ -152,37 +158,58 @@ public class NeuralSolver : IDisposable
|
|||||||
yield return point;
|
yield return point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
HttpResponseMessage response = HttpClient.PostAsync($"{NeuralBackend}/finish", null).Result;
|
||||||
|
}
|
||||||
|
|
||||||
public static void Test()
|
public static void Test()
|
||||||
{
|
{
|
||||||
using (NeuralSolver s = new NeuralSolver(20, 20, false, false))
|
using (NeuralSolver s = new NeuralSolver(20, 20, false, false))
|
||||||
{
|
{
|
||||||
//s.Initialize();
|
s.TryLoad(new TrainRequest
|
||||||
s.Train(new TrainRequest
|
|
||||||
{
|
{
|
||||||
ForwardDatasetSize = 2000,
|
ForwardDatasetSize = 2000,
|
||||||
ForwardBatchSize = 16,
|
ForwardBatchSize = 16,
|
||||||
ForwardEpochs = 15,
|
ForwardEpochs = 15,
|
||||||
BackwardDatasetSize = 4000,
|
BackwardDatasetSize = 16000,
|
||||||
BackwardBatchSize = 16,
|
BackwardBatchSize = 16,
|
||||||
BackwardEpochs = 20,
|
BackwardEpochs = 40,
|
||||||
});
|
});
|
||||||
|
//s.SaveModel();
|
||||||
Board b = new Board(20, 20);
|
Board b = new Board(20, 20);
|
||||||
foreach ((int, int) p in Circle())
|
foreach ((int, int) p in Circle())
|
||||||
b.Lives.Add(p);
|
b.Lives.Add(p);
|
||||||
b.Evaluate();
|
b.Evaluate();
|
||||||
|
Console.WriteLine(b.ToString());
|
||||||
|
Console.WriteLine("-------------");
|
||||||
Board h = s.Predict(new PredictRequest
|
Board h = s.Predict(new PredictRequest
|
||||||
{
|
{
|
||||||
Lives = b.Lives.Select(((int x, int y) cell) => new List<int>{cell.x, cell.y}).ToList(),
|
Lives = b.Lives.Select(((int x, int y) cell) => new List<int>{cell.x, cell.y}).ToList(),
|
||||||
Direction = "backward"
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user