Skip to content

Commit

Permalink
ICallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Mar 4, 2023
1 parent 76a964f commit cbf2d81
Show file tree
Hide file tree
Showing 32 changed files with 269 additions and 216 deletions.
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Console/Tensorflow.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.10.0" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/TensorFlowNET.Core/Keras/Engine/ICallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Tensorflow.Keras.Engine;

public interface ICallback
{
Dictionary<string, List<float>> history { get; set; }
void on_train_begin();
void on_epoch_begin(int epoch);
void on_train_batch_begin(long step);
void on_train_batch_end(long end_step, Dictionary<string, float> logs);
void on_epoch_end(int epoch, Dictionary<string, float> epoch_logs);
void on_predict_begin();
void on_predict_batch_begin(long step);
void on_predict_batch_end(long end_step, Dictionary<string, Tensors> logs);
void on_predict_end();
}
67 changes: 63 additions & 4 deletions src/TensorFlowNET.Core/Keras/Engine/IModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
namespace Tensorflow.Keras.Engine
using Tensorflow.Functions;
using Tensorflow.Keras.Losses;
using Tensorflow.Keras.Saving;
using Tensorflow.NumPy;

namespace Tensorflow.Keras.Engine;

public interface IModel : ILayer
{
public interface IModel
{
}
void compile(IOptimizer optimizer = null,
ILossFunc loss = null,
string[] metrics = null);

void compile(string optimizer, string loss, string[] metrics);

ICallback fit(NDArray x, NDArray y,
int batch_size = -1,
int epochs = 1,
int verbose = 1,
float validation_split = 0f,
bool shuffle = true,
int initial_epoch = 0,
int max_queue_size = 10,
int workers = 1,
bool use_multiprocessing = false);

void save(string filepath,
bool overwrite = true,
bool include_optimizer = true,
string save_format = "tf",
SaveOptions? options = null,
ConcreteFunction? signatures = null,
bool save_traces = true);

void save_weights(string filepath,
bool overwrite = true,
string save_format = null,
object options = null);

void load_weights(string filepath,
bool by_name = false,
bool skip_mismatch = false,
object options = null);

void evaluate(NDArray x, NDArray y,
int batch_size = -1,
int verbose = 1,
int steps = -1,
int max_queue_size = 10,
int workers = 1,
bool use_multiprocessing = false,
bool return_dict = false);

Tensors predict(Tensor x,
int batch_size = -1,
int verbose = 0,
int steps = -1,
int max_queue_size = 10,
int workers = 1,
bool use_multiprocessing = false);

void summary(int line_length = -1, float[] positions = null);

IKerasConfig get_config();
}
13 changes: 13 additions & 0 deletions src/TensorFlowNET.Core/Keras/Engine/IOptimizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Tensorflow.Keras.Engine;

public interface IOptimizer
{
Tensor[] aggregate_gradients(IEnumerable<(Tensor, IVariableV1)> grads_and_vars);
Tensor[] clip_gradients(Tensor[] grads);
void apply_gradients((Tensor, ResourceVariable) grads_and_vars,
string name = null,
bool experimental_aggregate_gradients = true);
void apply_gradients(IEnumerable<(Tensor, ResourceVariable)> grads_and_vars,
string name = null,
bool experimental_aggregate_gradients = true);
}
9 changes: 9 additions & 0 deletions src/TensorFlowNET.Core/Keras/IKerasApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow.Keras.Engine;
using Tensorflow.Keras.Layers;
using Tensorflow.Keras.Losses;
using Tensorflow.Keras.Metrics;
Expand All @@ -13,5 +14,13 @@ public interface IKerasApi
public ILossesApi losses { get; }
public IMetricsApi metrics { get; }
public IInitializersApi initializers { get; }

/// <summary>
/// `Model` groups layers into an object with training and inference features.
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
/// <returns></returns>
public IModel Model(Tensors inputs, Tensors outputs, string name = null);
}
}
6 changes: 2 additions & 4 deletions src/TensorFlowNET.Core/Keras/Layers/ILayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine;
using Tensorflow.Keras.Engine;
using Tensorflow.Keras.Saving;
using Tensorflow.Training;

Expand All @@ -15,7 +13,7 @@ public interface ILayer: IWithTrackable, IKerasConfigable
List<ILayer> Layers { get; }
List<INode> InboundNodes { get; }
List<INode> OutboundNodes { get; }
Tensors Apply(Tensors inputs, Tensor state = null, bool is_training = false);
Tensors Apply(Tensors inputs, Tensor state = null, bool training = false);
List<IVariableV1> TrainableVariables { get; }
List<IVariableV1> TrainableWeights { get; }
List<IVariableV1> NonTrainableWeights { get; }
Expand Down
6 changes: 3 additions & 3 deletions src/TensorFlowNET.Core/Tensorflow.Binding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>Tensorflow.Binding</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace>
<TargetTensorFlow>2.10.0</TargetTensorFlow>
<Version>0.100.4</Version>
<Version>1.0.0</Version>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
Expand All @@ -20,7 +20,7 @@
<Description>Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io</Description>
<AssemblyVersion>0.100.4.0</AssemblyVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageReleaseNotes>
tf.net 0.100.x and above are based on tensorflow native 2.10.0

Expand All @@ -38,7 +38,7 @@ https://tensorflownet.readthedocs.io</Description>
tf.net 0.7x.x aligns with TensorFlow v2.7.x native library.
tf.net 0.10x.x aligns with TensorFlow v2.10.x native library.
</PackageReleaseNotes>
<FileVersion>0.100.4.0</FileVersion>
<FileVersion>1.0.0.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<SignAssembly>true</SignAssembly>
Expand Down
112 changes: 56 additions & 56 deletions src/TensorFlowNET.Keras/Callbacks/CallbackList.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow.Keras.Engine;

namespace Tensorflow.Keras.Callbacks
namespace Tensorflow.Keras.Callbacks;

public class CallbackList
{
public class CallbackList
{
List<ICallback> callbacks = new List<ICallback>();
public History History => callbacks[0] as History;

public CallbackList(CallbackParams parameters)
{
callbacks.Add(new History(parameters));
callbacks.Add(new ProgbarLogger(parameters));
}

public void on_train_begin()
{
callbacks.ForEach(x => x.on_train_begin());
}

public void on_epoch_begin(int epoch)
{
callbacks.ForEach(x => x.on_epoch_begin(epoch));
}

public void on_train_batch_begin(long step)
{
callbacks.ForEach(x => x.on_train_batch_begin(step));
}

public void on_train_batch_end(long end_step, Dictionary<string, float> logs)
{
callbacks.ForEach(x => x.on_train_batch_end(end_step, logs));
}

public void on_epoch_end(int epoch, Dictionary<string, float> epoch_logs)
{
callbacks.ForEach(x => x.on_epoch_end(epoch, epoch_logs));
}

public void on_predict_begin()
{
callbacks.ForEach(x => x.on_predict_begin());
}

public void on_predict_batch_begin(long step)
{
callbacks.ForEach(x => x.on_predict_batch_begin(step));
}

public void on_predict_batch_end(long end_step, Dictionary<string, Tensors> logs)
{
callbacks.ForEach(x => x.on_predict_batch_end(end_step, logs));
}

public void on_predict_end()
{
callbacks.ForEach(x => x.on_predict_end());
}
List<ICallback> callbacks = new List<ICallback>();
public History History => callbacks[0] as History;

public CallbackList(CallbackParams parameters)
{
callbacks.Add(new History(parameters));
callbacks.Add(new ProgbarLogger(parameters));
}

public void on_train_begin()
{
callbacks.ForEach(x => x.on_train_begin());
}

public void on_epoch_begin(int epoch)
{
callbacks.ForEach(x => x.on_epoch_begin(epoch));
}

public void on_train_batch_begin(long step)
{
callbacks.ForEach(x => x.on_train_batch_begin(step));
}

public void on_train_batch_end(long end_step, Dictionary<string, float> logs)
{
callbacks.ForEach(x => x.on_train_batch_end(end_step, logs));
}

public void on_epoch_end(int epoch, Dictionary<string, float> epoch_logs)
{
callbacks.ForEach(x => x.on_epoch_end(epoch, epoch_logs));
}

public void on_predict_begin()
{
callbacks.ForEach(x => x.on_predict_begin());
}

public void on_predict_batch_begin(long step)
{
callbacks.ForEach(x => x.on_predict_batch_begin(step));
}

public void on_predict_batch_end(long end_step, Dictionary<string, Tensors> logs)
{
callbacks.ForEach(x => x.on_predict_batch_end(end_step, logs));
}

public void on_predict_end()
{
callbacks.ForEach(x => x.on_predict_end());
}
}
Loading

0 comments on commit cbf2d81

Please sign in to comment.